Light sensor connected to GPIO of Raspberry pi

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.jpg

I 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

Most digital inputs have a “no-mans-land” between what they consider “high” and what they consideer “low”. When they see something in that “no-mans-land”, things can bounce around. The simple solution is to add something called a [“Schmitt trigger” which has “hysteresis” – i.e., when the input is in “no-mans-land” it treats it as the last valid input. (Unfortunately, at the moment, Sparkfun is showing them as out of stock, but Digi-Key is showing the same part in stock [here.) BTW, the Digi-Key web site shows the supply voltage for this part to be 3V to 18V, though I only recall using them at 5V. Also note that this part is an “inverter”, meaning that a “high” on the input yeilds a “low” on the output and a “low” on the input yeilds a “high” on the output.](CD40106BE Texas Instruments | Integrated Circuits (ICs) | DigiKey)](https://www.sparkfun.com/products/13952)

You might consider using an OP AMP as a [comparator.

That would allow you to set the specific light level that gives you a low or high output and you don’t need too many parts.](https://www.electronics-tutorials.ws/opamp/op-amp-comparator.html)