Monday, May 13, 2013

Automatic Room light Controller with OpAmp:

Automatic Room light Controller with OpAmp: 

It is more important to save power. here the one way to save the power by automatic controlling of room lights. This is also an application of OpAmp based comparator. This is based on the light intensity in the room, here we use LDR(light dependent resistor) who's resistance decreases with increase in room light intensity. There is a choice that you can change the threshold level to your desired level so that it can operate at that intensity level. It does not require any microcontroller or any other programmable devices.

components procured:
>LDR  
>OpAmp 741
>resistor 10k
>BC547 transistor
>SPDT relay 12V
>1N4001 diode 
>battery and a bulb

 here i connected the LDR output to the inverting terminal and a threshold voltage of 1.5v is connected to the non-inverting terminal. so that whenever the light intensity increases the LDR output will decrease and if it is less than the threshold the output of the opamp will be +Vsat=+5v.This will trun on the BC547 so that relay will be connected as shown here, bulb will not glow
                                           VLDR >1.5v ; transistor ON; Light OFF;

  to see the output                                             A book on OPAMP circuits

 when the room light intensity decreases LDR resistance will decrease and voltage across the resistor goes high. when it is greater than the threshold opamp output goes to -Vsat=-5v. then the BC547 will goes to the cutoff region (OFF state). so the light will glow.
                                  VLDR < 1.5v ; transistor OFF; light ON;

Automatic room fan controller using OpAmp:


This is the another application of a comparator. This can save the power and we don't need to switch ON and OFF the ceiling FAN manually. here we can set the threshold voltage to our desired value. The ceiling fan will be switched ON when the temperature increases the threshold value. We are using LM35 sensor to sense the room temperature. The sensitivity of this LM35 sensor is 10mv/ 'C. I want that fan to be switched on when the temperature increases more than 27'C. so set my threshold voltage to
                                 Vthreshold = 27'C x 10mv = 270mv = 0.27V

                      here the room temperature is 26'c so the output voltage is < 0.27V ; Fan off
  to see the output 



 when the temperature increases to 27'c or more, the output voltage of LM35 will increases to more than 0.27V. so that opamp output will goes to +Vsat=+5v. This will switch ON the transistor and so relay connects. This will switch ON the ceiling fan.

    here the temperature is 28'c so the output voltage >0.27v; fan ON      LM35 sensor

4x4 matrix keypad interfacing to 8051(AT89c51)

//----- reading the char/number from the keyboard----//
#include<reg51.h>
#define ROW P1      // assigning  Port1 to ROW
#define COL P2       // assigning port2 to COL
void msdelay(unsigned int value);
void sertx(unsigned char );
unsigned char dat[4][4]
={'0','1','2','3',    // keypad read data
                          '4','5','6','7',
                           '8','9','A','B',
                           'C','D','E','F'};
void main()
{
 unsigned char colloc,rowloc; // initializing column and row locations
 TMOD=0x20;               // timer -2 mode-2
 TH1=0xFD;                 // preset FD
 SCON=0x50;              // serial control reg. 9600 baudrate
 TR1=1;                      // timer-1 start
 COL=0xFF;               // make column pins input
 while(1)
 {
   do
   {
    ROW=0x00;      // write "00000000" to ROW  
       colloc=COL;  // read column input
    colloc&=0x0F;  // hide higher nibble
    }while(colloc!=0x0f); //check whether any change in column pins
    do
    {
     do
     {
      msdelay(20);
      colloc=COL;
      colloc&=0x0F;
      }while(colloc==0x0F);
      msdelay(20);                  // check column after 20ms
      colloc=COL;                  // it is for accuracy if there is any random spike it can ignore
      colloc&=0x0F;
      }while(colloc==0x0F);
    while(1)
    {
     ROW=0xFE;         // write "1111_1110" on to ROW
     colloc=COL;
     colloc&=0x0F;
     if(colloc!=0x0F// if there is a change in column/button pressed
     { 
      rowloc=0;          
       break;
      }
      ROW=0xFD;  // writing "1111_1101" onto the ROW
      colloc=COL;
      colloc&=0x0F;
      if(colloc!=0x0F // if there is a change in column/button pressed
       {
        rowloc=1;
        break;
        }
      ROW=0xFB;   // writing "1111_1011" onto the ROW
      colloc=COL;
      colloc&=0x0F;
      if(colloc!=0x0F // if button pressed / change in column
       {
        rowloc=2;
        break;
        }
      ROW=0xF7;     // writing "1111_0111" onto ROW
      colloc=COL;
      colloc&=0x0F;
      if(colloc!=0x0F// if button pressed/change in column
      {
       rowloc=3;
       break;
       }
       }
   if(colloc==0x0E)
   sertx( dat[rowloc][0]);
   if(colloc==0x0D)
   sertx(dat[rowloc][1]);
   if(colloc==0x0B)
   sertx(dat[rowloc][2]);
   if(colloc==0x07)
    sertx(dat[rowloc][3]);
    }
  }
void sertx(unsigned char x)
{
 SBUF=x;
 while(TI==0);
 TI=0;
 }
void msdelay(unsigned int value)
{
 unsigned int i,j;
 for(j=0;j<value;j++)
 for(i=0;i<100;i++) ;
  }

Result:   to see output

Dc motor interfacing to 8051(AT89c51)

Controlling  2 motors in both forward, reverse and one forward one reverse operation according to the 4 input pins. When input is 1110=both in forward;1101=both in reverse;1011=left one forward right one stop; 0111=left one stop and right one forward.

MicroCode:

#include<reg51.h>
sfr sw=0x90;
sbit EN1=P2^0;
sbit EN2=P3^0;
sbit MTR1_0=P2^1;
sbit MTR1_1=P2^2;
sbit MTR2_0=P3^1;
sbit MTR2_1=P3^2;
void main( )
{
EN1=0;
MTR1_0=0;
MTR1_1=0;
EN2=0;
MTR2_0=0;
MTR2_1=0;
while(1)
{
EN1=1;
EN2=1;
if(sw==0xFE)
{
MTR1_0=0;
MTR1_1=1;
MTR2_0=0;
MTR2_1=1;
}
else if(sw==0xFD)
{
MTR1_0=1;
MTR1_1=0;
MTR2_0=1;
MTR2_1=0;
}
else if(sw==0xFB)
{
MTR1_0=0;
MTR1_1=1;
MTR2_0=0;
MTR2_1=0;
}
else if(sw==0xF7)
{
MTR1_0=0;
MTR1_1=0;
MTR2_0=0;
MTR2_1=1;
}
else
{
MTR1_0=0;
MTR1_1=0;
MTR2_0=0;
MTR2_1=0;
 }
}
}

Result:   to see output

Display messages on 16x16 LCD using 8051(AT89c51)

#include<reg51.h>
sfr ldata=0x90;
sbit rs=P2^0;
sbit rw=P2^1;
sbit en=P2^2;
sbit sw1=P3^0;
sbit sw2=P3^1;
sbit sw3=P3^2;
void Tdelay(unsigned int );
void lcdcmd(unsigned char );
void lcd_data(unsigned char );
void lcd_init(void);
void lcd_display(unsigned char *c );

int main( )
{
while(1){
lcd_init( );
if (sw1==0)
lcd_display("Embedded Systems");
if (sw2==0)
lcd_display("Electronics");
if(sw3==0)
lcd_display("Embedded programming");
}
}
void Tdelay(unsigned int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<1275;j++);
}
void lcd_init( void)
{
lcdcmd(0x38);
Tdelay(25);
lcdcmd(0x0F);      //display on , cursor blinking
Tdelay(25);
lcdcmd(0x06);       //  cursor increment
Tdelay(25);
lcdcmd(0x80);        // start at row-1
Tdelay(25);
lcdcmd(0x01);        // clear screen
Tdelay(10);

}
void lcdcmd(unsigned char cvalue )   // lcd in command mode
{
ldata=cvalue;
rs=0;
rw=0;
en=1;
Tdelay(1);
en=0;
}
void lcd_display(unsigned char *dvalue)
{
unsigned int x;
for(x=0;dvalue[x]!=0;x++)
{
lcd_data(dvalue[x]);
Tdelay(10);
}
}
void lcd_data(unsigned char dvalue)       // lcd in data mode
{
ldata=dvalue;
rs=1;
rw=0;
en=1;
Tdelay(1);
en=0;
}

Result: to see output

16x2 LCD interfacing to 8051(AT89c51)

Displaying “Embedded Systems” in 16x2 LCD display  
#include<reg51.h>
sfr ldata=0x90;
sbit rs=P2^0;   // LCD reset
sbit rw=P2^1;  // read/write control
sbit en=P2^2;  // LCD r/w enable
void Tdelay(unsigned int );     // functions initializaion
void lcdcmd(unsigned char );
void lcd_data(unsigned char );
void lcd_init(void);
void lcd_display(unsigned char *c );

int main( )
{
while(1){
lcd_init( );
 lcd_display("Embedded Systems");
}
}
void Tdelay(unsigned int ms)  // providing some delay
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<1275;j++);
}
void lcd_init( void)
{
lcdcmd(0x38);
Tdelay(25);
lcdcmd(0x0F);      //display on , cursor blinking
Tdelay(25);
lcdcmd(0x06);       //  cursor increment
Tdelay(25);
lcdcmd(0x80);      // start at row-1
Tdelay(25);
lcdcmd(0x01);      // clear screen
Tdelay(10);
}
void lcdcmd(unsigned char cvalue ) // LCD in command mode opearaion
{
ldata=cvalue;
rs=0;
rw=0;
en=1;
Tdelay(1);
en=0;
}
void lcd_display(unsigned char *dvalue)
{
unsigned int x;
for(x=0;dvalue[x]!=0;x++)
{
lcd_data(dvalue[x]);
Tdelay(10);
}
}
void lcd_data(unsigned char dvalue) // LCD in data mode operation
{
ldata=dvalue;
rs=1;
rw=0;
en=1;
Tdelay(1);
en=0;
}
  

Result: to see output

Serial data transmission using 8051(AT89c51)

  Transmit the following strings when the corresponding switch is pressed
     “EMBEDDED SYSTEMS” ; “ EMBEDDED PROGRAMMING” ; “ASSEMBLY PROGRAMMING”;   

#include<reg51.h>
sbit sw1=P2^0;  // switchs connecting port-2 lower pins 0,1,2
sbit sw2=P2^1;
sbit sw3=P2^2;
void Transmit(unsigned char ); // function initialization
void main()
{
unsigned char x;
unsigned char name1[ ]="Embedded systems";
unsigned char name2[ ]="Embedded programming";
unsigned char name3[ ]="Assembly programming";
TMOD=0x20;  // timer-1 mode-2
TH1=0xFD;      // preset value FD
SCON=0x50;  //Serial control 9600 baud rate selection
TR1=1;            // run timer-1
while(1)
{
if(sw1==0)      // check if switch 1 is pressed
{
for(x=0;x<=16;x++ // transmitting 17 characters to serial port
{
Transmit(name1[x]);
}
}
if(sw2==0)       // check if switch 2 is pressed
{
for(x=0;x<=20;x++)   // transmitting 21 characters to the serial port
{
Transmit(name2[x]);
}
}
if(sw3==0)    // check if switch 3 is pressed
{
for(x=0;x<=20;x++// transmitting 21 characters to the serial port
{
Transmit(name3[x]);
}
}
}
}

// serial port declaration
void Transmit(unsigned char i)
{
SBUF=i;
while(TI==0); // wait until serial Transmit interrupt flag  overflow
TI=0;
}




Result: to see output                                                         serial cable
 

Pulse Width Modulation(PWM) using 8051

PWM has many applications in real world , for example:  to control the speed of a DC motor we use PWM signal. Here we are generating a PWM signal having  250ms , 125ms and 50ms pulse widths.


#include<reg51.h>
void T0delay(void);
sbit Mybit=P1^0;
// output PWM port
void main(void)
{
unsigned char x;
while(1)
{
Mybit=1;
for(x=0;x<10;x++) 
// generating 250ms delay for ON time
{
T0delay();
}
Mybit=0;
for(x=0;x<10;x++) 
// generating 250ms delay for OFF time
{
T0delay();
}
Mybit=1;
for(x=0;x<5;x++) 
// generating 125ms delay for ON time
{
T0delay();
}
Mybit=0;
for(x=0;x<5;x++) 
// generating 125ms delay for OFF time
{
T0delay();
}
Mybit=1;
for(x=0;x<2;x++) 
// generating 50ms delay for ON time
{
T0delay();
}
Mybit=0;
for(x=0;x<2;x++) 
// generating 50ms delay for OFF time
{
T0delay();
}
}
}

// 25ms delay function
void T0delay(void)
{
TMOD=0X01;
// timer-0 mode-1
TL0=0xFE;
TH0=0xA5;
TR0=1;
while(TF0==0);
TR0=0;
TF0=0;
}




Result: to see output

Pulse Generator using 8051


Providing Delay using internal timers of AT89c51. We are generating a pulse wave having 250ms ON time and 250ms OFF time. microcontroller clock time is 1.085us.
                                             25ms / 1.085us = 23041 = A5FE in hex
                                             25ms x 10 = 250ms                                             

#include<reg51.h>
void T0delay(void);
// delay function
sbit Mybit=P2^0;
// output port
void main(void)
{
unsigned char x;
while(1)
{
Mybit= ~Mybit;
// inverter
for(x=0;x<10;x++)
T0delay();
}
}
void T0delay(void)
{
TMOD=0x01;
// selecting timer-0 mode-1
TL0=0xFE;  
// count starts at A5FE
TH0=0xA5;
TR0=1;       
// timer-0 run
while(TF0==0);
//wait until timer-0 flag overflow
TR0=0;
// then stop timer-0
TF0=0;
// reset the overflow flag
}





  


Result:     to see output

Serial LEDs using 8051


connecting LEDS to P2 pins and SWITCHES to P1 pins.

#include<reg51.h>
sfr leds=0xA0;
sfr SW=0x90;
unsigned int x;
int main ()
{

while(1)
{
if(SW==0xFE)
{
      leds=
0x08 ;                       //"1000"
    for( x=0;x<20000;x++);      
// providing some random delay    
    leds=0x04;                          //"0100"
    for( x=0;x<20000;x++);
    leds=0x02;                          //"0010"
    for( x=0;x<20000;x++);
    leds=0x01;                          //"0001"
    for( x=0;x<20000;x++);
}
else if(SW==0xFD)
{   
    leds=0x01;                          //"0001"

    for( x=0;x<20000;x++);
    leds=0x02;                          //"0010"
    for( x=0;x<20000;x++);
    leds=0x04;                          //"0100"
    for( x=0;x<20000;x++);
    leds=0x08;                          //"1000"
    for( x=0;x<20000;x++);
 }
else if(SW==0xFB)
{   
    leds=0x09;                        //"1001"

    for( x=0;x<20000;x++);
    leds=0x06;                       //"0110"
    for( x=0;x<20000;x++);
  }
else if(SW==0xF7)
{
    leds=0x06;                      //"
0110"
    for( x=0;x<20000;x++);
    leds=0x09;                     //"1001"
    for( x=0;x<20000;x++);
}
else
    leds=0x00;
}
}

 
  to see output