5

Edit for searches: This question is about using a 5v relay with Raspberry Pi GPIO pins that outputs 3.3v.

There are a number of great questions below, read them through to understand whats going on.

If you just need a tldr on what worked for me, these are what I find to work with mine:

  • a 3.3v relay, like the SRD-03VDC-SL-C as suggested by an answer below
  • a 5v solid-state-relay can work, provided the signal can work with 3.3v, e.g. high-state signal starts at 2.5v, such as this. I also find that the relay that comes with an optocoupler could work as well, provided that it has separate pins for the optocoupler vcc and coil vcc, and the optocoupler can be activated with 3.3v, such as this.

I'm trying to control a 5v relay with the GPIO pins.

The wiring are setup this way:

  • Relay's VCC connected to physical pin #4 (5v)
  • Relay's GND connected to physical pin #6 (GND)
  • Relay's Input / signal connected to physical pin #8 (GPIO 14)

For some reason, I found that the relay is activated when the pin is set to output mode, regardless if it is set to high or low, and it can be deactivated by setting it to input mode. Essentially making my "blink" code looks like this:

GPIO.setmode(GPIO.BCM)
while True:
  GPIO.setup(14, GPIO.OUT)
  time.sleep(1)
  GPIO.setup(14, GPIO.IN)

While this code that I used to blink LEDs does nothing (the relay is left in the "on" mode)

GPIO.setmode(GPIO.BCM)
GPIO.setup(14, GPIO.OUT)
while True:
  GPIO.output(14, GPIO.HIGH)
  time.sleep(1)
  GPIO.output(14, GPIO.LOW)

My questions are:

  • Why is this the case?
  • Is it because the "low" state of the output pin still puts out non-zero voltage that is high enough to activate the relay? Do I have to put some resistor in between?
  • Is this the expected behaviour?

Edit: So I've been doing some additional testing, and it appears that the relay also gets triggered when I connect the relay's input pin to one of the RPi's GND pin. From what I understand from reading this thread, I believe this means the relay has a low-level-trigger?

I'm still not sure how this explains the behaviour above though? Why is setting the GPIO pin to output mode makes it gives out the same behaviour as a GND pin?

hndr
  • 153
  • 1
  • 6

4 Answers4

5

The problem

The problem is that your relay board is designed to run at 5V, but your Pi's GPIO is 3.3V. The relay is a 5V relay, so the power supply needs to be 5V, but you can alter the design of the driver board slightly to make it work at 3.3V input. Looking at this image, we can clearly see the transistor is marked 2TY which means it's an S8550 PNP transistor, and the resistors are all marked "102" which means they're 1K ohm resistors. Also, although I don't read Chinese, Google translate helpfully tells me that 开关 near the green LED means "switch" and 电源 near the red LED means "power supply" so we can infer that the red LED will be on whenever this board is powered, and the green LED will probably toggle with the state of the input pin.

5V relay board

From that, it's pretty simple to draw a schematic:

schematic

simulate this circuit – Schematic created using CircuitLab

Note that without R4 (which is what you have), we get a voltage output like this: enter image description here

As you can see, nothing happens until the input voltage rises above 4.1V which is higher than your Pi can manage. The PNP transistor is only forward biased (conducting) when the base voltage is around 0.7V lower than the emitter voltage, or about 4.3V. The Pi's GPIO is only 3.3V.

A bad solution (do NOT do this!)

If you add the 470 ohm resistor R4, and you'll get a voltage plot like this instead: enter image description here

Now the relay's state changes when the output voltage rises above about 2.6V. Problem solved? No! I'm embarrassed to say that was the "solution" I had originally posted, because I was only thinking about properly biasing the PNP transistor, but as @Milliways correctly pointed out, this will likely be fatal to the Pi. Let's analyze why and come up with a real solution.

Pi GPIO

The GPIO pins on the Pi, like most processors, can be thought of as a pair of FETs and a pair of diodes something like this:

schematic

simulate this circuit

The complementary FETs M1 and M2 provide the 0 or 3.3V output and protection diodes D1 and D2 protect against brief excursions on the pin of less than 0V or greater than 3.3V. However, these protection diodes are not terribly robust and are not intended to remain on (forward biased) and conducting for any length of time (think milliseconds). If they are forward biased for a longer period, with a current of, say 1mA or more, the device will overheat and be destroyed, rendering your Pi partially non-functional at best, or completely dead at worst.

If we look at the relay circuit above we see that from +5V through resistor R4 (470 ohms) and R1 (1k ohm) goes straight into the pin. Because 5V > 3.3V the protection diode will be forward biased and will be conducting and heating the part. The current would be about (5 - 3.3)/(470 + 1000) = 1.2mA which is higher than would be safe. The other suggestion originally posted was replace R1 with a 2.2K part and apply R4 with 680 ohms. That would result in (5 - 3.3)/(680 + 2200) = 0.6mA which might be low enough to be safe, but it's not the best possible design.

A real solution

There are a number of safer alternatives. The simplest would be to simply replace the relay, which is an SRD-05VDC-SL-C with a drop-in replacement SRD-03VDC-SL-C (see http://www.mouser.com/ds/2/321/27115-Single-Relay-Board-Datasheet-709206.pdf for datasheet). Then use 3.3V supply as Vcc for the relay board.

Another is to use a different transistor. Recall that using a PNP transistor means that to turn the transistor on, we need to have a VBE (voltage from base to emitter) of around -0.7V. With an NPN transistor, it's the same but with the polarity reversed, so around +0.7V. This brings the voltage well within the 3.3V range of the Pi and could be done like this:

schematic

simulate this circuit

About current

With this circuit, GPIO pin current will be about 2mA. If I'm not mistaken, the GPIO pins are set by default to source 2mA, so no change is required using the latter circuit. The Pi's GPIO pins can source up to 16mA, but they should not be asked to source more current than they're programmed for to avoid damage.

Also, the entire relay circuit will draw about 80mA from the 5V power supply. The onboard 5V supply should be more than adequate for this.

Acknowledgement

First, apologies for my original answer which was not good. Second, thanks to @Milliways for pointing that out. I'm hoping that by showing both the bad solution, analyzing it, and showing real solutions, that others may learn from my error.

Edward
  • 961
  • 1
  • 6
  • 25
2

I'm still not sure how this explains the behaviour above though?

Because the Pi GPIOs only output 3.3V, and presumably this is not enough to trigger high on the 5V relay control circuit.

The that reason that using it set to input works is the relay input is probably pulled high so that it's default state is off. An input GPIO is in a high impedance state (aka high-Z), which is different from an output driven high or driven low; hence the GPIOs really operate in terms of three state logic. The high-Z state is much the same as a piece of wire that is not connected to anything, including ground. It has very high impedance and will take on the voltage of anything with less impedance attached to it -- meaning it is probably not a good idea for you to do this, since you are effectively driving the Pi input high with 5V, over the 3.3V tolerance of the pin.

You can double check the state of the input to confirm this (if you want to keep gambling...).

Milliways
  • 62,573
  • 32
  • 113
  • 225
goldilocks
  • 60,325
  • 17
  • 117
  • 234
1

Does your relay board have the circled component on it?

Relay board with transistor coil control

If so, then you'll need to find the datasheet for the board. The transistor can be setup to activate the coil on a low input... i.e. Ground the input terminal. Or it can be setup to activate the coil on a high input... i.e. 5V applied to the input. If you can't find more information online about that particular board, try asking your local shop for more information. Most electronics shop people will talk your ears off if you ask a question or two... I know, because I was one once.


If you are looking at the guide linked to by @RichardChambers then the relay board has the transistor and the diode (the orange component with the clear packaging) listed in the relay control circuit. However, it's important to note that the transistor could be setup to follow reverse logic. The type of transistor will determine how the input functions. An NPN BJT configured as in the above guide, will follow normal logic. However, some other transistor might be used to change the input voltage sensitivity or perhaps the logic applied.

RubberStamp
  • 1,449
  • 1
  • 12
  • 16
1

I believe your relay board works like mine so the relay is on when the output is low, the relay is off when the output is high.

Change your code accordingly:

GPIO.setmode(GPIO.BCM)
GPIO.setup(14, GPIO.OUT)
while True:

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

  # relay off
  GPIO.output(14, GPIO.HIGH)
  time.sleep(1)

Similar post: can't set gpio to low, trying to control single channel relay (raspberry pi b+)

For reference the spec on my relay module states:

IN1 - Controls relay 1, active Low! Relay will turn on when this input goes below about 2.0V

http://modtronix.com/mod-rly2-5v.html

CoderMike
  • 7,102
  • 1
  • 11
  • 16