Controlling 5V relay with Raspberry

Ralay

In todays article, we are going to control a relay with our RaspberryPi. If you have never heard of a relay, it is a simple switch alternating between two circuits that can be controlled by setting its input pin to HIGH or LOW. There are many types of relays, but we will be using an electromagnetic one, which is the most common and the cheapest. It is ususally a small box with three inputs. One for voltage, other for ground and the last one for controlling it. Inside, there is a coil which pruduces an electromagnetic field, when a current is running through it, which moves a wire to the opposite side and switches between the two circuits. Every time the relay changes its state, you can hear a click inside - that is the wire moving there and back!

Relay

Usage

Why would I use a a weird clicking box as a switch if I can create a simple switch with a transistor you might be asking right now. Well, transistors can handle just certain amount of voltage/current going through them and they are not suitable for switching on/off electronic appliances that are connected to a standard 230V/120-130V (depending on where you live) plug you have at home. Ralays on the other side can handle much higher voltage/current so you can use them to switch your lights, water pump or anything else you can possibly think of.

Which relay to buy?

One of the important characteristics of a relay is its working voltage. RaspberryPi can give you 3.3V or 5V. Most of the relays you will find need at least 5V, but some of them can work even with 3.3V so don’t forget to keep this in mind when buying one. They come as a single relays or on a board where you have 2/4/8… of them. Their price starts at around 1$ on Ebay, so you can definitely buy at least two of them to experiment with.

I have bough 5V 4–Channel Relay Module for 2.5$ and this tutorial assumes you have the same relay as I do.

Connecting it to Raspberry

This board has 6 pins - VCC, GND and 4 times IN. As the relay is 5V, you need to connect VCC to Raspberry’s 5V output(pin 2/4) and GND to ground(pins 6/9/14/20/25…). The remaining pins control the four relays on board. At first, I though controlling the relay would be as simple as seting a GPIO on Raspberry to OUTPUT, connecting it to IN pin on the board, setting it to HIGH/LOW and here we go, but… If you try this, you will find out it doesn’t work. This relay board is “LOW ACTIVATED” relay, which means it switches ON if you connect the IN pin to the ground. Go ahead and try it! It then switches back to OFF if it is disconnected fro the ground. But now, how to manage to control it with our GPIO?

One option is to set the GPIO to INPUT mode to switch the relay ON, so the GPIO will basically serve as ground and then set the GPIO back to OUTPUT mode to switch it OFF, but that is not the right way to do it. We will need two resistors (2.2kΩ & 10kΩ) and one NPN transistor to create an additional small control circuit. We will use the transistor as a switch to connect relay’s IN pin to ground or let it disconnected. Connect a GPIO in OUTPUT mode to the transistors base through a 2.2kΩ resistor, then connect relay IN pin to transistors colector and ground transistors emitor. Last thing to do is to ground the GPIO through a 10kΩ resistor. See schematics below to make this description clear. The collector-emitor way of the transistor is closed by default, so the relay will be off. But if you let a small amount of current flow to the base (setting the GPIO in OUTPUT mode to HIGH), the collector-emitor way will open and the IN pin will be grounded, hence the relay will turn ON.

Circuit Circuit schematics

Controlling the relay

Now we have everything set up to finally control the relay. Let’s test it out with the gpio command line tool. Turn on a terminal on your Raspberry and set your GPIO to output mode:

gpio mode 7 OUT

Let’s check if it is relly in OUTPUT mode by writing:

gpio readall

Which will show a schematics of your Raspberry with all pinouts, their number, mode and current value. Now turn the relay ON by setting the GPIO to HIGH:

gpio write 7 1

The realay should turn ON. Horay! Now turn it back OFF by setting the GPIO to LOW:

gpio write 7 0

Voila! Here we go, we have sucesfully build a circuit to control our relay! Now go ahead and controll everyhing at your home!

Do you have any questions or suggestions to make this article better? Don’t hesitate and leave a comment!