Arduino Relay Code, Connection and Control

A Relay is an electronic switch like the one we have in our house to on-off the lights or fans. Arduino Relay is an electronically controlled mechanical switch that means it can be controlled by applying a secondary supply. In this article, we are going to structure controlling the relays with Arduino and study everything about the relay. Here we will share the Arduino relay Code, connection, and control.

How does a relay work?

A Relay is a mechanical switch that works on electromagnetic principal. To trigger it OFF or ON, an external voltage is required. It has an electromagnet that attracts the plug when it energizes and triggers it from NO to NC.

The electromechanical relay was originally built using some mechanical parts such as an electromagnet, a movable armature, contacts, yoke, and spring/frame/stand. The parts are appearing in the picture of the relay attached below.

An electromagnet plays a major role in the relay’s work. It is a metal that does not have any magnetic property but can be turned into a magnet with the help of an electrical signal. We know that when the current passes through the conductor, it acquires the properties of the magnet. Therefore, when metal is bent by a copper wire and powered by an adequate power supply, then metal can act as a magnet and attract metals within its range.

Movable Armature:

A movable armature is a simple metal piece balanced on a spindle or a stand. It helps to make or break relationships with the contacts associated with it.

Contacts:

These are the conductors that exist within the device and are connected to the terminals.

Yoke:

It is a small metal piece that is fixed on a core to attract and hold the armature when the coil is activated.

Spring

Some relays do not require any spring. But if it is required, then, it is attached to one end of the armature to ensure its easy and free movement. Also, instead of a spring, a metal stand-like structure can be used.

Relay Pins

It has a total of 5 pins. A, B, COM, NO & NC.

  • A & B is for low voltage supply Ex:- Connection from Arduino.
  • COM is for main supply Ex:- AC supply.
  • NO (Normally open) for output.
  • NC(Normally close) for output.

NO & NC is the state where no supply is connected.

Relay pinWorking
ATerminal for primary supply EX:- 5v
BTerminal for primary supply EX:- GND
COMTerminal for secondary supply Ex:- AC supply
NONormal open (State when no supply is provided) EX:- Arduino power relay is OFF
NCNormal close (State when no supply is provided)EX:- Arduino power relay is ON

How to connect a relay with Arduino

Working on Arduino with a relay is very simple. The 5v relay is easily available in the market. It can be directly used with Arduino. Connect pin no. A to Arduino pin no. 9 & B to GND. Follow the picture of the Arduino relay sketch pasted below.

Now, you need an Arduino controlling relays code. Here, we have a test code ready with us. Copy and upload it to your Arduino, then it is ready for testing.

void setup() {
pinMode(9,OUTPUT);
}
void loop() {
digitalWrite(9, HIGH);
delay(1000);
digitaWrite(9, LOW);
delay(1000);
}

When the code is uploaded finely, you will hear a click sound from the relay This is how the relay can be used with Arduino, simply. But relay is an inductive load which means it will give a reverse voltage that may damage your Arduino. Though, it can be avoided by using a diode.

Flyback Diode for the Relay

Arduino relay Code
relay circuit Arduino

A flyback diode is placed from the power supply with reverse polarity and parallel to the induction coil of the relay. The use of a diode in a relay circuit prevents huge voltage spikes from occurring when the power supply is disconnected.

The relay circuit for Arduino is very simple, plug a 1N4148 diode parallel to the supply as shown in the image.

Controlling Relay Module using Arduino

5v relay module for Arduino is available in one of our articles published previously click here, how to control a relay using Arduino and

3 thoughts on “Arduino Relay Code, Connection and Control”

Leave a Comment