0

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 https://raspberrypi.stackexchange.com/questions/27928/power-a-5v-relay-from-gpio-pins – CoderMike Oct 09 '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 09 '17 at 20:34
  • Just an update: I have disconnected all relay connections, run the code, same behaviour persists. – Kalen Hourna Oct 09 '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 ? https://pinout.xyz/pinout/pin13_gpio27 – CoderMike Oct 09 '17 at 22:25
  • According to your link the relay will be on when the signal is low. – CoderMike Oct 09 '17 at 22:32

3 Answers3

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

CoderMike
  • 6,982
  • 1
  • 10
  • 15
  • 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: https://raspberrypi.stackexchange.com/questions/54376/even-setting-a-pin-to-output-activates-the-relay It is decided that, relay is faulty but I am not sure about that. – Kalen Hourna Oct 09 '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 07: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 have the same problem with my raspberry b plus v1.2

and this: KF-301 1 relay 5V module low-level trigger relay

it seems that this relay is really low-level trigger. Even the voltage when GPIO.setup(pin, GPIO.OUT) is set seems enough to trigger it.

My multimeter was to inaccurate but it seems that if you have GPIO.IN set you have a negative voltage on the pin and therefore the relay seems to turn of.

GPIO.LOW is still to much voltage.

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.

Alexzoin
  • 11
  • 4