Read the statement by Michael Teeuw here.
{HowTo} turn on/off your monitor (Time based, PIR/Button, App)
-
Hello everyone, I’m getting desperate. I’ve now read the entire tutorial but I can’t find my mistake.
I am using my MM with a PIR sensor and would like to switch the backlight of the display with a relay. so i use this script pir.py:
#!/usr/bin/env python import sys import time import RPi.GPIO as io import subprocess io.setmode(io.BCM) SHUTOFF_DELAY = 120 # in seconds, how long the monitor will be on until next button press or PIR detection PIR_PIN = 22 # 15 on the board (this needn't to be a PIR. Can be a button also) LED_PIN = 16 # optional def main(): io.setup(PIR_PIN, io.IN) io.setup(LED_PIN, io.OUT) turned_off = False last_motion_time = time.time() while True: if io.input(PIR_PIN): last_motion_time = time.time() io.output(LED_PIN, io.LOW) print ".", sys.stdout.flush() if turned_off: turned_off = False turn_on() else: if not turned_off and time.time() > (last_motion_time + SHUTOFF_DELAY): turned_off = True turn_off() if not turned_off and time.time() > (last_motion_time + 1): io.output(LED_PIN, io.HIGH) time.sleep(.1) def turn_on(): subprocess.call("sh /home/pi/monitor_on.sh", shell=True) def turn_off(): subprocess.call("sh /home/pi/monitor_off.sh", shell=True) if __name__ == '__main__': try: main() except KeyboardInterrupt: io.cleanup()
Unfortunately, when I run this script in the terminal, I only get the feedback from the PIR sensor (…) but the relay is not switched. But if I run the monitor_on.sh or monitor_off.sh script it works:
monitor_on.sh
gpio -g mode 27 out sleep 0.2 gpio -g write 27 1
monitor_off.sh
gpio -g mode 27 out sleep 0.2 gpio -g write 27 0
But the PIR.py doesn’t work. Where could the mistake lie?
Many greetings Andreas
-
@andi-aw-weiss I think you are using the wrong script. With just a quick look it seems you’re running the script from point 2.2 (Using PIR script) of the first post. What you want is point 5.2 (Use relay with a PIR). That script used in 5.2 is different than that of 2.2, which does not have code to control the relay.
Have a look at it again.
-
@MadScientist Hey, unfortunately the problem hasn’t changed. I have now like you said the script from 5.2. The problem remains. PIR detects motion. Relay does not switch. :loudly_crying_face: I don’t know
-
@MadScientist ok it seems to be working now. However, the relay switches the first time after the 120 s have expired and a movement is detected. not previously. The change quote: “io.output(RELAY_PIN, False)” and for monitor off “io.output(RELAY_PIN, True)” only causes the relay to work in reverse. I now use this script:
#!/usr/bin/env python import sys import time import RPi.GPIO as io import subprocess io.setmode(io.BCM) SHUTOFF_DELAY = 120 # in seconds, how long the monitor will be on until next button press or PIR detection PIR_PIN = 22 # 15 on the board (this needn't to be a PIR. Can be a button also) LED_PIN = 17 # 11 on the board (optional): It just shows detection time of PIR without delay time RELAY_PIN = 27 # 13 on the board def main(): io.setup(PIR_PIN, io.IN) io.setup(LED_PIN, io.OUT) io.setup(RELAY_PIN, io.OUT) turned_off = False last_motion_time = time.time() while True: if io.input(PIR_PIN): last_motion_time = time.time() io.output(LED_PIN, io.LOW) print ("."), sys.stdout.flush() if turned_off: turned_off = False turn_on() else: if not turned_off and time.time() > (last_motion_time + SHUTOFF_DELAY): turned_off = True turn_off() if not turned_off and time.time() > (last_motion_time + 1): io.output(LED_PIN, io.HIGH) time.sleep(.1) def turn_on(): io.output(RELAY_PIN, True) def turn_off(): io.output(RELAY_PIN, False) if __name__ == '__main__': try: main() except KeyboardInterrupt: io.cleanup()
where could the error lie? or is that how it should be? Thank you MadScientist
-
Hey hey,
So what exactly doesn’t work? My English isn’t the best
In my case I am turning on a LED Stripe with the pir.
When I cold start the raspi, the LED stripe turns on for a few seconds (could be the turnoff delay time).
After a while it turns off.
From this moment on it should work as you expect. When movement is detected, the led (or relay) turns on for the delay time and extend the delay time for every new movement within the original delay time. -
@andi-aw-weiss said in {HowTo} turn on/off your monitor (Time based, PIR/Button, App):
The change quote: “io.output(RELAY_PIN, False)” and for monitor off “io.output(RELAY_PIN, True)” only causes the relay to work in reverse. I now use this script:
Are you saying the screen turns off when the PIR detects a motion? In that case you have to change the the terminals on the relay. You have three terminals to connect the wires to. GND, normal open and normal close.
Another note: I have a bunch of relays here that are supposed to be compatibles with Raspberry Pi’s but somehow they don’t properly work. They switch exactly once and then are stuck. Not sure if you’re having the same issues.
-
@cruunnerr @MadScientist Thanks to both of you . For development/tutorial and support. My MM works. I was probably impatient the whole time I was testing. The relay only switches for the first time after the 120 s have expired. But that’s ok that it only occurs when you restart. I consider the problem solved. THANK YOU