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

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

  • 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.

  • 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

  • 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?

  • 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.

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

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

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


  • >