So, I'm trying to set up my raspberry pi to take input from a PIR sensor in order to automatically turn on a monitor when it detects motion. If no motion has been detected in 60 seconds, the monitor is turned off until motion is again detected. I also have it set so that between 7-10am and 5-8pm the monitor will just stay on.
In testing though, I've noticed a peculiar thing happening: Every x seconds or y seconds (the number of seconds seems to vary whenever I restart the pi or change sensors, but it always occurs at two different amounts), I detect a false positive (despite there being no motion at all). Now, the PIR I'm using (HC-SR501) has configurable sensitivity and time delay. I've tried changing both and there seems to be no effect.
I've tried four different sensors now, and all four give this false positive very consistently.
Below you can see my (python) code. If anyone can help me out with why this may be happening and how to fix it, I'd appreciate it.
#!/usr/bin/env python
import sys
import time
import RPi.GPIO as io
import subprocess
io.setmode(io.BCM)
SHUTOFF_DELAY = 60 # seconds
PIR_PIN = 25 # 22 on the board
def main():
io.setup(PIR_PIN, io.IN, pull_up_down=io.PUD_DOWN)
turned_off = False
previous_state = False
last_motion_time = time.time()
while True:
currentHour = time.localtime().tm_hour
# if it's 7-10am or 5-8pm, turn the screen on and poll less often
if ((7 <= currentHour < 10) or (17 <= currentHour < 20)):
print 'The current hour is {}, screen should remain on'.format(currentHour)
sys.stdout.flush()
if turned_off:
turned_off = False
turn_on()
time.sleep(60)
# if it's not during one of our 'busy hours', look for motion
else:
if io.input(PIR_PIN) and previous_state == False:
print "It's been {} seconds since the last motion\n".format(time.time()-last_motion_time)
sys.stdout.flush()
last_motion_time = time.time()
previous_state = True
if turned_off:
turned_off = False
turn_on()
elif previous_state == true:
if not turned_off and time.time() > (last_motion_time + SHUTOFF_DELAY):
turned_off = True
turn_off()
previous_state = False
time.sleep(.1)
def turn_on():
subprocess.call("sh /home/pi/MotionSensor/monitor_on.sh", shell = True)
def turn_off():
subprocess.call("sh /home/pi/MotionSensor/monitor_off.sh", shell = True)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
io.cleanup()