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!
Regards
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 yesterday
-
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 yesterday
-
1@gerzan: A schematic might be better than pictures. Here’s how – Seamus yesterday
-
2your response to comments needs to be edited into your question not in a string of comments. – Steve Robillard yesterday
-
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 18 hours ago
Question
The OP’s has a relay module, but his python program is not working. How to fix it?
Answer
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. Manual/By hand/offline Module test 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. 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, …
References
/ to continue, …
Appendices
/ to continue, …
Figures
/ 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.
See https://raspberrypi.stackexchange.com/a/100014/8697
The particular model you linked is even worse than most! It would not meet the standards for mains isolation.
Categories: Uncategorized