Working a relay using a Raspberry Pi

The way technology goes in and out of fashion these days is truly amazing. Gadgets are replaced by new gadgets and they in turn are replaced by newer still! With this rapid pace set by modern technology, hobbyists and people who like to delve in electronics find solace in cool black boxes like Raspberry pi and you know what; they simply love it!

Raspberry Pi

Raspberry Pi is a very small (and the term used to describe it is credit card sized) programmable device which can be used for fun, utility and most of all experimentation. Since Raspberry Pi is an easy to use programmable device, it is very popular in gadgetry all over the world. Some of the Raspberry pi utilities are tablet computers, home automation, different kind of arcade games, media streaming etc.

I hope you have a basic understanding of how the pi works. Assuming that you have had hands-on practice on the Raspberry Pi, I’ll jump to relays and time delays. The Raspberry Pi has a 26-pin General Purpose Input/output (GPIO) connector and this carries a set of signals and buses. There are 8 general purpose digital I/O pins – these can be programmed as either digital outputs or inputs. A relay is an electrically operated switch. It has an AC, DC and coil rating. The AC and DC load ratings relate to the switch-contacts, and state the maximum load current that the relay can carry in either case, while the coil rating is what is required to energize the relay to work! It is usually in mW. The following circuit schematic is all you need to energize a relay which you can further use for switching another circuit. It uses a resistor, a transistor and of course the most essential component, the Raspberry Pi.

GPIO pin 17 is used here. To activate the relay the circuit will send a very small current from this pin through the resistor. This current will saturate the BJT (transistor) and current will begin to flow from the voltage rail connect at the top (Vcc = 5Volts).

The diode in this circuit is there to take care of the current that results from the de-energizing of the relay coil. It helps in avoiding a voltage spike and protects the relay. You’ll have to configure the pin 17 of the GPIO. It is just a small piece of code. To drive the relay using the GPIO peripheral’s hardware registers a little bit of time delay coding is necessary. You can use the usleep(1) function call which is a is a C runtime library function built upon system timers to create a short time delay before reading the register to feed back the pin status. This is because the rise time for a GPIO pin is around 100ns to 3V. This could create clocking problem for the Raspberry pi in reading the pin status.