4

This seems like it should be really simple, so maybe I am missing something. I am just trying to open and close my relay but all it does it open's and doesnt close. I can use GPIO.cleanup() to make it close but I dont want to apply this at this point in the project.

Link to 5v relay -> https://www.amazon.co.uk/XCSOURCE-Channel-optocoupler-Arduino-TE213/dp/B00ZR3B252

UPDATE: 6th Jan 17 As suggested in the comments I have tested the GPIO's and can see that 3.3V comes out when HIGH and it goes back to 0v on LOW. But LOW does not turn off the relay oddly. I have tried 2 of these relays and both do not switch off when at LOW (0v).

I have tried to find the original documentation for GPIO.clean() function since this allows it to close but cant find it. Can anyone think why this does this?

enter image description here

import RPi.GPIO as GPIO
from time import sleep

relayPin = 32

GPIO.setmode(GPIO.BOARD)
GPIO.setup(relayPin, GPIO.OUT)

## Turn on the Relay (this works - it clicks gives 3.3v)
GPIO.output(relayPin,GPIO.HIGH)
sleep(1)

## Turn off the Relay (this does nothing but goes back to 0v)
GPIO.output(relayPin,GPIO.LOW)
sleep(1)

## if I add GPIO.cleanup(), the relay then closes, 
## but I dont want to cleanup at this point
  • 1
    Basically when you switch the GPIO to OUTPUT mode the relay switches on and when you switch the GPIO to INPUT mode the relay switches off. I expect the relay needs 5V to work properly, not the Pi's 3V3. You may be slowly destroying the Pi by directly connecting a GPIO to the relay. – joan Dec 31 '16 at 12:26
  • Its not connected to 3.3v though? I am using the 5v from the RP (see diagram above). Bit confused about what you mean about input output. Are you saying it should be GPIO.input(relayPin,GPIO.HIGH) to turn it off? – Oli Girling Dec 31 '16 at 12:35
  • You are connecting a GPIO to the relay in an attempt to control the relay logic circuitry. It is only safe to connect 3V3 to a Pi GPIO. Because of the way your relay operates you may be connecting 5V to the Pi and may be damaging the GPIO and ultimately the Pi. The GPIO.setup(relayPin, GPIO.OUT) sets the GPIO as an OUTPUT. When an OUTPUT a GPIO is actively driven to 3V3 or 0V. The GPIO.cleanup() changes the GPIO to INPUT mode. – joan Dec 31 '16 at 14:48
  • @joan The part Oli links to requires 2v to switch, so that should be fine. I don't understand your concern about damaging the GPIO - could you elaborate please? The IN pin on the relay module is an input, not an output. – Mark Smith Dec 31 '16 at 16:45
6

I had the same problem as you, but I finally solved it.

As you are powering it with 5V, both 0V or 3.3V coming from the GPIO pin are considered as "low level", so it won't actually switch.

You need to power it with 3.3V (it seems it was designed to work also with lower voltages), this way it can correctly distinguish between 0V (low level) and 3.3V (high level).

  • This was also the issue with my relay and saved it from the trash. TONGLING brand purchased off Amazon in a sensor kit. – Spechal Dec 2 '18 at 22:42
1

You can't use 5 V relay with signal from Raspberry Pi while Raspberry Pi signal is 3.3 V. So if you used Logic level converter to convert 3.3 V from Raspberry to 5 V then to relay in I think it may solve this issue.

I don't know why most distributors say it should work with raspberry pi while the ideal one for Raspberry Pi is 3.3 V relay.

So you have 2 choices either:

  1. 3.3 V relay, or;
  2. Logic level converter with a 5 V relay.
  • 1
    Here the problem seems the low level, 0V, not 3.3V/5V – RalfFriedl May 19 at 7:36
  • So, Are you sure you using HIGH Level relay not low level? I think your implementation is low level relay, so it closes when applying 5v not 0v (vice versa) then you must use logic converter like adafruit one. And while your high 3v is not high enough to CLOSESO,your problem occurs – Sherif Sameh May 20 at 12:02
0

The specs for this relay state that the trigger is "● IN: relay module trigger pin (high level trigger) ● Trigger voltage:2-5V", which means that it should turn ON with a GPIO.HIGH command (+3.3V) and turn OFF with a GPIO.LOW command (0V or ground).

Can you check the voltage output of the PRi's GPIO pin when it is set to HIGH to make sure you are getting the full +3.3V? It's possible that you are not reaching the trigger threshold.

  • I will check this when I am back in the office on Friday. To ensure it wasnt a damaged pin, I did check 3 GPIO's all with the same result, so I am not sure its a broken pin. I will also try another relay. – Oli Girling Jan 4 '17 at 23:05
  • I have updated the question to show an update. Any idea? – Oli Girling Jan 6 '17 at 19:27
  • The GPIO.clean() command returns the GPIO pin to an input state, which it is floating. It would be the same as disconnecting the "IN" wire on the relay. Just as an experiment, you could switch the GPIO from an output to an input to see if that turns off the relay. You are correct that your code should work, but it's possible that relay is not working as expected. – GCass Jan 6 '17 at 22:00

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.