How to use Relays with Arduino

arduino relay

How to control AC appliances with Microcontrollers like Arduino has always been a questions for beginners. As you may know Arduino works on 5V logic level i.e it can only control devices that operate on 5V. But what if you want to control devices/appliances that work on more than 5V like 24V or even 220V AC?

In this article we will discuss about relays. How they work and how to use them to control high voltage devices. But first, if you are absolute beginner then I recommend you to check out “Introduction to Arduino” to get familiar with Arduino.

Once you are familiar with that, we can now get started. Let’s first take a look at what a Relay is.

What is a Relay?

A relay is an electo-mechanical switch which is used to control high power device using low power input. So using Relay we can control high power appliances like Light bulbs or Motors using Microcontrollers like Arduino and ESP.

How a Relay works?

The operation is quite simple, There is a coil which acts as an electromagnet. When powered this coil attracts a metal contact which toggles the switch position.

There are usually three contacts on a SPDT relay, normally open(NO), normally closed(NC) and common(COM)

Relay symbol
relay circuit diagram
Internal relay diagram

When the coil is powered on it attracts the contact which closes the Normally Open pin. A connection is made and the Appliance is turned on.
This allows one circuit (Lower powered) to switch on another circuit (Higher powered).

Using Relay Modules.

We can’t use a relay directly with arduino. It needs a driver circuit for better working and protection of the microcontroller. This circuit usually consists of a reverse protection diode, transistor a couple of resistors and an opto-coupler for isolation.

We can build this circuit on a breadboard or Perf board but there are Relay modules available for cheap which are better made and easy to use. These modules can be in 1ch, 2ch, 4ch all the way up to 16ch boards. Here ch means Channel or number of relays on the board.


Here is a list of all the materials we will need to for controlling an AC appliance using Arduino.

After gathering these components we can move on and start making connections.


For this test I have used a 4ch relay module. But you can use any number of relays. All relays have similar pinouts: VCC, GND and Inputs.

4ch relay module pinouts
Relay Module Pinouts.

As labeled in the image above, first there is GND. Which connects to the Arduino or any microcontroller. This is very important I have seen people not connecting the grounds in a circuit and then ask me why the circuit does not work.

The next 4 pins (IN1, IN2, IN3, IN4) are the input pins which will be connected to Digital pins of Arduino. These pins work at 5V logic level so if you are using a 3.3V microcontroller like ESP8266, you will need a logic converter.

Last pin is VCC, this is the +5V pin that powers the board. On a multiple channel relay you will always see another set of VCC pins usually to the right. These are the pins used to power the relays. I recommend powering it with a separate 5V supply if you have a 6ch or higher board.

Now let’s make the connections:

Arduino nano and relay board

In the above diagram, the relay module is connected to an Arduino Nano V3.0. The connections are pretty simple here:

  • VCC >> +5V (The relays are powered by arduino directly but if you have a module with more than 4ch, use a separate power supply)
  • GND >> GND (If using external power supply, connect its GND to Arduino to make a common ground.)
  • IN1 >> D2
  • IN2 >> D3
  • IN3 >> D4
  • IN4 >> D5

That’s all the connections, now we can write a simple code to to toggle on the relays.


For testing this setup we don’t need a complex code. Below is a simple code that will test the relay modules and make sure our setup works.

#define IN1 D2
#define IN2 D3
#define IN3 D4
#define IN4 D5

Void setup()
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);

  digitalWrite(IN1, LOW);
  digitalWrite(IN1, LOW);
  digitalWrite(IN1, LOW);
  digitalWrite(IN1, LOW);

void loop()
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, HIGH);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);

What this code will do is turn on each relay one by one then stay on for 2 seconds and then turn off the relays one by one again. This is just to test the setup.

Final Note:

This post was just an introduction to relays and how they can be controlled using an arduino. In future projects we will use this knowledge to make awesome projects like home automation and more.

If you have any questions or doubts feel free to ask in the comment section.
Hope this post was informative and you learnt something new. If you did, make sure to subscribe to stay updated with latest tutorials and posts.

Leave a Reply

Your email address will not be published. Required fields are marked *