Control a relay using the Arduino

Most of us are familiar with the switches used in household devices. A relay is a type of switch that can be connected to an Arduino or any other microcontroller.


In this tutorial, we will use the Arduino to control a relay on and off every three seconds. You can use the same code to control a fan to be turned on and off automatically every 10 minutes to save power!


Overview


Relays are used in a variety of applications, such as controlling a light bulb or fan with your Arduino.


The relay's most essential function is that it allows you to turn on and off devices that operate at 220 volts using the Arduino, which operates at 5 volts.


When the relay is paired with a Wi-Fi or Bluetooth module, it allows you to control home appliances remotely, making it useful in smart home applications.


Getting the items


For this project you will need the following components, you can buy them from our store.

Sale Off
Voltaat Arduino Uno R3 (Voltaat Version)
45 QAR
Sale Off
Voltaat 1 Channel Relay Module
13 QAR
Sale Off
Voltaat Jumper Wires - Male to Female (40 Pack)
10 QAR

Wiring it up


To set up the relay in the correct way, follow the instructions below. The image demonstrates how to connect the wires between the relay and the Arduino. Once the relay and the Arduino are connected to each other, connect the Arduino to your computer using the USB cable.


Connections from the relay to the Arduino:

• Relay signal pin → Arduino pin 3

• Relay VCC pin (+ pin) → Arduino VCC pin (5V)

• Relay GND pin (- pin) → Arduino GND pin


Coding


The function of this sketch is to control the relay. We programmed the relay to deliver current every three seconds. This code may be used in a variety of applications, such as turning a fan on when the temperature rises above a certain threshold and off when it drops again.


Let’s test out the code together!


/*
Voltaat learn (https://www.voltaat.com)
Link for full tutorial: https://bit.ly/3SlR4TD

Tutorial: Control a relay using the Arduino

The function of this sketch is to control the relay.
We programmed the relay to deliver current every three seconds.

Connections from the relay to the Arduino:
• Relay signal pin → Arduino pin 3
• Relay VCC pin (+ pin) → Arduino 5V pin (5V)
• Relay GND pin (- pin) → Arduino GND pin

*/



//Define the variable Relay to Digital Input Pin 3
const int Relay =3;

//Commands inside void setup run once
void setup(){
//Start the serial monitor at 9600 baud rate (9600 bits per second)
Serial.begin(9600);
//Relay is defined as an output
pinMode(Relay,OUTPUT);

}

//Commands inside void loop run forever
void loop(){
//Turn the relay on
digitalWrite(Relay,HIGH);
//Print to serial monitor
Serial.println("The relay is turned on");
//Delay for 3 seconds
delay(3000);
//Turn the relay off
digitalWrite(Relay,LOW);
//Print to serial monitor
Serial.println("The relay is turned off");
//Delay for 3 seconds
delay(3000);
}

Testing it out




Now you must have correctly wired the relay to the Arduino as we explained in the wiring section, as well as uploaded the code to your Arduino board.


You may now access the serial monitor on your Arduino IDE by clicking on the magnifying glass icon at the top right corner.



The serial monitor is a great tool that can facilitate communication between the computer and the Arduino. It can allow us to send and receive different commands as well as view information directly from the Arduino.


As we can see in the following image, the serial monitor displays the relay state. You can also hear the sound of the relay switching on and off every three seconds.


You should also make sure you have chosen the right baud rate (9600) as specified in the code.


Resources 


Arduino Code

Fritzing Wiring file

Related Tutorials


A transistor is a device that can be used as a switch. It is made from a special material and has at least three terminals for easy connection to an external circuit.


A soil moisture sensor is a device that measures the amount of water in the soil. The sensor consists of two metal probes that can be inserted into the soil to measure the moisture levels.


Have you ever wondered how your TV or air conditioner remote works? This is exactly what we will discover in this tutorial.