Hi, I have created simple light dector. Schemea is below:
https://i.ibb.co/2hRQVJj/light-detector.jpg
Real circuit on breadboard:
https://i.ibb.co/QN5RhvH/light-real-circut.jpgI have written code in python using GPIO library:
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