Arduino Relay : Circuit, Working, Code, Specification & Its Applications

An electrically operated switch like a relay is used to turn ON/OFF a load by allowing the flow of current throughout it. This relay is simply controlled by low voltage (5V) which is generated by the pins of Arduino So, a relay module controlling with the Arduino board is very simple. Usually, relays are very helpful whenever you want to control an electrical circuit with a low-power signal. There are different kinds of relays used in various applications. This relay module is powered with 5V which is suitable to use with an Arduino. Similarly, there are other types of relay modules available that are powered with 3.3V which are ideal for different microcontrollers like ESP8266, ESP32, etc. This article discusses an overview of an Arduino relay – working with applications.


What is Arduino Relay?

Arduino relay definition is; a relay that is used with a microcontroller like the Arduino to control either high-voltage or low-voltage devices. Actually, a relay is a switch that is operated electrically through an electromagnet. This electromagnet is simply triggered through a low voltage like 5V from a microcontroller & it pulls a relay contact to connect or disconnect a high voltage-based circuit.

Arduino Relay Circuit Diagram

The Arduino-controlled relay circuit is shown below. This circuit explains to you how to control a relay with the help of an Arduino. The required components to build this circuit mainly include the Arduino Board, Resistors – 1K & 10K, BC547 transistor, 6V/12V relay, 1N4007 diode & a 12V fan. Once the button is pushed then the fan will be switched ON and until the same button is again pushed, the fan will stay in the same condition.

Arduino Relay Circuit
Arduino Relay Circuit

Arduino Relay Operation

This circuit works in two cases like turning on/off a load with a relay & a button. Once the button is pushed then the Arduino board will set pin-2 in HIGH condition, which means 5 volts on pin-2 of board. So this voltage is mainly utilized to make the transistor ON. So this transistor will turn ON the relay & the load-like fan will be powered using the main power supply.

Here to power up the transistor as well as the load, you cannot utilize 5V directly from the USB because usually, the USB port delivers 100mA only. So this is not sufficient to activate the relay & the LOAD. So external power supply from 7V to 12V must use to provide power to the controller board, the transistor, and the relay.

Here, the load uses its own power supply. For example, if you utilize a light bulb or fan then you should connect from 110/220V mains otherwise any other power source.

PCBWay

Arduino Relay Code

Arduino relay switch code for turning on a load with a relay & a button

/* sketch
turn on a fan using a relay and a button
*/
int pinButton = 8;
int Relay = 2;
int stateRelay = LOW;
int stateButton;
int previous = LOW;
long time = 0;
long debounce = 500;
void setup() {
pinMode(pinButton, INPUT);
pinMode(Relay, OUTPUT);
}
void loop() {
stateButton = digitalRead(pinButton);
if(stateButton == HIGH && previous == LOW && millis() – time > debounce) {
if(stateRelay == HIGH){
stateRelay = LOW;
} else {
stateRelay = HIGH;
}
time = millis();
}
digitalWrite(Relay, stateRelay);
previous == stateButton;
}

Turn OFF the relay with a delay

You can use the following code example to introduce a delay within the circuit. So, the “stayON” variable is utilized to delay() the program execution within the preferred amount of time. Here, once the button is pushed then the relay will be turned ON & after five seconds the relay will be turned OFF.

Code for turning off a load with a relay & a button.

int pinButton = 8;
int Relay = 2;
int stateRelay = LOW;
int stateButton;
int previous = LOW;
long time = 0;
long debounce = 500;
int stayON = 5000; //stay on for 5000 ms
void setup() {
pinMode(pinButton, INPUT);
pinMode(Relay, OUTPUT);
}
void loop() {
stateButton = digitalRead(pinButton);
if(stateButton == HIGH && previous == LOW && millis() – time > debounce) {
if(stateRelay == HIGH){
digitalWrite(Relay, LOW);
} else {
digitalWrite(Relay, HIGH);
delay(stayON);
digitalWrite(Relay, LOW);
}
time = millis();
}
previous == stateButton;

Arduino Relay Wiring Diagram

The Arduino relay wiring with the DC motor is shown below. The main intention of this wiring is to control a DC motor with the help of a relay and Arduino. The required components of this wiring mainly include; Uno Rev3, Relay Module, Dupont wire, USB cable for powering & programming, Battery, Connector of battery, Screwdriver for connecting wires to the module, and DC motor.

Specifications:

The Arduino relay specifications include the following.

  • It is controllable with digital output.
  • It is compatible with any 5V microcontroller like Arduino.
  • Rated through-current is 10A for NO and 5A for NC.
  • The control signal is TTL level.
  • Maximum switching voltage is 250VAC or 30VDC.
  • The maximum switching current is 10A.
  • Its size is 43mm x 17mm x 17mm.

Arduino Relay Module

These modules are available with additional components & circuitry on a board. These modules are mainly used due to many reasons like the following.

  • These modules are very easy to use.
  • They include the required drive circuitry.
  • Some relay modules come with an LED indicator to indicate the status of the relay.
  • It saves more time for prototypes.

The relay module includes different pins which are discussed below.

Relay Module Pin Diagram
                                                       Relay Module Pin Diagram
  • Pin1 Signal pin (Relay Trigger): This input pin is used to activate the relay.
  • Pin2 (Ground): This is a ground pin.
  • Pin3 (VCC): This input supply pin is used to power the relay coil.
  • Pin4 (Normally Open): This is the relay’s NO (Normally open) terminal.
  • Pin5 (Common): This is the relay’s common terminal.
  • Pin6 (Normally Closed): This is the Normally closed (NC) terminal of the relay.

Step1: Wiring of Arduino board & the relay board

  • Take a dupont cable and one end of this cable to PIN 7 (Digital PWM) of the controller board and connect the remaining end of the cable to the Signal PIN of the relay module.
  • Now we need to make a connection between the 5V pin of Arduino and the relay module’s positive (+) pin.
  • Connect the GND pin of Arduino to the relay module’s negative (-) pin.
  • Now the connections between UNO board & relay module are completed.

Step2: Relay board wiring to the Supply & the load

  • Connect the 9V battery’s positive (+ve) terminal to the Normally Open terminal of the relay module.
  • Connect the Common terminal of the Relay module to the DC motor’s Positive (+ve) terminal.
  • Connect the battery’s negative (-) terminal to the DC motor.

Step 3: Now complete How to utilize a Relay with Arduino wiring diagram.

  • When PIN 7 of the Arduino toggles, the relay switches between both the ON & OFF conditions. The Arduino code for this wiring is given below.
  • For every second, this circuit toggles the relay ON & OFF. In real-time-based applications, this relay can be used to switch ON a light once you detect a motion and also to switch ON the motor once the level of the water is under a fixed range.
Arduino Relay Wiring
Arduino Relay Wiring

Code

#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
}

Now Open Arduino IDE -> Copy & paste the following Arduino code within the Arduino Editor tab. Now Arduino board needs to connect to the PC with the help of the USB cable and program the Arduino board.

What is Relay SPDT Arduino?

SPDT Relay is an electromagnetic switch, used to control the AC devices with a small DC current of an Arduino board.

How Many Relays Can An Arduino Control?

An Arduino board controls up to 20 relays because a relay connected to an Arduino is equivalent to the number of analog pins (6-pins) and digital pins (14-pins) in an Arduino

What is a Relay Module used for?

Relay modules are capable of handling loads up to 10 Amps. These are Ideal for different devices such as passive infrared detectors & other sensors. These modules are used with Arduino & other microcontrollers.

What Does a Relay Do in an Electrical Circuit?

A relay is an electrically operated switch used to open & close electrical circuits by simply getting electrical signals from external sources. Once an electrical signal is received then it transmits to other devices by simply turning ON & OFF the switch.

Thus, this is an overview of an Arduino relay and its working. This module is a very convenient board to use that can be utilized mainly for controlling high voltage and high current loads like solenoid valves, motors, AC loads & lamps. This rely is used to interface with microcontrollers like an Arduino, PIC, etc. Here is a question for you, what is the function of an Arduino Board?