1

I am trying to control a relay using a very simple python code:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(27, GPIO.OUT)
GPIO.output(27, GPIO.HIGH)
time.sleep(1)
GPIO.output(27, GPIO.LOW)

Relay switches on, but seemingly GPIO.output(27, GPIO.LOW) line does nothing. Relay does not switch off. I have tried different pins (17,24,27) but it didn't help.

If I run the code again it shows a warning message: "RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings."

I have found out that if i put GPIO.cleanup() command to the bottom, relay switches off. But I suppose it is not proper way off switching off.

I have tried using GPIO.output(27, 0) command, but it is not working, too.

That is the relay I am trying the control:

https://www.aliexpress.com/item/Integrated-Songle-5V-relay-low-lever-trigger/32622551636.html?spm=a2g10.10010108.1000016.1.2763efb0bhADO&isOrigTitle=true

Thanks for your help!

  • The GPIO output pins are 3.3Volt - I'm surprised your relay turns on at all. Post a picture of your wiring. Similar post here raspberrypi.stackexchange.com/questions/27928/… – CoderMike Oct 9 '17 at 18:27
  • I have seen many tutorials that people operate this exact same relay without additional electronics. Vcc to 5v output, GND to ground, IN to GPIO pin, and should be good. I am complete noob, by the way. – Kalen Hourna Oct 9 '17 at 20:34
  • Just an update: I have disconnected all relay connections, run the code, same behaviour persists. – Kalen Hourna Oct 9 '17 at 20:48
  • Your code tested fine on my Pi3 with an LED connected to GPIO BCM 27. You realise BCM 27 is pin 13 ? pinout.xyz/pinout/pin13_gpio27 – CoderMike Oct 9 '17 at 22:25
  • According to your link the relay will be on when the signal is low. – CoderMike Oct 9 '17 at 22:32
1

I suspect your not using the correct pin. BCM channel 27 is pin number 13.

Pi3 GPIO

I found a relay similar to yours, the relay is on when the output is low, off when the output is high. I swapped your code round :

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(27, GPIO.OUT)

# relay on
GPIO.output(27, GPIO.LOW) 
time.sleep(1)

# relay off
GPIO.output(27, GPIO.HIGH)

Relay

  • Upon further investigation, it seems that GPIO.setup(27, GPIO.OUT) command switches the relay on, GPIO.setup(27, GPIO.IN) switches off. HIGH and LOW has no effect. Except, with HIGH command, green led gets dimmer. With LOW command, it turns back to usual brightness. That one is the exact problem i have: raspberrypi.stackexchange.com/questions/54376/… It is decided that, relay is faulty but I am not sure about that. – Kalen Hourna Oct 9 '17 at 23:44
  • Which model of Raspberry Pi are you using and what is the specification of the power supply? – CoderMike Oct 10 '17 at 7:54
  • Raspberry Pi B+, powered by a standart usb charger. Btw, I have connected Vcc to rpi's 3v3 out, my problem is solved. Everything is fine now. – Kalen Hourna Oct 10 '17 at 19:33
0

I'm working on a project using a similar code and relay setup. My relay was also reversed like CoderMike pointed out. I recommend defining global ON and OFF variables with the high and low values so you can swap it easily in your code. My hardware guy and I went back and forth so many times on how we were wiring and it was really convenient. Improves readability too.

import RPi.GPIO as GPIO
import time

global ON
global OFF
ON = GPIO.LOW
OFF = GPIO.HIGH
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(27, GPIO.OUT)

GPIO.output(27, ON) 
time.sleep(1)

GPIO.output(27, OFF)

Just read your comment. I had this same exact problem.

Upon further investigation, it seems that GPIO.setup(27, GPIO.OUT) command switches the relay on, GPIO.setup(27, GPIO.IN) switches off. HIGH and LOW has no effect. Except, with HIGH command, green led gets dimmer. With LOW command, it turns back to usual brightness. That one is the exact problem i have...

Make sure you you have ALL of the connections on the relay being grounded back to the Pi. Stting the pins to IN just makes them unusable for output. I do not recommend toggling between in and out for simulating output. Let me know if you need help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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