Reputation: 437
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.
Upvotes: 3
Views: 546
Reputation: 1
The relay board that you are using maybe works on reverse logic. It means if the gpio state is high, then the relay will be off and if gpio state is low the relay will be in on state. That's why you are facing this problem.
Relay------Raspberry pi
Vcc ------ 5V as the relay is 5V it needs 5V to work
Input------ GPIO
GND ------ GND
5v relay may work if the trigger voltage of that relay board board is in the range of the raspberry pi gpio output voltage. For that you can check the Datasheet of the optocoupler that's on relay board.
But using 5V relay with raspberry pi gpio directly is not recommended as the gpio work on 3.3V signal using a leave shift or an optocoupler between two is highly recommended.
Upvotes: 0
Reputation: 437
Connecting the vcc of the relay to 3.3v of the RPi caused the relay to turn off correctly. I am not sure if this is the solution for this.
Upvotes: 0