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.setup(relayPin, GPIO.OUT)

## Turn on the Relay (this works - it clicks gives 3.3v)

## Turn off the Relay (this does nothing but goes back to 0v)

## 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, 2016 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? Dec 31, 2016 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, 2016 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, 2016 at 16:45

4 Answers 4


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, 2018 at 22:42
  • Yes, exactly same thing. 3.3V is the solution and with my relay- the same as above in the image, it flips the meaning of HIGH and LOW. Now it switches on with LOW and off with HIGH. Looks little strange, but looking at the voltage differences actually it might be clear. (Somebody else pointed out that relays normally operate in this fashion.)
    – Martin T.
    Nov 17, 2019 at 14:29
  • This worked for me as well. The LEDs on the relay aren't as bright as they were with the 5V.
    – WebMasterP
    Jan 5, 2021 at 21:48

Relays are in general two types

  1. Low Level Trigger (more common)
  2. High Level Trigger.

When a relay is OFF the COM (for common) connects to NC (Normally Connected) When a relay is ON the COM connects to NO (Normally Open)

A Low Level Trigger Relay is turned ON when the input signal is LOW (logic LOW) AND is OFF when input signal is HIGH (logic HIGH)

A High Level Trigger relay is turned ON when the input signal is HIGH (logic HIGH) and OFF when input is LOW (logic LOW)

For a 5V low Level Trigger Relay, that you usually find on the market, the logic low is usually 0V and logic high is 5V. These relays can be completely turned ON/OFF by an Arduino or any microcontroller that can output 0V-5V. Since a RPi can only output 0V-3.3V it is not always enough turn OFF these relays unless you use a level shifter/voltage converter. BUT there are 5V relays with opto-coupler (black thing on the input side) that are turned OFF even when input is 3.3V but i still would suggest a logic level shifter circuit.

So you have two options to have reliable circuit

  1. Use a 3V relay that is more compatible with Rpi.A 3V relay can handle 3.3V that a RPi outputs and should be good enough for prototype testing. But in a finished product you would want a voltage divider/or down level shifter to bring down 3.3V to 3V

  2. Use a 5V relay with up-converter to convert the 3.3V of RPi to 5V. Which option you choose is up to you but the 5V relays (especially multi-channels) are more easier to obtain.

Note 1: Also note down the current required to power on the relay (current to the Vcc of relay). A 5V of raspberry pi can output more current than the 3.3V rail. If you have multiple relays connected to 3.3V or 5V the total current requirement should be within the limits of these rails otherwise you may end up damaging the RPi or not being able to turn on the relays or both.

Note 2: Also note the current rating on the output side the relays can drive. A relay to drive AC loads may require higher output drive current that DC loads. This you can easily see written on the relay itself

Note 3: Usually the logic LOW and logic HIGH voltages can be a bit higher and lower respectively for any digital electronic circuit i.e. logic LOW can be 0V to 0.3V similarly 4.7V - 5V is logic HIGH. The actual values 0.3/4.7 will depend on your chip and you can get it from the datasheet


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, 2019 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 May 20, 2019 at 12:02

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. Jan 4, 2017 at 23:05
  • I have updated the question to show an update. Any idea? Jan 6, 2017 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, 2017 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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