Please update your Flash Player to view content.

                  FOLLOW  US





 
 
Interfacing Relay to Microcontroller
Tutorial - 8051 Tutorial
Written by Amol Shah   

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

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

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

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Ω

.

 

Comments  

 
+2 # Salman 2011-11-07 20:00
What are the values for R2, R3 & R4 for figure 2! Thanks is advance!
Reply | Reply with quote | Quote
 
 
+2 # Amol Shah 2011-11-08 16:16
Quoting Salman:
What are the values for R2, R3 & R4 for figure 2! Thanks is advance!


Value for R2 will depend on the current required by the relay. You can use 4.7k(else 2.2k), same for R4. For R3 you can use 1k.
Reply | Reply with quote | Quote
 
 
+1 # Vanessa 2011-11-20 08:02
what formula did you use in computing the values of each resistors? thanks in advanc
Reply | Reply with quote | Quote
 
 
+3 # amol shah 2011-11-20 10:15
Quoting Vanessa:
what formula did you use in computing the values of each resistors? thanks in advanc


The resistor in series with the led is for controlling the intensity of LED. Iled= (Vcc-1.2)/R.1 Normally Vcc =12v and I use 4.7k for red led.

The formulas for the base resistor have been provided in the above article....
Reply | Reply with quote | Quote
 
 
-1 # Vanessa 2011-11-20 12:17
thanks!
Reply | Reply with quote | Quote
 
 
-1 # Vanessa 2011-11-20 12:31
is the gain of the transistors you use here is the same for an npn 2n2222 transistor gain?
Reply | Reply with quote | Quote
 
 
-1 # amol shah 2011-11-20 13:30
you can use 2n2222 instead of BC547 i think the gain of 2n2222 is greater than BC547 so you can increase the value of BC547...but you can even continue with the above values
Reply | Reply with quote | Quote
 
 
+1 # rakshith 2011-11-08 23:13
R1 value depends on the type of LED normally u can use b/w 470ohm to 1K, higher the value of R1 lower the intensity of LED
Reply | Reply with quote | Quote
 
 
+1 # rakshith 2011-11-08 23:12
R1 value depends on the type of LED normally u can use b/w 470ohm to 1K, higher the value of R1 lower the intensity of LED
Reply | Reply with quote | Quote
 
 
+2 # palaniappan 2011-11-19 19:19
i need project based on interfacing relay to optocoupler
Reply | Reply with quote | Quote
 
 
+1 # amol shah 2011-11-20 10:11
Quoting palaniappan:
i need project based on interfacing relay to optocoupler

why do you require optocoupler to interface relay? The relay will give the required isolation. what project are you implementing?
Reply | Reply with quote | Quote
 
 
+1 # jacqueline 2011-12-28 02:00
hi I jsut want to thank you about ur article it has given me a lot of input
Reply | Reply with quote | Quote
 
 
+1 # gocha 2012-01-02 17:41
how to use no and nc in circuit?
Reply | Reply with quote | Quote
 
 
0 # prashanth 2012-01-23 21:08
hi ur article is very good...........
Reply | Reply with quote | Quote
 
 
0 # Niket 2012-01-26 14:49
what if I want to control the microcontroller with a RF module or lets say a xbee,in case we want to control the whole operation wirelessly.....what is the procedure?
Reply | Reply with quote | Quote
 
 
0 # tushar 2012-02-03 08:22
sir how to interface gsm module
Reply | Reply with quote | Quote
 

Add comment


Security code
Refresh

 

Featured Products

Rs800.00
Rs720.00
You Save: 10.00%

Rs600.00
Rs540.00
You Save: 10.00%

Rs1 800.00

 

Random Products

Rs180.00





Rs3 000.00





Rs10.00



Latest Video

Random Articles

Search Products

User Login






 
 
.
 TUTORIALS
.
  8051 Tutorial
 
  Power Supply
 
CODE LIBRARY
.

  8051 Assembly
 
  C Code Library

.
 PROJECTS
.
  Project List
 
  Synopsis and Abstract
 
DOWNLOADS
.

  Datasheets
 
  Download

.
 POPULAR
.
  About Us
 
  DNA Shop
 
  Totorial
 
  Code Library
 
  Download
 

.
 CONTACT US
.

Mr. Amol Shah

DNA Technology Nashik,

Maharashtra 422009 India

Phone : 0253-3023939

Mobile : 09225145135

Email : contact@dnatechindia.com