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

7
  • 2
    Would help if you documented the cabling (esp with pictures) and what relay module you have.
    – user115418
    Jun 8 '20 at 19:57
  • 1
    You'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 '20 at 20:02
  • 1
    @gerzan: A schematic might be better than pictures. Here's how
    – Seamus
    Jun 8 '20 at 20:52
  • 3
    your response to comments needs to be edited into your question not in a string of comments. Jun 8 '20 at 20:54
  • 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 (sossolutions.nl/2x-relais) and now it's running the way it should. Thanks for the help!
    – gerzan
    Jun 27 '20 at 7:23
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.

1
  • Your were right, I ordered a new one and now it's working!
    – gerzan
    Jun 27 '20 at 7:23
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

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.