Raspberry Pi GPIO: Driving a Relay using a Transistor
A transistor usually drives a relay by energising its coils, and then the relay contacts switch higher voltages and currents, which the transistor cannot manage directly. When you have a program that makes decisions, eventually it comes down to action, and this is where relays are most effective. This is where the computer world of ones and zeros meets the physical world of humans, and there is an almost human quality when a machine acts on logical decisions it has made.
In this type of circuit, the purpose of the transistor is to behave as a switch to energise the relay coil; therefore, it is a driver, or a load-switching transistor. Usually a general-purpose bipolar junction transistor (BJT) works fine for small relay loads, however, for larger relays a two-transistor Darlington circuit provides better performance. A MOSFET also provides better switching action, however a BJT is much cheaper.
Interfacing a Relay
A transistor usually receives a signal to the base junction from a CMOS or TTL system. The Raspberry Pi will usually provide 3.3 V to represent logic 1 output, whilst a TTL microcontroller will provide a 5 V output. In either case, the relay driver circuit is the same but the base junction resistor will be a different value, therefore, it is important to be able to calculate this value so that the transistor saturates sufficiently to operate the relay.
Relay
A relay is an electromechanical component, which controls the operation of one or more electrical contacts, through the magnetic force generated by its coil. At a very basic level, the control of a small amount of current through its coil, allows engineers to switch larger currents through its switch contacts.
Relay Diode
A relay coil is essentially an inductor, and when a current passes through it, it forms a magnetic field thus storing energy. However, the removal of the current causes the field to collapse and the stored energy presents itself as a back EMF. Therefore, a protection diode such as 1N4148 or 1N4001 bypasses this current to protect the transistor.
Typical Relay Resistance and Voltages
Voltage (V) | 5 | 9 | 12 | 24 | 48 |
Current (mA) | 72 | 40 | 30 | 15 | 10 |
Coil Resistance (Ω) | 69.4 | 225 | 400 | 1600 | 4800 |
Must Operate Voltage | 75 % Max | ||||
Tolerance | ±10 % at 23 °C |
These relays are typically available in 5 V, 9 V, 12 V, 24 V, and 48 V, coil versions. To energise the relay coil it must have sufficient "pull-in" current passing through it, therefore, the voltage must be precise. The table above shows the coil resistance and the current requirement for each relay. Note that the values of voltage, current, and resistance follow Ohm’s Law.
In this example I will be using the 5 V version of the G5LA relay because it is the cheapest and there seems to be plenty of documentation regarding its specifications. This relay also works with the Raspberry Pi, with a driver transistor.
Transistor Driver
The PN2222, PN2222A, and P2N2222A are general-purpose NPN transistors that could drive small relay loads less than 500 mA. For those manufactured by the On Semiconductor Corporation, their absolute maximum current carrying capacity Ic is rated at 600 mA. These transistors come in a TO-92 package and look the same, however note that the P2N2222 has a different pinout compared to the other two.
The Raspberry PI GPIO pin is capable of providing up to 16 mA of drive current, though in this circuit I plan to draw no more than 4 mA. In actuality, a very small amount of current in the order of micro-amps is required to saturate the base junction of a transistor such as the PN2222A. In this circuit, the 5 V and ground are provided by the Raspberry Pi, and can be taken from the GPIO header socket.
Calculating the Base Resistance
Assuming that the output drive current from the GPIO pin is a conservative 4 mA, the relay coil is a 5 V type with a resistance of 69.4 Ω, a suitable transistor is one that has an Ic rating greater than 72 mA, and hFE greater than 90. Therefore, choosing the commonly available PN2222A and selecting a typical hFE value of 100, a base resistance of 1388 Ω properly saturates the transistor. Hence, a standard resistor value of 1.2 kΩ works fine.
If you are using a different relay with a different coil resistance, then you should use the calculator in the following section of this article to help select the correct base resistance. To sufficiently saturate general-purpose transistors with an hFE of 100, usually a 1 kΩ base resistor works well in most cases.
Finding the Components
Open another tab and search for the following on Google. I recommend getting the cheapest.
- eBay
- "25x 2N2222 P2N2222A NPN"
- "eBay Omron 5 V relay"
This Article Continues...
Raspberry Pi GPIO: Driving a Relay using a TransistorDriving Relays with CMOS and TTL Outputs Calculator