Relay interfacing with Arduino

In this project we will interface relay to Arduino board. The relay is very useful element when it comes to operate switching A.C.(alternating current) or high power DC devices with micro controller. Relay are of many types and are available in wide range of actuating voltage i.e. 5V, 9V, 12V and so on.

Though arduino is capable to give 5V at its pins but ability to supply current is upto 40-50mA only and most of relay coil require current from 70-100mA. So if we directly connect relay to Arduino then micro controller may get damaged permanently. So to avoid harmful operation we connect a transistor (acting as switch) mostly NPN in common emitter configuration.

The circuit schematics are as bellow:-

Relay interfacing with push button control.

In above shown circuit we have used 5 volt relay which has resistance of 70 ohm. Therefore current drawn by relay can be given by ohms law as:-

I=5v/70ohm =71.42mA of current will be drawn by relay. Therefore we use BC547 NPN transistor which has ability to handle about of 100mA of collector current (obtained from datasheet).

Now because there is inductive coil in relay it has ability to store energy. So as to freewheel this energy freewheeling diode[F.D.] is used.

The LED is connected for observing operation of relay. LED is connected between common and NO(Normally Open) contact of relay supplied by 12V. Current limiting resistor (of atleast 1K) must be connected in series with LED to avoid harmful operation.

Relay coil is connected in collector terminal of transistor. The emitter terminal is connected to ground of Arduino and base terminal of transistor is connected to pin 13 of Arduino board through an 1K current limiting resistor.

At pin 7 push button with pull down resistor is connected. Other terminal of push button is connected to 5V of Arduino.

Arduino Code:-

const int basepin=13;
//constant variable for transistor base pin
const int buttonpin=7;
//constant variable for button pin
int relaystate;
//relay state either on or off.
void setup() {
pinMode(basepin,OUTPUT); 
//we need output from basepin so configured output.
pinMode(buttonpin,INPUT);
//we need input from button so configured input.
}
void loop() {
digitalWrite(basepin,pushbutton());//write high or low depending on press of pushbutton.   
delay(125);
//delay of 125 ms
}
//function created for push button operation.
int pushbutton(){
int buttonstate=digitalRead(buttonpin);  
if(buttonstate==HIGH){
if(relaystate==LOW){
  relaystate=HIGH; 
}
else{relaystate=LOW;}
}
return relaystate;
  
}

Working of circuit:-

When the push button is pressed for first time micro controller registers the press and relay is turned on and 12 volt appears across series combination of 1K resistor and LED. When same button is pressed second time the relay would turn off and LED will also gets turned off.

Leave a comment

Design a site like this with WordPress.com
Get started