I have connected a 2 channel 12 v relay like this to the GPIO of my Raspberry pi. The relay is used to switch on and off an AC lightbulb.
I am using this code to turn on/off the relay:
import RPi.GPIO as io
import time
io.setmode(io.BOARD)
io.setup(5,io.OUT)
io.output(5, not io.input(5))
Connected like this:
schematic
simulate this circuit – Schematic created using CircuitLab
The light instantly turns off when it is already on, but not the other way.
The problem: I have to tap the relay in order for the light to turn on after running the code. The amount of time after running the code does not matter.
-
1\$\begingroup\$ What voltage are you supplying to it? \$\endgroup\$Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2017年12月11日 15:11:42 +00:00Commented Dec 11, 2017 at 15:11
-
\$\begingroup\$ What do your connections look like? Can you post a schematic? \$\endgroup\$evildemonic– evildemonic2017年12月12日 15:39:41 +00:00Commented Dec 12, 2017 at 15:39
-
\$\begingroup\$ Supplying 5v from the Raspberry Pi @ignacio-vazquez-abrams \$\endgroup\$Alfred Løvgren– Alfred Løvgren2017年12月12日 16:34:15 +00:00Commented Dec 12, 2017 at 16:34
-
1\$\begingroup\$ Updated now @evildemonic \$\endgroup\$Alfred Løvgren– Alfred Løvgren2017年12月12日 16:34:48 +00:00Commented Dec 12, 2017 at 16:34
-
1\$\begingroup\$ The relays shown in your link require 12 Volts for proper operation. They are unlikely to operate with only 5 volts. \$\endgroup\$Peter Bennett– Peter Bennett2017年12月12日 17:03:18 +00:00Commented Dec 12, 2017 at 17:03
2 Answers 2
The board you are using is intended for 12V VCC, and has 12V relays. A typical relay may only trigger on 80% of its nominal coil voltage, but it will hold at 40% of the coil voltage. This means that when you power it at 5V, it's not enough to turn on, but once you physically force the relay to close, then the 5V is fine.
While there are ways to use the existing board, your best option is getting a 5V relay board.
You say it's a 12V relay, but you're only running the coil on 5V. Is the rated coil voltage compatible with a 5V drive?
You may also be hitting the GPIO maximum sink current connecting a relay directly to the GPIO pin, in which case, the relay may not even be getting the full 5V.
-
\$\begingroup\$ The board uses a opto isolator and a transistor driver for the relay, so the 3.3V gpio is not directly driving the relay. \$\endgroup\$Passerby– Passerby2017年12月12日 17:22:34 +00:00Commented Dec 12, 2017 at 17:22
-
\$\begingroup\$ @Passerby In that case, your diagram is wrong, as it shows 5V going to the relay coil. \$\endgroup\$Simon B– Simon B2017年12月12日 17:33:43 +00:00Commented Dec 12, 2017 at 17:33
-
\$\begingroup\$ Not my diagram, op has simplified the board. It's more of a block diagram. \$\endgroup\$Passerby– Passerby2017年12月12日 17:51:07 +00:00Commented Dec 12, 2017 at 17:51