Arduino relay Code, connection and contol

Spread the love

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

Working of Arduino relay and Code

Relay is a mechanical switch it works on electromagnetic principal. To trigger it between OFF and ON external voltage is required. It has a Electromagnet, when it energise it will attract the plug and trigger it from NO to NC.

Working relay
Working relay

Inside Relay

The electromechanical relay was originally built using some mechanical parts such as an electromagnet, a movable armature, contacts, yoke, and spring / frame / stand, these parts appearing in the internal photos of the relay below. All these are logically arranged to form into a relay.

Arduino relay Code

An electromagnet plays a major role in Riley’s work. It is a metal that does not have a 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 a metal is bent by a copper wire and powered by an adequate power supply, that metal can act as a magnet and attract metals within its range.

Movable Armature:

Movable armor 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.


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


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


Some relays do not require any spring, but if used, it is attached to one end of the armature to ensure its easy and free movement. 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 when 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 provide)EX:- Arduino power relay is ON

How to use a relay with Arduino and relay Code

working Arduino with a relay is very simple. In market 5v relay for Arduino is available. That Relay we can directly use with Arduino. Connect pin no A to Arduino pin no. 9 & B to GND. Follow the following Arduino relay sketch.

Arduino relay Code
arduino 5v relay circuit

you need Arduino controlling relays code. Here we have a test code just Copy it and upload it to your Arduino. Arduino relay Code for testing.

void setup() {
void loop() {
digitalWrite(9, HIGH);
digitaWrite(9, LOW);

If everything is okay you will listen to a click sound from relay. using a relay with Arduino is that simple. But relay is an inductive load which means it will give a reverse voltage which kills your Arduino. We can avoid this using a diode.

Flyback diode for 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.

Relay circuit for Arduino is very simple plug a 1N4148 diode parallel o supply. as show in image.

Controlling relay module using Arduino

5v relay module for Arduino is available. We already publish an article on in click here. How to control a relay using Arduino and button.

Add a Comment

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