0
\$\begingroup\$

I have created simple light dector. Schemea is below:

enter image description here

Real circuit on breadboard:

[img]https://i.ibb.co/QN5RhvH/light-real-circut.jpg[/img]

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 event only when light has turned from night to dawn. I have configured pull up resistor on board. On board I use: GL5539 photoresistor, which has light resistance in range 50-10k, dark resistance 5 mega ohm, 2N3906 PNP transistor.

The problem is, it records also events with twilight. I have checked voltage level on photoresistor and there is 2.63 V in daily light, but when I put my finger on photoresistor, then voltage is rising up to about 3.0 V(small than 0.7 V) and event is detected by input GPIO port. Why? When I takee off my finger, then event is alos detect and that is correct. But first siutation shouldn't be detected. I would be grateful for explanation, some advices. Best regards

\$\endgroup\$
5
  • \$\begingroup\$ You need to state unambiguously what you want the GPIO pin voltage level to do when you shade the phtoresistor. At the moment it sounds like it's doing what the schematic implies it should do. \$\endgroup\$ Commented Jun 10, 2020 at 12:28
  • \$\begingroup\$ I expect if there is shade then there shouldn't be any reaction on GPIO. Reaction should be only when there is changes from 0(night) light to daylight. \$\endgroup\$ Commented Jun 10, 2020 at 12:40
  • \$\begingroup\$ There will be a change when daylight goes to night time too with this circuit. Given that you are using a GPIO you can use code to detect the change from night to daylight and ignore the change from day to night. \$\endgroup\$ Commented Jun 10, 2020 at 12:45
  • \$\begingroup\$ How should I change circuit to detect only changes from night to daylight? In code I have only detection caused by falling voltage to the ground GPIO.FALLING, so I expected it will work only in one of those two cases. \$\endgroup\$ Commented Jun 10, 2020 at 13:43
  • \$\begingroup\$ Make the decision in software. Edit the code to make it work the other way. \$\endgroup\$ Commented Jun 10, 2020 at 13:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.