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

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

About: Part software developer, part maker.

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.


Tools and materials used in this video:

Step 1: What Is a Relay?

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.

Step 2: Mechanical Vs. Solid State Relays

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.

Step 3: The Flyback Diode

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.

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.

Step 4: Electrical Isolation

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.

Step 5: Electrical Connections of Relay Module

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.

Step 6: Arduino Code for Driving the Relay

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.

Step 7: Next Steps

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!

Be the First to Share


    • Jewelry Challenge

      Jewelry Challenge
    • One Board Contest

      One Board Contest
    • New Year, New Skill Student Design Challenge

      New Year, New Skill Student Design Challenge