Page 1 of 1

Light sensor connected to GPIO

Posted: Tue Jun 09, 2020 2:23 pm
by pirx1988
Hi, I have created simple light dector. Schemea is below:
Image

Real circuit on breadboard:

Image

I have written code in python using GPIO library:

Code: Select all

import signal
import sys
import RPi.GPIO as GPIO
import sqlite3
import datetime
INPUT_GPIO = 16
def save_light_event_in_db():
    try:
        conn = sqlite3.connect('/home/pi/databases/light_sensor.db')
        cur = conn.cursor()
        cur.execute('SELECT * from light_events')
        light_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        message = "Wykryto świt!"
        sqlite_insert_with_param = """INSERT INTO light_events
                              (created_at, message)
                              VALUES (?, ?);"""
        data_tuple = (light_time, message)
        cur.execute(sqlite_insert_with_param, data_tuple)
        conn.commit()
    except Error as e:
            print(e)
    finally:
       if conn:
           conn.close()

def signal_handler(sig, frame):
    GPIO.cleanup()
    sys.exit(0)
def light_detection_callback(channel):
    print("Yeah, juz swita, wstawać!")
    save_light_event_in_db()
    
if __name__ == '__main__':
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(INPUT_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.add_event_detect(INPUT_GPIO, GPIO.FALLING, 
            callback=light_detection_callback, bouncetime=1000) 
    signal.signal(signal.SIGINT, signal_handler)
    signal.pause()


This circuit should detect only situation when light has changed from night to dawn. I have configured pull up resistor on board and I've used and GL5539, which has light resistance in range 50-10k, and dark resistance 5 mega ohm. The problem is, it records also events with. twilight I don't know wy. Where is a problem?
I would be grateful for explanation, some advices.
Best regards

Re: Light sensor connected to GPIO

Posted: Wed Jun 10, 2020 11:09 am
by boyoh
pirx1988 wrote:
Tue Jun 09, 2020 2:23 pm
Hi, I have created simple light dector. Schemea is below:
Image

Real circuit on breadboard:

Image

I have written code in python using GPIO library:

Code: Select all

import signal
import sys
import RPi.GPIO as GPIO
import sqlite3
import datetime
INPUT_GPIO = 16
def save_light_event_in_db():
    try:
        conn = sqlite3.connect('/home/pi/databases/light_sensor.db')
        cur = conn.cursor()
        cur.execute('SELECT * from light_events')
        light_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        message = "Wykryto świt!"
        sqlite_insert_with_param = """INSERT INTO light_events
                              (created_at, message)
                              VALUES (?, ?);"""
        data_tuple = (light_time, message)
        cur.execute(sqlite_insert_with_param, data_tuple)
        conn.commit()
    except Error as e:
            print(e)
    finally:
       if conn:
           conn.close()

def signal_handler(sig, frame):
    GPIO.cleanup()
    sys.exit(0)
def light_detection_callback(channel):
    print("Yeah, juz swita, wstawać!")
    save_light_event_in_db()
    
if __name__ == '__main__':
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(INPUT_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.add_event_detect(INPUT_GPIO, GPIO.FALLING, 
            callback=light_detection_callback, bouncetime=1000) 
    signal.signal(signal.SIGINT, signal_handler)
    signal.pause()


This circuit should detect only situation when light has changed from night to dawn. I have configured pull up resistor on board and I've used and GL5539, which has light resistance in range 50-10k, and dark resistance 5 mega ohm. The problem is, it records also events with. twilight I don't know wy. Where is a problem?
I would be grateful for explanation, some advices.
Best regards
From BoyOh
Change the 100k for preset to give the circuit some sensitivity adjustment, but still leave a low value resistor in series with it
as protection, Also some opaque masking over the LDR might help .

Nothing ventured nothing gained , Part of the learning curve

Regards BoyOh

Re: Light sensor connected to GPIO

Posted: Wed Jun 10, 2020 11:34 am
by drgeoff
Agreed that the 100k resistor should be adjusted. However, no need for "protection" as even if zero ohms is there, nothing will be damaged.

Re: Light sensor connected to GPIO

Posted: Wed Jun 10, 2020 1:03 pm
by PiGraham
You have a PNP transistor that will pull the gpio pin high when the LDR is lit.
You have an internal pullup which won't do anything.
You have an external pulldown that will hold the gpio low in the dark.
You try to detect falling edges, which will be darkening events, wen the transistor turns off because the LDR goes high impeadance..
You describe "light events" so maybe you intended to detect rising edges

Use a potentiometer so you can adjust the trigger level.

Re: Light sensor connected to GPIO

Posted: Wed Jun 10, 2020 7:14 pm
by boyoh
drgeoff wrote:
Wed Jun 10, 2020 11:34 am
Agreed that the 100k resistor should be adjusted. However, no need for "protection" as even if zero ohms is there, nothing will be damaged.
drgeoeff
Not detrimental to circuit operation , but you have proved your point

Regards BoyOh

Re: Light sensor connected to GPIO

Posted: Wed Jun 10, 2020 11:30 pm
by emma1997
Doubtful anything but the LDR by itself is required. Using a built-in Pi pullup will do the job. I use a similar sensor without any other parts to inhibit an outdoor spotlight at dawn and enable at dusk. Simply masking with a tiny bit of tape proves more than adequate for any sensitivity adjustments.