Relay connection with 8051

this tutorial will give you small illustration of relay working with 8051.there are many situation where embedded system has to control large voltage or large current. in such case circuitry of embedded system which is operated at low voltage or low current can not drive. a relay is a elctro-mechanical device  which is used to isolate two circuits electrically and connect them magnetically.it is assumed you know to interface LCD

PROTEUS DESIGN

 

lcd is as usually connected  to port 2 .switch is used to control the state of bulb operated at 12v. a high source of current is required to connect the terminals,it can be achieved by using uln2003.

typically a relay has got three terminals which will work as switches namely normally open(NO) terminal,normally close(NC) terminal and common(com) terminal.when the relay is not energized then NC and common will be connected. if it is energized then NO and common will get connected

 

CODE

you can use relay as coupling agent for making connection between dc and ac components.when switch 'sw' gets plugged relay goes high which in turn causes bulb to glow.

complete code

#include<reg51.h>
#define lcdport P2
sbit sw=P1^5;
sbit rs=P1^0;
sbit rw=P1^1;
sbit en=P1^2;
sbit relay=P3^0;    
void lcd_cmd(char);
void lcd_init();
void lcd_data(char);
void lcd_string(char *);
void delay(unsigned int);

void main()
{
lcdport=0x00;
P3=0x00;
sw=0;
relay=0;
lcd_init();
delay(100);
lcd_string("RELAY CONTROLLED");
delay(1);
   while(1)
   {     lcd_cmd(0x01);
           lcd_string("cfl  bulb");
           delay(100);
           lcd_cmd(0x01);
           delay(1);
        if(sw==1)
           {
            lcd_cmd(0xc0);
            delay(1);
            lcd_string("cfl bulb on ");
            relay=1;
        }
           else
           {
            lcd_cmd(0xc0);
            delay(1);
            lcd_string("cfl bulb off");
             relay=0;
        }
       }    
}

 void delay(unsigned int x)
{
 unsigned int i,j;
 for(i=0;i<x;i++)
     for(j=0;j<1275;j++);
}

void lcd_init()
{
  lcd_cmd(0x38);
  delay(1);
  lcd_cmd(0x01);
  delay(1);
  lcd_cmd(0x0c);
  delay(1);
  lcd_cmd(0x80);
  delay(1);
  lcd_cmd(0x0e);
  delay(1);
}

void lcd_cmd(char value)
{
    rw=0;
    rs=0;
      lcdport = value;
      en=0;
      delay(1);
      en=1;
}
  
void lcd_string(char *p)
{
  while(*p!='\0')
   {
     lcd_data(*p);
     delay(20);
     p++;
   }
}

void lcd_data(char value)
{
    rs=1;
      rw=0;
      lcdport=value;
      en=0; 
      delay(1);
      en=1;
}

 

VIDEO

 

 

Category: 

tags: 

Share

Who's new

  • ravirajpatil871...
  • shubhambajoria
  • yassir
  • demiholyman890954
  • scottgillum51169040

Get Notified

 

Share

We are Social

Syndicate

Subscribe to Syndicate