3

I bought RPi, flashed the OS. I also bought a 5V relay. I have connected the 2nd pin to VCC of the relay, 6th pin to GND of the relay, 40th pin to the 'IN' point of the relay. At present I have not connected anything to the other side of the relay (but the problem also exists if I connect a load of led). Also no other changes have been made to the RPi except the SSH connection enablement and software update using sudo apt get commands i.e. RPi is almost at factory settings.

My code:

import RPi.GPIO as GPIO
import time

in1 = 29  # i.e 40th pin 

GPIO.setmode(GPIO.BCM)
GPIO.setup(in1, GPIO.OUT)

try:
    GPIO.output(in1, GPIO.HIGH)  # 1
    time.sleep(1)
    GPIO.output(in1, GPIO.LOW)  # 2
    print("inside try after low")  # 3
    time.sleep(1)
      
except KeyboardInterrupt:
    GPIO.cleanup()  # 4

My problem: Once the relay turns high at comment # 1 it does not turns off at point # 2. I kill the program using 'control + c' then it turns off which is not what I desire. I just want it to turn on and then turn off.

The point # 3 does get executed. # 4 executes when ctrl+c pressed. If I remove cleanup code and do not press ctrl+c then the relay's green light remains turned on continuously.

This is raspberry pi 64bit OS.

5
  • 1
    Just adding a documentation link as a reference
    – Pieterjan
    Commented May 13 at 4:41
  • 1
    You probably have to use a while loop as described in the answer from the link. Don't know why it would make a difference though
    – Pieterjan
    Commented May 13 at 4:46
  • 1
    sorry to skip it. I just want it to turn on and turn it off. removed some code for brevity to focus just on the issue. edited the last line now.
    – LearneriOS
    Commented May 13 at 4:50
  • Connecting the vcc of the relay to 3.3v of the RPi caused the relay to turn off correctly. I am not sure if this is the solution for this.
    – LearneriOS
    Commented May 13 at 9:14
  • 1
    In that case it would sound to me that you're having a floating output, that's neither connected to vcc nor gnd after calling GPIO.output(in1, GPIO.LOW). I found a quick explainer about this stuff here. I assume that you can configure pull-up/pull-down resistors through RPi.GPIO, as described here
    – Pieterjan
    Commented May 13 at 9:48

3 Answers 3

0

Connecting the vcc of the relay to 3.3v of the RPi caused the relay to turn off correctly. I am not sure if this is the solution for this.

1
  • You need to align the GPIO voltage levels with what relay is expecting. Most likely the relay wants you to have Open Drain or Open Collector type of pin with a pull-up resistor to the respective VCC. That may very well explain what's going on. In any case it's not a software issue, ask this on Raspberry Pi SO or forum. Note, you may burn your hardware pin with incorrect electical connection.
    – 0andriy
    Commented May 14 at 10:02
0

The relay board that you are using maybe works on reverse logic. It means if the gpio state is high, then the relay will be off and if gpio state is low the relay will be in on state. That's why you are facing this problem.

Relay------Raspberry pi

Vcc ------ 5V as the relay is 5V it needs 5V to work

Input------ GPIO

GND ------ GND

5v relay may work if the trigger voltage of that relay board board is in the range of the raspberry pi gpio output voltage. For that you can check the Datasheet of the optocoupler that's on relay board.

But using 5V relay with raspberry pi gpio directly is not recommended as the gpio work on 3.3V signal using a leave shift or an optocoupler between two is highly recommended.

0

It worked for me when I connect the VCC to 3.3V instead of 5V

2
  • same on my side. But did you face the original problem (that is mentioned in the question) when you connect 5v ?
    – LearneriOS
    Commented Jun 28 at 11:46
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jun 28 at 13:39

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