How To Use A Relay With Arduino

How To Use A Relay With Arduino

In this tutorial, I will show you how to use a relay with Arduino and how relays work.

By the end of this tutorial, you will be confident in working with relays and interfacing relays with Arduino.

This will help you to work on Arduino controlled Motor, power switches and other high power applications. This article will cover projects involving 5 V, 9 V, and 12 V relay connections to Arduino.

The first part of the article covers the basics of relay and relay board connections. I will then continue with the Arduino project where you will drive a relay using Arduino to control a DC circuit.

At the end of the article, you will find all the details to control an AC circuit ( 230 V lamp, dimmer etc) using Arduino.

Let’s get started!

1: Components Needed To Control Relay Using Arduino

Hardware Components

Tools

Software

You will be able to complete the project with the items listed above. I suggest you first go through the entire article to understand the connections, safety tips and some more useful hints.

You will program the Arduino to control the relay board. The relay board will then turn the load ON or OFF.

-> Learn more about How Easy Is It To Learn Arduino here.

Relay Basics

Relays are helpful when you need to control a circuit using a low power signal (Arduino Digital Pin, for example).

There are different types of relays for multiple applications. The mechanical relay will consist of a coil which when activated, closes or opens the switch (due to magnetic properties of the coil).

The Solid-state Relays(SSR) are less bulky and consist of no moving parts.

Here is one example of a 5 V relay.

Example data sheet – OMIH-SH-112L,394

Example data sheet - OMIH-SH-112L,394

I have taken the image from the above datasheet. If you are planning for a DC load, the maximum current the relay can support is 16 A. This is how I read the relay.

Depending on the type of the load, you have to also derate it (choose a much higher current rated relay than load current)

I will brief you on the most important parameters you should look at, before buying the relay for your application.

  1. Switching Voltage – Each relay will have a rating separately for AC and DC. You have to make sure that the load you need to drive requires a voltage less than the relay switching voltage. 

Example: If I want to drive a 9 V DC motor using the relay, I will go with a relay that has the switching voltage rated to 12 V or higher

  1. Contact current rating – This rating should be higher than the maximum expected load current.
  1. Coil voltage – This is the voltage needed for the relay to turn on. You cannot provide voltage more than 5 V using Arduino. Hence you can use a circuit similar to below to provide the 12 V needed by the relay.

The transistor acts like a switch. You can use an Arduino digital pin to control the transistor. You will need this circuit if you want to drive the relay and not a relay board.

At the end of the article, you will see the benefits of using relays and relay modules.

When you send a Logic 1 on the digital output pin of the Arduino, the transistor will be ON. When the transistor is ON, the relay will get the required coil voltage (VCC) and it will be ON.

To turn OFF the relay, you have to send a Logic 0 to the transistor.

Once the transistor is off, the relay coil voltage drops to zero. The load will be now disconnected.

You have to use a diode in parallel with the relay coil. This is for protecting both the transistor and Arduino as well.

When the relay is ON, The coil in the relay will have energy stored in the form of a magnetic field.

Once the relay is turned OFF, the magnetic field energy will be dissipated in the form of high voltage.

The Flywheel diode will act as a clamp and also provide the path for the coil to discharge the energy stored safely.

For different types of relay drive circuits, I recommend you to go through the relay switch circuit tutorials.

2: Step By Step Instructions

I will highlight the important pin details and the connections first. The following steps will take you through a step by step connection guide to complete this project.

step by step connection guide

Pinout information

Arduino UNORelay Board
5 V(+) symbol
GND(-) symbol
PIN 7S pin (input pin)

You will find the pin description on the load side of the relay module in the table below.

Relay Board PinDescription
NONormally Open – This pin gets connected to the COM pin when the relay is OFF. When the relay is ON, this pin gets connected to the COM pin.
COMCommon
NCNormally Closed – This pin gets connected to the COM pin when the relay is OFF. When the relay is ON, this gets disconnected from the COM pin

Step 1: Wiring Arduino and the relay board

In this step, I will show you the wiring needed between Arduino and the relay.

1) Take a jumper (dupont cable) and connect one end to PIN 7 of Arduino

Take a jumper (dupont cable) and connect one end to PIN 7 of Arduino

2) Connect the other end of the jumper to the S PIN on the relay module. The connection will look like the image below

Connect the other end of the jumper to the S PIN on the relay module

3) Make a connection between Arduino 5 V pin and the (+) PIN on the relay module

Make a connection between Arduino 5 V pin and the (+) PIN on the relay module

4) Make a connection between the Arduino GND pin and the (-) PIN on the relay module

You will find three GND pins on the Arduino UNO. You can connect to the GND PIN which is more easily accessible.

Now, You have completed the connections between Arduino UNO and the Relay module.

In the next steps, we will connect the relay module to the load.

Make a connection between the Arduino GND pin and the (-) PIN on the relay module

Step 2: Wiring the Relay board to the Supply and the load

In this step, I will show you how to connect a 9 V supply and the load to the relay module. I have taken the DC motor as an example for the demo.

1) Connect the positive terminal of the 9 V battery to the NO PIN of the relay module

Connect the positive terminal of the 9 V battery to the NO PIN of the relay module

The switching part of the relay is electrically isolated from the driver side (where you have made a connection from Arduino).

This isolation helps to protect Arduino from high voltage supply and loads connected to the relay.

2) Connect the C (common) PIN of the Relay module to the Positive terminal of the DC motor

Connect the C (common) PIN of the Relay module to the Positive terminal of the DC motor

You will be able to identify the positive and negative terminal on the DC motor by the color of the wires.

The Red wire is the positive terminal and the black wire is the negative terminal of the DC motor.

3) Connect the negative terminal of the battery to the DC motor

Connect the negative terminal of the battery to the DC motor

Step 3: Complete wiring diagram for How to use a Relay with Arduino

Complete wiring diagram for How to use a Relay with Arduino

You can use the connection diagram to verify your connections once more! You may notice that nothing will happen.

You have to now tell Arduino to toggle the PIN 7 so that the relay switches between ON and OFF states.

You will find the code for the Arduino in the next step!

The same steps apply to both a 12 V supply and a 9 V supply! Always make sure that the relay rating is higher than the supply voltage. 

When you are working with AC mains, you have to take special care. Always make connections with AC mains disconnected.

High voltage is very dangerous and safety always comes first.

You should always insulate the open ends of the cable and provide a cover to protect others from accidentally touching the relay section.

Step 4: Arduino Sketch for How to use a Relay with Arduino

The sketch toggles the relay ON and OFF every one second. This is a simple sketch only for the demo.

In real applications, you can drive the relay to turn on light when you detect a motion or turn on the motor when the water level is below a certain threshold!

The relays find their applications all around you!

  1. Open Arduino IDE
  2. Copy and paste the below code in the Arduino Editor tab
  3. Connect Arduino to the PC using the USB cable
  4. Program the Arduino
#define RELAY_PIN 7

void setup() {
  // initialize digital pin RELAY_PIN as an output.
  pinMode(RELAY_PIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(RELAY_PIN, HIGH);   // turn the RELAY on 
  delay(1000);                     // wait for a second
  digitalWrite(RELAY_PIN, LOW);    // turn the RELAY off
  delay(1000);                     // wait for a second
}

-> Read our guide about What You Can Build with Adruino.

3: Relay Versus Relay Board? Which One Should You Choose?

Relay Versus Relay Board

Relay modules come with additional circuitry and components on a board mounted. I would go with the Relay module for quick prototypes for the following reasons.

  • Comes with easy to use connectors
  • Contains all the necessary drive circuitry
  • Some modules will have an LED indication to tell the relay status
  • Easy to mount on a fixture (mounting holes will be there)
  • Saves time for prototype

I will also tell you why I would go for a single relay.

  • Saves a lot of space 
  • Not all relays you need comes in a module
  • I can design my own drive circuitry and be sure about it

Hence, both connecting Arduino to Relay as well as Relay boards bring you unique advantages!

> Check out our guide to the Top 12 Best Arduino Online Courses

Conclusion

In this article, I covered the basics of a relay and important parameters of a relay to look for.

I also showed how to complete the connection between Arduino and relay module with a DC motor load as an example. 

I have worked with several relays and have good hands-on experience working with the relays. I will be excited to see what new applications you will find for your next project which uses a relay!

I would love to hear from you. Did you find this article useful? Have you got some suggestions?

Please reply in the comments and I will be glad to reply to all your comments. 

Now it is your turn to suggest a topic which you would like to learn about next!

Share the article with your friends too!