Arduino Relay | Circuit, working code, specification, and applications

Arduino relay
Arduino relay

Overview of Arduino Relay

A relay used with a microcontroller like the Arduino to control either high-voltage or low-voltage devices is known as an Arduino relay. A relay is actually a switch that is electrically activated by an electromagnet. Simply activated by a low voltage, such as 5V, from a microcontroller, this electromagnet pulls a relay contact to connect or detach a high voltage-based circuit.

A load is turned ON/OFF by an electrical switch, such as a relay, by allowing current to flow through it. Simply put, low voltage (5V) generated by the Arduino’s pins controls this relay. Therefore, controlling a relay module using an Arduino board is relatively easy. When using a low-power signal to operate an electrical circuit, relays are typically highly beneficial.

Relays come in a variety of varieties and are utilized in numerous applications. This relay module can be used with an Arduino because it can be powered by 5V. Similar to this, there are several 3.3V-powered relay module kinds that are suitable for ESP8266, ESP32, and other microcontrollers. If you are interested in arduino relay, you can look at the Electronic Part to see more information about it.

Arduino Relay Circuit Diagram

The relay circuit operated by an Arduino is displayed below. This circuit demonstrates how to use an Arduino to control a relay. The primary parts needed to make this circuit are the Arduino Board, 1K and 10K resistors, a BC547 transistor, a 6V/12V relay, a 1N4007 diode, and a 12V fan. The fan will turn ON after the button is pushed, and it will remain in that state until the same button is again pushed.

Arduino Relay circuit diagram
Arduino Relay circuit diagram

Arduino Relay Operation

This circuit functions in two scenarios, such as switching a load on or off using a button or a relay. When the button is pressed, the Arduino board sets pin-2 to HIGH, which signifies that pin-2 of the board is receiving 5 volts. Therefore, the major purpose of this voltage is to turn on the transistor. As a result, this transistor will activate the relay, which will then switch on the main power supply and power the load-like fan.

You cannot use 5V directly from the USB port in this case since the USB port typically only provides 100mA of current. Therefore, this is insufficient to turn on the relay and the LOAD. The controller board, the transistor, and the relay must therefore be powered by an external power supply that ranges in voltage from 7V to 12V.

The load in this instance has its own power source. For instance, you should connect to 110 or 220V mains if you need a light bulb or fan; otherwise, use another power source.

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

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

The concept of using buttons is same as we used in previous blog push button combinational lock using arduino.

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 wiring for the Arduino relay and the DC motor is displayed below. This wiring is primarily intended to control a DC motor using an Arduino and a relay. The major parts needed for this wiring are the Uno Rev3 controller, the relay module, the dupont wire, the USB cable for powering & programming, the battery, the battery connector, the screwdriver for attaching the wires to the module, and the DC motor.

Arduino Relay Module

Relay module pin diagram
Relay module pin diagram

Step 1: Wiring of Arduino board & the relay board

Take a dupont cable, attach one end of it to the controller board’s PIN 7 (Digital PWM), and then attach the other end to the relay module’s Signal PIN. Now we must connect the positive (+) pin of the relay module to the Arduino’s 5V pin. Connect the negative (-) pin of the relay module to the Arduino’s GND pin. The relay module and UNO board are now fully connected.

Step 2: Relay board wiring to the Supply & the load

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

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

The relay toggles between the ON and OFF states when the Arduino’s PIN 7 changes. Below is the Arduino code for this circuit. This circuit switches the relay ON and OFF once every second. This relay can be used in real-time applications to turn on a light when motion is detected and to turn on the motor when the water level drops to a specific level.

Arduino Relay Wiring
Arduino Relay Wiring

FAQ

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?

Leave a Comment

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