Just starting to explore python, Raspberry and a relay. I want to control my 2-channel relay with a button. This is my test code:
import RPi.GPIO as GPIO import time import requests from datetime import datetime from time import sleep GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) # button GPIO.setup(22, GPIO.OUT) # IN1 on relay GPIO.setup(23, GPIO.OUT) # IN2 on relay try: while True: input_state = GPIO.input(18) if input_state == False: print('Button push') GPIO.output(22, 1) GPIO.output(23, 1) sleep(0.5) GPIO.output(22, 0) GPIO.output(23, 0) except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt GPIO.cleanup()
Now when I run the code, the relay is switched on and only powered off when I hit ctrl-c. The push of the button has no effect. Just the GPIO.setup seems to trigger the relay. I power the relay with 5V from the Raspberry (2B).
Probably some newbie coding failure. Any help is appreciated!
edit: This is the relay I’m trying to use. I bought some years ago, I think from eBay (not Amazon).
2Would help if you documented the cabling (esp with pictures) and what relay module you have. – Andyroo Jun 8 at 19:57
1You’re right. Relay: amazon.com/Channel-optocoupler-Compatible-Atomic-Market/dp/… / 5v from raspberry to vcc and same with ground / pin 22 to IN1 on relay / pin 23 to IN2 on relay / Will add some pictures asap – gerzan Jun 8 at 20:02
1@gerzan: A schematic might be better than pictures. Here’s how – Seamus Jun 8 at 20:52
2your response to comments needs to be edited into your question not in a string of comments. – Steve Robillard Jun 8 at 20:54
You relay looks good, perhaps just wiring a bit wrong. I am compiling an answer below. Perhaps confirm (1) Your relay is indeed from Amazon, not eBay, (2) You relay looks exactly the same as shown in my answer, (3) The Songle relay switch module marks 5V, not 12V. – tlfong01 Jun 9 at 6:41
@tlfong01 Sorry for the late reply. I did the testing you suggested and the relay behaved not the way it should. I ordered a new relay (sossolutions.nl/2x-relais) and now it’s running the way it should. Thanks for the help! – gerzan 2 mins ago
The OP’s has a relay module, but his python program is not working. How to fix it?
1. First thing first. Let us confirm that the following is the OP’s relay module.
2. Let us look at the relay’s schematic and how to do the wiring.
3. 3V3 Logical Level Signal Manual Testing (Without using Rpi)
a. Put Rpi aside. Do not use the Rpi’s 5V and 3V3 rail.
b. Remove JD-Vcc jumper.
c. Connect JD-Vcc terminal to 5V power (External power, not Rpi’s 5V rail)
d. Connect Vcc terminal to 3V3 power (External power, not Rpi’s 3V3 rail, can use 3V battery)
e. Use a jumper wire and connect one end to IN1 terminal.
f. By hand, connect/touch the other jumper end to ground to see if the LED and relay switch turn on/click.
g. Touch jumper wire to 3V/3V3 and see if LED and relay turn off,
h. Use a multimeter to measure the driver current, between IN1 and ground. The current should be less than 5mA.
4. 5V0 Logical Level Signal Manual Testing (Without using Rpi)
If you are using a logical level converter (for example NPN BJT 2N2222) to shift up Rpi GPIO pin’s 3V3 logical signal to 5V, then you don’t need to remove the JDVcc jumper, and no need to power the input circuit with 3V3 power, as shown below. The testing procedure is same as above, except you are now using 5V power and also 5V logic level signal to test the module.
5. Python program to toggle relay
# relay toggle test 2019jan10 tlfong01 2019jan109hkt1625 *** # Rpi3B+ stretch (Linux 4.14.34-v7+ arm) IDLE python 3.5.3 # Program Description # 1. Setup Rpi.GPIO BCM mode # 2. Set GPIO11 as relayPin to control 5V relay KY019 # 3. Toggle 100 times relayPin at 1Hz import RPi.GPIO as GPIO from time import sleep # *** GPIO Functions *** def setGpioMode(): GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) return # *** Set Gpio pin as output *** def setGpioPinOutput(gpioPin): GPIO.setup(gpioPin, GPIO.OUT) return # *** Set Gpoio pin High/Low *** def setGpioPinHigh(gpioPin): GPIO.output(gpioPin, GPIO.HIGH) return def setGpioPinLow(gpioPin): GPIO.output(gpioPin, GPIO.LOW) return # *** Toggle Gpio pin *** def toggleGpioPin(gpioPin, highSecond, lowSecond, count): for i in range(count): setGpioPinHigh(gpioPin) sleep(highSecond) setGpioPinLow(gpioPin) sleep(lowSecond) return # *** Main *** print('Begin togglePin(), ...') relayPin = 11 highSecond = 1 lowSecond = 1 count = 100 setGpioMode() setGpioPinOutput(relayPin) toggleGpioPin(relayPin, highSecond, lowSecond, count) print('End togglePin().') # *** End ***
/ to continue, …
Relay Newbie FAQ
Q1. Why relay is always turned on?
Q2. Why relay is turned off only when there is the Ctrl-C interrupt?
Q3. Is there another way to turn off the relay beside the Ctrl-C interrupt?
Q3. What does the JDVcc jumper do?
Q4. Is the JDVcc jumper only for Rpi?
Q5. Is there any work around if no JDVcc jumper is available?
Q6. Is it safe to power Vcc and JDVcc with 5V, with the JDVcc jumper on?
Q7. Is the optical isolator really necessary?
Q8. What is the meaning of Total optical isolation?
Q8. Is it safe to let the relay module to pass 10A?
Q9. Is it safe to let the relay module to switch 250VAC?
Q10. How many types of relay modules are there?
Q11. Is there any relay selection guide for the relay newbies?
Q12. Is it OK to get a fake relay module from eBay?
/ to continue, …
(1) Amazon 5V Two 2 Channel Relay Module with optocoupler Compatible with Arduino PIC AVR DSP ARM by Atomic Market
/ to continue, …
Appendix A – Songle 5V Relay Switch Characteristics
Appendix B – Simple High Level Triggered Relay Module without JDVcc jumper and optical isolation
Appendix C – Relay with High / Low Level Triggered Select Jumper
The above eBay relay is an example of High/Low level trigger select relay.
The relay can be used for Rpi as High or Low trigger as summarized below:
(1) High trigger
No modification is necessary. Just jumper H and there you go.
(2) Low trigger
There is 50% chance that Low trigger won’t work, because Rpi’s High signal (about 3V) is not High enough (need 4.2V) to untrigger the relay. The workaround is simple: just add a series 4k7 to the IN terminal (See Appendix D’s discussion section). Rpi’s GPIO signal now goes to the other end of 4k7 not connected to IN.
Warning: eBay can give you a big surprise: No guarantee no nothing won’t melt down or blow up. You burn Me no burn. Good luck.
Appendix D – How to modify an Arduino High Trigger relay module to make it Rpi compatible
Discussion – In the above High trigger application, Rpi High signal is not high enough to pass big enough current to drive the LED, and the workaround is to decrease the series resistance by adding another resistance in parallel.
In an complimentary case where a relay or buzzer is low triggered, the Rpi’s High is not high enough to deactivate/untrigger the LED or switching transister. Then the workaround is to add a series resistor to the input terminal, so that the even Rpi High is not high enough, input current is smaller now and so can deactivate/untrigger the LED/switching transistor. See the following Q&A for an example of active buzzer.
Active Piezo Buzzer Makes Sound On Both Rpi GPIO Low and High Level Signal
/ to continue, …
/ to continue, …
You are another victim of the shoddy products for sale on the web.
This is not a coding problem – the relay is UNSUITABLE for the Pi.
They can be only be used with additional circuitry, or modifying the module.
The particular model you linked is even worse than most! It would not meet the standards for mains isolation.