I bought RPi, flashed the OS. I also bought a 5V relay. I have connected the 2nd pin to VCC of the relay, 6th pin to GND of the relay, 40th pin to the 'IN' point of the relay. At present I have not connected anything to the other side of the relay (but the problem also exists if I connect a load of led). Also no other changes have been made to the RPi except the SSH connection enablement and software update using sudo apt get commands i.e. RPi is almost at factory settings.
My code:
import RPi.GPIO as GPIO
import time
in1 = 29 # i.e 40th pin
GPIO.setmode(GPIO.BCM)
GPIO.setup(in1, GPIO.OUT)
try:
GPIO.output(in1, GPIO.HIGH) # 1
time.sleep(1)
GPIO.output(in1, GPIO.LOW) # 2
print("inside try after low") # 3
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup() # 4
My problem:
Once the relay turns high at comment # 1
it does not turns off at point # 2
. I kill the program using 'control + c' then it turns off which is not what I desire. I just want it to turn on and then turn off.
The point # 3
does get executed. # 4
executes when ctrl+c pressed. If I remove cleanup code and do not press ctrl+c then the relay's green light remains turned on continuously.
This is raspberry pi 64bit OS.
while
loop as described in the answer from the link. Don't know why it would make a difference thoughGPIO.output(in1, GPIO.LOW)
. I found a quick explainer about this stuff here. I assume that you can configure pull-up/pull-down resistors through RPi.GPIO, as described here