Arduino Relay Basics - How It Works and How to Use It

by taste_the_code in Circuits > Arduino

1735 Views, 3 Favorites, 0 Comments

Arduino Relay Basics - How It Works and How to Use It

VID_20211105_070656.00_04_50_18.Still018.jpg
Arduino relay basics - how it works and how to use it (+ giveaway!)

Arduino projects on their own are basically limited to working with low voltages. However, very often in a project, we want to be able to switch AC loads and for that, the simplest device we can use is a relay.

Put it simply, a relay is just an electronically controlled switch. By applying a voltage on the input, we can control a separate and completely isolated circuit that can be of a much higher voltage than the Arduino can withstand.

Hey, I'm Bill from Taste The Code, and today we will look into how you can use relays with Arduino to control AC devices.

Supplies

Tools and materials used in this video:

What Is a Relay?

VID_20211105_070656.00_01_50_20.Still010.jpg
VID_20211105_070656.00_00_42_18.Still003.jpg
VID_20211105_070656.00_00_26_28.Still001.jpg
VID_20211105_070656.00_00_27_26.Still002.jpg
VID_20211105_070656.00_00_56_09.Still005.jpg
VID_20211105_070656.00_01_09_24.Still006.jpg

In its simplest form, a relay is just an electromagnet that we can apply voltage to and that will attract a contact that will then close or open another circuit. Each such relay can either have one or multiple contacts that can then control multiple circuits.

I made a separate video explaining everything about switches so feel free to check it to brush up on your knowledge.

In the image above, is a relay from an old TV that is controlled with 12V and it does have two separate contacts on the output. When a voltage is applied to the input, both of the outputs are switched and we can turn on two separate circuits at once.

These relays come in different shapes and sizes but they also come in different voltage ratings where the most common ones are 5V and 12V.

Mechanical Vs. Solid State Relays

VID_20211105_070656.00_00_43_03.Still004.jpg

Other than the mechanical relays that we saw so far, there is also another type of relay, called Solid State Relay. As the name implies, this type of relay does not have any moving parts and it does the switching through a thyristor.

Since there are no moving parts, these relays can, in theory, last longer than their mechanical counterparts but they come with two caveats. When they fail, they typically fail in a way that they stay "ON" with active load and they are much more sensitive to electrical noise.

So keep this in mind when choosing between both.

The Flyback Diode

VID_20211105_070656.00_01_42_04.Still011.jpg
VID_20211105_070656.00_01_55_07.Still012.jpg
VID_20211105_070656.00_02_11_20.Still013.jpg
VID_20211105_070656.00_02_19_11.Still014.jpg

On a mechanical relay, when we energize its coil, a magnetic field is created on the coil and this magnetic field pulls the contact to close the circuit. When we now disconnect the coil to turn off the relay, that collapsing magnetic field creates a sudden voltage spike that can create arcs across the switch contacts if present or destroy our electronics.

If you want to learn more about this effect Dave from EEVBlog has an awesome video on it that you can check out. https://www.youtube.com/watch?v=hReCPMIcLHg&ab_channel=EEVblog

To protect our electronics from this voltage spike, relays need a so-called flyback diode that is applied across the coil contacts. This diode will provide a path for the voltage spike to be discharged through the coil and will greatly extend the life of our electronics.

With the relay modules that you can purchase online, you can note that many of them have this diode right across the coil. Additionally, these modules also have some extra components that greatly improve the operation of the circuit and can also provide electrical isolation from the relay to the control circuit through the use of optocouplers.

Electrical Isolation

VID_20211105_070656.00_01_13_19.Still007.jpg

Depending on the project requirements, there are times where the relays need to be completely separated from the rest of the circuit and some of the modules allow that through an additional power connector and a jumper across it.

When the jumper is in position, the input power is shared on the control side of the relays and on the relays as well so they are powered from the same source.

When we want to electrically isolate them, we can remove this jumper and power the relays separately through another power supply. This is especially useful in situations where the relays and the control electronics need to be powered from different voltages like 5V and 12V.

Additionally, by using a separate power supply for the relay coils, we can greatly reduce the noise that is generated in the main control circuit, thus improving its operation and stability.

One thing to note here is that you will need a common ground connection between the two power supplies so they are referenced from the same potential. To do this, simply connect the GND connections on the Arduino and the external power supply.

Electrical Connections of Relay Module

VID_20211105_070656.00_04_04_19.Still016.jpg
VID_20211105_070656.00_02_43_21.Still015.jpg
VID_20211105_070656.00_01_18_21.Still008.jpg

When it comes to wiring, to use a relay module with Arduino, we first need to connect VCC on the module to the 5V pin on the Arduino and GND to ground. In this case, we use a relay board that is operated at 5V so we don't have to worry about different voltages.

To control the relay, we need to connect its control pin or pins as in this case, to any of the digital pins on the Arduino.

Depending on the relay module's internal wiring, they can either be driven high or low. Being driven high means that we need to apply logical high or VCC on its input so we can turn the relay on. Contrary to that, if a relay module is driven low, we need to connect its input pin to the ground so we can turn on the relay.

This changes if we are sourcing or sinking current through the Arduino and if you want to learn more about it, check my other video on the topic.

Arduino Code for Driving the Relay

take 1 raw.00_06_40_21.Still025.jpg
VID_20211105_070656.00_04_13_10.Still017.jpg

With the electrical connections done, the code that now drives the relays is extremely simple and it is basically as if we are driving LEDs on a digital pin.

In my case, the relay modules that I'm using are driven low, so I'm specifying the output pin to start and stay high when not in use in the setup function.

This keeps the relay turned off and to turn it on, we need to output a low state on that output by using the digitalWrite function on the Arduino.

If we now add some delays and a statement to turn off the relay, we've now basically recreated the example Blink sketch where the relay is turned off and on for 1 second continuously.

To control the rest of the relays on the board we need to just repeat the same procedure for each and with that, we now have the ability to control any load with the Arduino, no matter what its voltage is.

Downloads

Next Steps

VID_20211105_070656.00_01_33_02.Still009.jpg

Relays are an essential part of many Arduino projects and I hope that you now know a lot more about how you can use them in your projects. 

If you liked this Instructable, I will suggest that you subscribe to my YouTube channel and also check out my other Instructables as well for more projects.

Cheers and thanks for reading!