Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4804

Automation, sensing and robotics • Re: code PIR motion sensor to trigger a relay On/Off not working

$
0
0

So it must be the relay - is it an "active low" module and can you show a picture of it and the wiring?
i bought from ebay: https://www.ebay.com/itm/292160479941
seems "active low" according to description. here is the relay. once i run the py script, relay instantly turns On and refuses to turn Off
Image. even when i stop the script. running it again gives this error "
/home/pi/motion_relay2.py:16: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(RELAY_PIN, GPIO.OUT)
"
i dont know how to get away from error except by rebooting the rpi4.

here is my current schematic based on Claude.ai:
Image
Claude.ai gave me this py script but it is having the exact same issues as before:

Code:

import timeimport RPi.GPIO as GPIO# PIR Motion Sensor GPIO pinPIR_PIN = 4# Relay GPIO pinRELAY_PIN = 17# Time delay before turning off relay (in seconds)DELAY_TIME = 300  # 5 minutesdef setup():    GPIO.setmode(GPIO.BCM)    GPIO.setup(PIR_PIN, GPIO.IN)    GPIO.setup(RELAY_PIN, GPIO.OUT)    GPIO.output(RELAY_PIN, GPIO.LOW)  # Relay initially offdef motion_detected():    if GPIO.input(PIR_PIN) == GPIO.HIGH:        return True    else:        return Falsedef turn_relay_on():    GPIO.output(RELAY_PIN, GPIO.HIGH)    print("Relay turned on")def turn_relay_off():    GPIO.output(RELAY_PIN, GPIO.LOW)    print("Relay turned off")def main():    setup()    last_motion_time = time.time()    while True:        if motion_detected():            turn_relay_on()            last_motion_time = time.time()        elif time.time() - last_motion_time > DELAY_TIME:            turn_relay_off()        time.sleep(1)if __name__ == "__main__":    main()

Statistics: Posted by tung256 — Mon Sep 09, 2024 11:43 pm



Viewing all articles
Browse latest Browse all 4804

Trending Articles