0

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).

This is the circuit: Raspberry 2b and a relay

This is a picture of the relay: Relay

tlfong01
  • 4,384
  • 3
  • 9
  • 23
gerzan
  • 9
  • 2
  • 2
    Would help if you documented the cabling (esp with pictures) and what relay module you have. –  Jun 08 '20 at 19:57
  • 1
    You're right. Relay: https://www.amazon.com/Channel-optocoupler-Compatible-Atomic-Market/dp/B00TMFVVG6 / 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 08 '20 at 20:02
  • 1
    @gerzan: A schematic might be better than pictures. [Here's how](https://raspberrypi.meta.stackexchange.com/questions/2074/how-do-i-add-a-schematic-to-my-question?r=SearchResults&s=1|28.1004) – Seamus Jun 08 '20 at 20:52
  • 3
    your response to comments needs to be edited into your question not in a string of comments. – Steve Robillard Jun 08 '20 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 09 '20 at 06:41
  • 1
    @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 (https://www.sossolutions.nl/2x-relais) and now it's running the way it should. Thanks for the help! – gerzan Jun 27 '20 at 07:23
  • @gerzan, Now nice to hear the good news. Have a great project. Cheers. – tlfong01 Jun 27 '20 at 07:42

3 Answers3

1

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.

Milliways
  • 54,718
  • 26
  • 92
  • 182
1

I'm sorry not being here a month ago, because that relay board is the perfect one for Raspberry Pi and it works smoothlessly.

Main things to consider are three:

  • these relays are 5V (Amazon main picture wrongly shows 12V version)
  • the inputs are "active low" meaning that connecting them to ground results in corresponding relay coil to receive current and the corresponding NO relay output being connected to the corresponding relay common
  • a JDVcc jumper is present to differentiate power between logic section of the board and relays section

You did two things wrong:

  • you wired both the relays and the rest of the board to 5V
  • your program is supposing to activate the relays with a HIGH pin level

How to solve:

  • power the board logic section with 3.3V, connecting Vcc with 3.3V Raspberry Pi pin
  • power the relays with 5V, connecting [JD-Vcc][1] with 5V Raspberry Pi pin
  • swap the logic levels for ON and OFF in your code, this way (minimum modifications)
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
GPIO.output(22, 1)                      # set relays initially non active
GPIO.output(23, 1)

try:
    while True:
        input_state = GPIO.input(18)
        if input_state == False:
            print('Button push')
            GPIO.output(22, 0)
            GPIO.output(23, 0)
            sleep(0.5)
            GPIO.output(22, 1)
            GPIO.output(23, 1)
except KeyboardInterrupt:          # trap a CTRL+C keyboard interrupt  
    GPIO.cleanup()

From an electrical point of view, my measurements says that:

  • when both GPIO22 and GPIO23 are LOW, those pins drew less than 1mA together (0.44mA each)
  • when both GPIO22 and GPIO23 are HIGH, both 5V and 3.3V pins do not reach 1mA current
  • when both relays are active they drew from 5V pin a total of 101mA, more or less half of it each

[both][2] these measurements are totally compatible with Raspberry Pi.

In my current applications these relays do not manage mains (230V here) and I strongly encourage you to do the same, given your apparent level of experience and the concrete risk of electrocution or Raspberry Pi blowing.

[1]: JD seems to mean "relay" in chinese, find this and other more deep and interesting information at https://electronics.stackexchange.com/questions/505318/how-to-properly-use-a-relay-module-with-jd-vcc-from-arduino-raspberry

[2]: What is the maximum current the GPIO pins can output?, https://www.raspberrypi.org/forums/viewtopic.php?p=158827

mrq
  • 111
  • 3
1

Try removing the jumper and powering VCC with 3.3V and JDVCC with 5V. I have used similar relays with 3.3V systems without any problems.

Dmitry Grigoryev
  • 26,688
  • 4
  • 44
  • 133