electroSome

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

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.

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

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

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++)
    {
    }
  }
}