Thursday, September 20, 2012

GPIO Output Relay Interface

Note:  See my revised relay circuit here.

To allow the greatest flexibility, my outputs are all relays.  This allows me to switch a variety of voltages and provides protection to the GPIO pins of the Raspberry Pi.  A relay is just a switch that is controlled by an electromagnetic coil.  Powering the coil will make the switch turn on.  The relays I used are made to mount on a circuit board and can be driven by 5V.  They can easily switch 12V or more at a moderate current level.  They are NOT meant to control house current!  That can be the topic of another post.

The Pi GPIO pins will only output 3.3V at a few milliamps.  This is not enough to drive the relay directly, but is is enough to switch a transistor on and off.  A common NPN switching transistor handles that job nicely.  I used the 2N2222 which is highly available (i.e. even Radio Shack carries it.)

Note:  NO SPST is a switch type designation and means Normally Open, Single Pole Single Throw.  This is the simplest type of switch.

A 1K ohm current limiting resistor is attached to the base of the transistor.  Power is connected to the relay coil which is then connected to the collector of the transistor.  The emitter is connected to ground.  When the GPIO pin is low, no current will flow from the collector to the emitter and the relay will be off.  Setting the GPIO pin to high will "turn on" the transistor, power will flow and the relay will turn on.

Notice that there is a diode attached across the coil of the relay in a reversed orientation.  There is magnetic energy stored in the coil while it is energized and holding the relay closed.  When power is removed, the collapsing magnetic field in the coil causes a brief but powerful surge of reverse voltage which can damage the switching transistor and cause premature failure.  The diode is there to stop this reverse voltage and protect the transistor.  I prefer to use relays that have internal surge suppression diodes and they are becoming more common now.  If your relay does not have the diode internally, then it is highly recommended that one be added.


  1. Are their easy extension board to drive a 24V relay via the GPIO pins with the Raspberry Pi boards?

    1. Probably. A quick Google search didn't show any, but I'm sure there are some out there or soon will be.

      The circuit I show says 5V, but that is just because that is what I used. The 5V power can be taken from the GPIO pin. Just be careful not to draw too much current or the voltage will drop and the Pi will hang or reboot.

      You can use the same circuit to control a 24V relay, but you may need to choose another transistor for it to work well. And, obviously, you must supply the 12V to switch the relay. The Pi just needs to switch the transistor.

  2. the max232 is perfect for type circuit!!!!

  3. Unless you Bias the Base to ground, it WILL burn up!

    1. In my application, the base is connected to a GPIO pin with the pull down resistor enabled. So, it is already biased to ground. I should have pointed this out. A more correct drawing would show a 10K ohm resistor connecting the base to ground.

      Many times I have disconnected the GPIO ribbon cable, which DOES leave the base open. It still doesn't burn up. At 5V, the 2N2222 is a pretty robust transistor.

  4. What would be the considerations to relay 127VAC?
    I want to turn on/off the amplifier I use with the RPI.