Control LED with Relay

Aim

To learn how to control a relay using Arduino.

Components required for the experiment

Arduino UNO Board, Breadboard, Relay,  LED,  and Resistor (220 Ohm).

What is Relay and where it is used?

A relay is an electrical device that is used to control the switching of electrical circuits using a low-power signal. It consists of a control circuit and a switching circuit. The control circuit is designed to operate with low-power signals, such as those from a microcontroller, and is responsible for opening and closing the switching circuit. The switching circuit is designed to handle high voltage and current levels and is used to switch the electrical circuits on and off.

Relays are commonly used in automation and control systems to switch electrical circuits on and off. They are used in a variety of applications such as home automation, industrial control, and automotive systems. They are also used to control the operation of large motors and other high-power devices.

Circuit Diagram

To practice, make the circuit diagram with the exact pin-to-pin connection as shown below. This will help you to complete the experiment successfully. Click on the image to enlarge.

Code

const int relay = 3;
void setup()
{
pinMode(relay, OUTPUT);
}

void loop()
{
digitalWrite(relay, HIGH);
delay(1000);
digitalWrite(relay,LOW);
delay(1000);
}

Code Explained

The code functions similarly to the Blink of LED example. However, in addition to blinking the built-in LED, it also activates a relay connected to digital pin 13 every second. When the digitalWrite(LED_BUILTIN, HIGH) command is executed, the relay is activated, thereby closing the circuit of an LED connected to a 9-volt battery.

Now, let's go through the code line by line to understand what each line does:

const int relay = 3;  // the Arduino pin, which connects to the IN pin of relay

This line creates a variable named relay and sets its value to 3. This is the PIN that the relay module data pin is connected to on the Arduino board.

void setup()
{
pinMode(relay, OUTPUT);
}

The setup() function is called once at the beginning of the program. This function sets the pinMode of the relay to OUTPUT so that it can be used to control the LED and Relay.

void loop()
{
digitalWrite(relay, HIGH);
delay(1000);
digitalWrite(relay,LOW);
delay(1000);
}

This section of code is within the loop() function which runs continuously after the setup() function is executed. It begins by setting the output of the digital pin 3 (relay) to high, which turns on the LED. Then there is a delay of 1000 milliseconds (1 second) before the next line of code is executed. Next, the output of the digital pin 3 (relay) is set to low, which turns off the LED. Another delay of 1000 milliseconds (1 second) is introduced before the loop() function starts over again.

Output

  1. Press the start simulation button in the interface.

  2. Depending on the delay specified in the code, the simulation will turn the Relay either ON or OFF.

Conclusion

By incorporating the specified delay in the code, the relay effectively operates according to the desired timing. This functionality enables the control of powerful electric motors and other electrical or electronic equipment that require high voltage and current handling capabilities.

Assignment

Add a push button in the circuit and change the code to turn ON the LED when the push button is pressed.