Saturday, October 4, 2014

GPIO Output Relay Interface


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

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.