How to use a 5V Relay with Arduino

6,322 views

Introduction

Interfacing a 5V SPDT relay module with an Arduino UNO microcontroller is an efficient and versatile solution for controlling AC appliances. The relay module acts as an electronic switch that can be controlled by a digital signal generated by an Arduino UNO microcontroller, allowing the user to switch the state of an electrical circuit and power on or off an AC appliance.

This system is particularly useful for applications where remote control or automation of AC appliances is required, such as in home automation, industrial control, and robotics. The system is easy to set up and use, making it a valuable tool for both professionals and hobbyists alike.

Hardware Components

You will require the following hardware for Interfacing Relay with Arduino.

S.noComponentValueQty
1.Arduino UNO1
2.USB Cable Type A to B1
3.Relay1
4.LED Strip1
5.Power Adapter12V-1
6.DC Power Jack1
7.Power Adapter for Arduino9V-1
8.Breadboard1
9.Jumper Wires1

Relay with Arduino UNO

  1. Connect the 5V SPDT relay module to the Arduino Uno as follows:
  • VCC pin to 5V
  • IN1 pin to digital pin 2
  • GND pin to GND
  1. Define the pin number for the relay in the setup function:
int relayPin = 2;
  1. In the setup function, set the relay pin as an output using the pinMode() function:
pinMode(relayPin, OUTPUT);
  1. In the loop function, use the digitalWrite() function to control the state of the relay. For example, to turn on the relay:
digitalWrite(relayPin, HIGH);
  1. To turn off the relay:
digitalWrite(relayPin, LOW);
  1. To post the status of the relay on the serial monitor, use the Serial.println() function:
if (digitalRead(relayPin) == HIGH) {
  Serial.println("Relay is on");
} else {
  Serial.println("Relay is off");
}
  1. Upload the code to your Arduino Uno.

Schematic

Make connections according to the circuit diagram given below.

Wiring / Connections

ArduinoRelay
5VVCC
GNDGND
D3SIG PIN

Installing Arduino IDE

First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.

Code

Now copy the following code and upload it to Arduino IDE Software.

// constants won't change
const int RELAY_PIN = 3;  // the Arduino pin, which connects to the IN pin of relay

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin as an output.
  pinMode(RELAY_PIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(RELAY_PIN, HIGH);
  delay(500);
  digitalWrite(RELAY_PIN, LOW);
  delay(500);
}

Working Explanation

In the setup function, the Arduino’s serial communication is initialized and the relay pin is defined and set as an output pin. This is done to ensure that the relay pin is ready to receive digital signals from the microcontroller. In the loop function, the digitalWrite() function is used to control the state of the relay. This function sends a digital signal to the relay pin, which in turn switches the state of the relay, turning it on or off.

The digitalRead() function is used to read the state of the relay. This function reads the digital signal from the relay pin and stores it in a variable. Then, the Serial.println() function is used to display the status of the relay on the serial monitor. This function displays the state of the relay on the serial monitor, whether it is on or off.

Applications

  • Industrial Control
  • Robotics
  • Automotive
  • Agriculture
  • Smart City
  • Safety and Security
  • Medical
  • Energy Management

Conclusion.

We hope you have found this Arduino Relay Circuit very useful. If you feel any difficulty in making it feel free to ask anything in the comment section.