Interfacing Relay to Microcontroller
Written by Amol Shah, on Nov-2007
Views 38751    

Relays are devices which allow low power circuits to switch a relatively high Current/Voltage ON/OFF. For a relay to operate a suitable pull-in & holding current should be passed through its coil. Generally relay coils are designed to operate from a particular voltage often its 5V or 12V.

The function of relay driver circuit is to provide the necessary current (typically 25 to 70ma) to energize the relay coil.

Relay Driver Circuit Using Single Transistor
Relay Driver Circuit Using Single Transistor

Figure 1 shows the basic relay driver circuit. As you can see an NPN transistor BC547 is being used to control the relay. The transistor is driven into saturation (turned ON) when a LOGIC 1 is written on the PORT PIN thus turning ON the relay. The relay is turned OFF by writing LOGIC 0 on the port pin. A diode (1N4007/1N4148) is connected across the relay coil; this is done so as to protect the transistor from damage due to the BACK EMF generated in the relay's inductive coil when the transistor is turned OFF. When the transistor is switched OFF the energy stored in the inductor is dissipated through the diode & the internal resistance of the relay coil. Normally 1N4148 can be used as it is fast switching diode with a maximum forward current of 300ma. This diode is also called as free-wheeling diode.

The LED is used to indicate that the RELAY has been turned ON. The resistor R1 defines the current flowing through the LED thereby defining the LED’s intensity.

Resistor R2 is used as a Series Base Resistor to set the base current. When working with 8051 controllers I have noted that it’s not compulsory to use this resistor as the controller has internal 10k resistor which acts as a base resistor.

Microcontrollers have internal pull up resistors hence when a port pin is HIGH the output current flows through this internal pull up resistor. 8051 microcontrollers have an internal pull up of 10KΩ. Hence the maximum output current will be  5v/10k = 0.5ma. This current is not sufficient to drive the transistor into saturation and turn ON the relay. Hence an external pull up resistor R3 is used. Let us now calculate the value of R3. Normally a relay requires a pull in current of 70ma to be turned ON. So our BC547 transistor will require enough base current to make sure it remains saturated and provide the necessary collector current i.e. 70ma. The gain (hfe) of BC547 is 100 so we need to provide at least   70ma/100 = 0.7ma  of base current. In practice you require roughly double the value of this current so we will calculate for 1.4ma of base current.

Base Current(1.4ma) =o/p current of controller (0.5ma) + 5v/R3

Relay Driver Circuit Using Two Transistor
Relay Driver Circuit Using Two Transistor

From the above equation the value of R3 comes out to be 5.55KΩ. Typically I use 4.7KΩ resistor.

Whenever 8051 microcontroller is turned ON initially the controller is in reset state and all the controller pins are HIGH which would result in TURNING ON the relay every time power is turned ON or if there is a power fluctuation. This may also damage the device connected to relay so as to avoid this problem another transistor Q2 has been added between the controller & the previous transistor. This transistor acts as an inverter. Figure 2 shows the upated Relay Driver Circuit. So now when a High is applied from the controller the TRANSISTOR Q2 turns ON so the base of transistor Q1 gets 0 so the transistor Q1 turns OFF so the relay turns OFF. And when a LOW is applied from the controller the TRANSISTOR Q2 turns OFF so the base of transistor Q1 gets high voltage through the resistor R2 so the transistor Q1 turns ON which turns ON the relay.

So basically Q2 & R2 acts as an inverter.

Relay Driver Circuit using ULN2003
Relay Driver Circuit using ULN2003

If you want to connect more relays to microcontroller then you can use ULN 2003 for connecting seven relays or ULN 2803 for connecting eight relays. Figure 3 shows how to connect a relay to microcontroller using ULN 2003/ULN 2803.  These IC’s are high voltage, high current Darlington transistor arrays with open collector outputs and free-wheeling clamping diodes hence there is no need of a diode across the relay. Also there is no need of the series base resistor as the IC has an internal resistor of 2.7KΩ

 


Add this page to your favorite Social Bookmarking websites
Reddit! Del.icio.us! JoomlaVote! Google! Live! Facebook! StumbleUpon! Yahoo! Free social bookmarking plugins and extensions for Joomla! websites!
Quote this article in website Favoured Print Send to friend Related articles Save this to del.icio.us

Users' Comments (15) RSS feed comment
Posted by srikanth, on Nov-2008,
1. hai
ur explanation is very good thank u for providing good information
 
» Report this comment to administrator
» Reply to this comment...
» See all 1 replie(s)

Posted by suresh, on Jan-2009,
2. help
sir, your explanation is so simple and clear. and i am doing project and in that there is a controller to power on/off electrical appliances. can i use this ? is there any ic that will operate about 4 to 8 devices with microcontroller?
 
» Report this comment to administrator
» Reply to this comment...
» See all 1 replie(s)

Posted by Administrator, on Jan-2009,
3. use ULN2803
you can use ULN2803 which has 8 NPN darlington transistors so can be used to turn ON/OFF 8 relays
 
» Report this comment to administrator
» Reply to this comment...
» See all 1 replie(s)

Posted by Elec, on Feb-2009,
4. Alternate drive
By putting the resistor in serie with the transistor base, there is no consumption when the relay is deactivated. It also prevents a short circuit case if the microcontroller pin is set to output high. 
It can also prevents relay activation while the processor is being reset. 
For small relays, direct microcontroller drive is possible using 2, 3 or 4 pins from the same port so that they can be set/reset together and share the load. The pins must not be programmed as open collector, as this would stress the micro's protection diodes. In this case, there is no need for external components.
 
» Report this comment to administrator
» Reply to this comment...

Posted by max, on Mar-2009,
5. turn off relay
When is the relay supposed to turn to be turned off.?
 
» Report this comment to administrator
» Reply to this comment...
» See all 1 replie(s)

Posted by jron, on Jul-2009,
6. help
what kind of relay can i use? it is possible to use 12v or 5v relay in the circuit?
 
» Report this comment to administrator
» Reply to this comment...

Posted by amol, on Jul-2009,
7. re:help
you can use any kind of relay, 5v, 12v even 24v relays can be used. but see to it that the current required to energize the relay coil is less than the max collector current of the transistor.
 
» Report this comment to administrator
» Reply to this comment...

Posted by Prasad, on Jul-2009,
8. AC relay
Hi, Thanks for the ckt. 
 
I need some suggestion. 
 
For a project I need to controll a solenoid switch using a relay (OFF and ON). 
 
What type of relay can be used (AC In -->Relay--> AC Out). The relay will be controlled by a 555 timer or a microcontroller. 
 
Thanks in advance, 
Prasad
 
» Report this comment to administrator
» Reply to this comment...

Posted by Prady, on Sep-2009,
9. Plz help
sir. i want to control solenoid switch from 8051, so plz provide me information with a schematic diagram.
 
» Report this comment to administrator
» Reply to this comment...

Posted by Harsh dodia, on Sep-2009,
10. Complement
Sir the way you explain stuff is awesome...I understood everything u said ! Really a spl thx to you... 
I have 2 que: 
(1) If i use uln2003a to turn on/off a 4, 5v relay using 8051, is it necessary to connect a diode with uln2003 to eliminate BACK EMF ???? 
(2) can i connect the same 5v supply of 8051 to energies 4 relays or is it necessary to use separate 5v supply for relays ???? 
 
 
Waiting for your reply!!!
 
» Report this comment to administrator
» Reply to this comment...
» See all 1 replie(s)

Posted by rolex, on Apr-2010,
11. rolex
As replica watches it was rolex watches said above,fake rolex Welder watches are replica watch designed by Italo fake watches Fontano. Italo tag heuer Fontano is a breitling watches famous and very cartier watches talented designer whose patek philippe watches have won the hearts omega watches of brave men who always choose omega watches of the best possible breitling quality. Italo Fontano’s cartier watches have audemars piguet fantastic popularity with a montblanc watches great number of people: armani watches among them there are both armani those who love novelties and gucci watches unusua
 
» Report this comment to administrator
» Reply to this comment...

Posted by juli, on Apr-2010,
12. niceinformation
UR explanation wil be so gud.&it can clearly understand by all the people.thanq thanq very much for ur valuable information.
 
» Report this comment to administrator
» Reply to this comment...

Posted by Darsh, on May-2010,
13. nice article
Hi :grin, your article is very good.  
thanks for providing valuable info.
 
» Report this comment to administrator
» Reply to this comment...

Posted by Jake, on Jun-2010,
14. j
I'm using the parallax propeller and this circuit is not working. I don't know if there is something different about these micro controller's, but I have wired the circuit with all different possibilities for resistor values and it doesn't seem to be working. It outputs 3.3vdc, would that change anything? Is your Micro controller putting out 5vdc or something?
 
» Report this comment to administrator
» Reply to this comment...
» See all 1 replie(s)

Posted by Magi, on Jul-2010,
15. Need for R1
is it nessasary to use R1 when connecting relay using ULN 2803. then what is the value
 
» Report this comment to administrator
» Reply to this comment...
» See all 1 replie(s)

Add your comment



mXcomment 1.0.5 © 2007-2010 - visualclinic.fr
License Creative Commons - Some rights reserved
 
< Prev   Next >
Home arrow Tutorial arrow Interfacing Relay to Microcontroller