Interfacing Relay with 8051 using Keil C – AT89C51

Interfacing Relay with 8051 using Keil C – AT89C51

In some electronic applications we need to switch or control high voltages or high currents. In these cases we may use electromagnetic or solid state relays. For example, it can be used to control home appliances using low power electronic circuits.

relay

relay

An electromagnetic relay is a switch which is used to switch High Voltage or Current using Low power circuits. It magnetically isolates low power circuits from high power circuits. It is activated by energizing a electromagnet, coil wounded on a soft iron core. For detailed working of relay please visit this page. A relay should not be directly connected to a microcontroller, it needs a driving circuit due to the following reasons.

  • A microcontroller will not able to supply current required for the proper working of a relay. The maximum current that A89C51 microcontroller can sink is 15mA while a relay needs about 50 – 100mA current.
  • A relay is activated by energizing its coil. Microcontroller may stop working by the negative voltages produced in the relay due to its back emf.

Interfacing Relay with 8051 using Transistor

Transistor is wired as a switch. which drives the relay. The transistor will be in OFF state when the when the pin P2.0 is in LOW state. When 1 is written to P2.0 current will flow to the base of the transistor and the relay energises.

Circuit Diagram

Interfacing Relay with 8051 using Transistor - Circuit Diagram

Interfacing Relay with 8051 using Transistor – Circuit Diagram

Keil C Program

#include<reg52.h>

sbit relay_pin = P2^0;

void Delay_ms(int);

void main()
{
  do
  {
    relay_pin = 0; //Relay ON
    Delay_ms(1000);
    relay_pin = 1; //Relay OFF
    Delay_ms(1000);
  }while(1);
}

void Delay_ms(int k)
{
  int j;
  int i;
  for(i=0;i<k;i++)
  {
   for(j=0;j<100;j++)
   {
   }
  }
}

Interfacing Relay with 8051 using ULN2003

When we need more than one relays, using transistors and diodes become bulky. In these cases we can use ULN drivers. These are monolithic IC s consists of High Voltage High Current Darlington transistor arrays. When using these driver ICs we don’t need to connect freewheeling diode as they have built in clamp diodes. Here we are using ULN2003 for demostration.


 Circuit Diagram

Interfacing Relay with 8051 using ULN2003

Interfacing Relay with 8051 using ULN2003

Keil C Program

#include<reg52.h>

#define relayport P2

void Delay_ms(int);

void main()
{
  do
  {
    relayport = 0xFF; //All relays On
    Delay_ms(1000);
    relayport = 0x00; //All relays Off
    Delay_ms(1000);
  }while(1);
}

void Delay_ms(int k)
{
  int j;
  int i;
  for(i=0;i<k;i++)
  {
    for(j=0;j<100;j++)
    {
    }
  }
}

 

Share this post

Comments (14)

  • #[email protected]

    Thax for this tutorial….. add some more please!!!!!!

    February 20, 2015 at 1:09 pm
  • Ligo George

    Thanks for your feedback.

    February 23, 2015 at 4:23 pm
  • Er Aakash D Garg

    can we use TIP-120 for interfacing relay, will that be halpful for high current application?

    April 29, 2015 at 1:58 am
  • Ligo George

    Yes you can use it for high current applications.

    May 6, 2015 at 10:09 am
  • Er Aakash D Garg

    I was trying to interface relay via TIP 122 to At89S52, but having trouble as it is unsuccessful to drive relay

    May 8, 2015 at 8:27 pm
  • Ligo George

    Don’t you provide external pull up resistor ?

    May 22, 2015 at 12:07 pm
  • Er Aakash D Garg

    yes i did, but i think it is current in which At89s52 is lagging and didn’t able to switch TIP

    May 22, 2015 at 10:06 pm
  • Ligo George

    Try switching it by directly connecting to VCC, if it works you can solve the problem easily by reducing the value of PULL UP resistor.

    June 6, 2015 at 3:41 pm
  • Yogesh

    i have troble with all 8 relay ON at one time
    i have 8 relay interfacing with uln2803 i can only two relay on at a time but when we press third relay then all are OFF relay. so why?

    June 24, 2015 at 11:36 am
  • Ligo George

    May be you are breaching the current limit.

    July 1, 2015 at 7:25 pm
  • surbhi

    hey i want coding in c for elevator system in keil.
    i want when lift is at 1 position it should go to 3 floor step by step an then if 1 button is pressed then step by step upwards

    July 25, 2015 at 6:03 pm
  • sandip patel

    how to make relay circuit use of one switch and two relay.
    first press switch relay1 is on,
    second time press switch relay 1 is off and relay2 is on,
    third time press switch relay 2 is off and relay 1 is off.

    January 13, 2017 at 11:46 pm
  • anonymous

    hi Ebin George, i want in brief the code for IoT based home automation basead on KEIL mu vision. please share a response asap!!

    March 5, 2018 at 5:54 pm
  • Muhammad Saqib

    is we need any tansistor when we use 5v relay

    December 13, 2018 at 7:42 pm

Leave a Reply

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