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!

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
  • 7,102
  • 1
  • 11
  • 16
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