Relay Interfacing with Arduino

Our main aim in this tutorial is how to control an AC appliance using Arduino. The major issue is how can we drive an AC load using Arduino. We need some kind of interfacing devices. This can be done by a relay.

What is Relay

It is an electromechanical device. It contains a solenoid coil and some kind of mechanical assembly. When a coil is magnetised by applying its operating voltage it gets magnetised and changes the position of a switch. And the connected device will be ON or OFF depending on the connection.

A circuit that powers the relay is completely isolated from the part that switches the AC devices ON and OFF. It provides electrical isolation.

A relay is used to switch ON and OFF the AC appliances. It also provides an isolation between arduino that is running at 5V and AC appliances running at 220 AC.

It has COM(common), NC(normally connected) and NO(normally open) terminal.

How to Drive a Relay

A simple transistorised circuit is enough to drive the relay. Here NPN transistor BC548 works as a switch. Giving a high signal to its base conduct it and energises the relay coil and connection from COM-NC to COM-NO is done. Due to this a load connected to COM-NO terminal is powered because a circuit is now complete.

To drive this circuit we apply logic HIGH to transistor base using arduino function
digitalWrite(0, LOW);

Hardware Required

  • Arduino Uno
  • Relay 5V
  • NPN Transistor BC548
  • Resistor 1K ohm x2
  • Screw Terminal
  • Breadboard
  • LED

Relay Arduino Wiring Diagram – Without Load

Arduino Relay Wiring Diagram – With Load

While connecting the load make sure that relay is capable of driving the load. For this refer the datasheet, manual or just browse.

Arduino Relay Circuit Diagram – Without Load

Connect a load of your choice to the screw terminal in series.

Relay Arduino Circuit Diagram – With Load

Relay Arduino Code

Program 1: Basic code to ON the relay forever.

We simply need a high signal to the transistor base. For that we use digitalWrite(0, HIGH);

/*5V Relay Connection with Arduino
 Arduino Pin    Relay Circuit 
      0     =>  Transistor(BC548) Base      
*/
void setup() {
 pinMode(0,HIGH);
}

void loop() {  
digitalWrite(0,HIGH); //Relay is Triggered
}

Program 2: Make device ON and OFF at some delay continuously.

We can do ON and OFF the device by using some delay in the programming.

/*5V Relay Connection with Arduino
 Arduino Pin     Relay Circuit
       0     =>  Transistor(BC548) Base      
*/
void setup() {
 pinMode(0,HIGH);
}

void loop() {  
digitalWrite(0,HIGH); //Relay is Triggered(ON)
delay(2000);
digitalWrite(0,LOW); //Relay is OFF
delay(2000);
}

Note: A Relay interfacing involves AC supply. That is dangerous. Make sure you understand the risk and you know the wiring with AC Load. Avoid using this project if you are a novice, beginner in this job.