Wednesday 12 October 2016

Home Security System Using 8051 microcontroller


Hello people,

Following code may be useful for students or enthusiasts who are trying to design a home security system on microcontrollers. It has been successfully simulated as well as implemented on Explore Embedded AT89S52 dev board. If you are having any doubts regarding following code then post a comment. I will try my best to answer the questions.
/* Title: Home Security System  
****************************************************************************************************
Author: Chaitannya Supe
****************************************************************************************************
Description: This project consists of 8051 controller, keypad(4x4), LCD, IR sensor and buzzer.
When the system is turned on, it welcomes you and asks for password to be entered.

Case1: If you successfully log in into system then some message("welcome to CVS mansion" in my case)
will be blinked. IR sensor will now be armed to detect any intruder. If IR sensor output is high the
alarm(buzzer) will be triggered and LED pattern will be displayed. After alarm has been triggered, your
keypad will not work anymore.

Case2: If you fail to log in(enter wrong password): you will be given 3 attempts(total 4 including 
original attempt) and after which the system will be locked. After locking, your system will reboot
and again above procedure will be repeated. If system is locked 2 times then system will shut down and
your keypad will be disabled.
****************************************************************************************************
This system has been successfully tested and implemented on Explore Embedded AT89S52 kit.
Feel free to ask any queries.
___________________________________________________________________________________________________________________________
Connection settings of the Kit-

4x4 Matrix Keypad connection
R1 -----> P1.0
R2 -----> P1.1
R3 -----> P1.2
R4 -----> P1.3
C1 -----> P1.4
C2 -----> P1.5
C3 -----> P1.6
C4 -----> P1.7

16x2 LCD Connection
Register Select (RS) --------------> P3.2
Read/Write(R/W) -------------------> P3.3
Enable(EN) ------------------------> P3.4
LCD Data Pins (DB0-DB7) -----------> P2 (P2.0-P2.7)

Crystal Ossilator(11.0592MHz) -----> XTAL1,XTAL2
VB=Battery Supply
VCC=regulated 5V+
Gnd=Ground(0V)
*/

#include
#define seg P2
#define led P0

unsigned int password=1234,check=0;
unsigned char count=0,x=0,Master_count,Master_count_actual;  // variable declaration with password
sbit rs=P3^2;   // defines bit for register select 
sbit rw=P3^3;  // defines bit for read/write
sbit en=P3^4;  // defines bit for enable
sbit ir=P3^7;  // IR sensor to be placed near entrance door
sbit alarm=P3^6; // Connected to buzzer
void lcdcmd(unsigned char cmd);    // function declaration for LCD command
void lcddata(unsigned char value); // function declaration for LCD data

/* setting bits for Rows & Columns of Matrix Keyboard */
sbit R1=P1^0;
sbit R2=P1^1;
sbit R3=P1^2;
sbit R4=P1^3;

sbit C4=P1^4;// Connections are made so left and uppermost key is 0.
sbit C3=P1^5;
sbit C2=P1^6;
sbit C1=P1^7;

void MSDelay(unsigned int itime)   // delay function
{
 unsigned int i,j;
 for(i=0; i < itime ; i++);
        for(j=0; j<450 ; j++);
}

void lcdcmd(unsigned char cmd)  // lcd command function
{
 seg=cmd;
 rs=0;
 rw=0;
 en=1;
 MSDelay(1);
 en=0;
}

void lcddata(unsigned char value)  // lcd data function
{
 seg=value;
 rs=1;
 rw=0;
 en=1;
 MSDelay(1);
 en=0;
}

void lcd_puts(unsigned char *str) //lcd string print function(Optimised for less hex file size)
{                   
 while(*str)
 {
  lcddata(*str);
  str++;
 }
}

char keypad()  //matrix keypad function
{
 if(x <= 3)  // Decided as per the password length
 {
   /* Check conditions for buttons  pressed */
  R1=0;R2=R3=R4=1;     
  if(C4==0){  lcddata('*');check=check*10+0; MSDelay(130);x++;}  // for digit 0
  if(C3==0){  lcddata('*');check=check*10+1; MSDelay(130);x++;}  // for digit 1
  if(C2==0){  lcddata('*');check=check*10+2; MSDelay(130);x++;}  // for digit 2
  if(C1==0){  lcddata('*');check=check*10+3; MSDelay(130);x++;}  // for digit 3
  R2=0; R1=R3=R4=1;
  if(C4==0){  lcddata('*');check=check*10+4; MSDelay(130);x++;}   // for digit 4
  if(C3==0){  lcddata('*');check=check*10+5; MSDelay(130);x++;}    // for digit 5
  if(C2==0){  lcddata('*');check=check*10+6; MSDelay(130);x++;}    // for digit 6
  if(C1==0){  lcddata('*');check=check*10+7; MSDelay(130);x++;}    // for digit 7
  R3=0; R1=R2=R4=1;
  if(C4==0){  lcddata('*');check=check*10+8; MSDelay(130);x++;}  // for digit 8
  if(C3==0){  lcddata('*');check=check*10+9; MSDelay(130);x++;}  // for digit 9
 }// Digits A, C, D, E, F are not considered. However you can add it.

 R3=0; R1=R2=R4=1;
 if(C1 == 0)  // for digit 11 Enter function has been assigned
 {
  lcdcmd(0x01);
  x=0;
  if(check == password)   // checks the condition 
  {
   lcd_puts("    SUCCESS!    ");
   count=0;
   MSDelay(1000);
   lcdcmd(0x01);
   lcd_puts("<< Welcome to >>");
   lcdcmd(0xC0);
   lcd_puts("| CVS Mansion |");
   while(1)
   {
    if(ir == 1) // IR sensor output is high
    {
     alarm = 1;
     lcdcmd(0x0C);
     lcdcmd(0x01);
     lcd_puts("INTRUDER ALERT!!");
     MSDelay(10);
     while(1) // LED pattern
     {
      led = 0x18;
      MSDelay(20);
      led = 0x3C;
      MSDelay(20);
      led = 0x7E;
      MSDelay(20);
      led = 0xFF;
      MSDelay(20);
      led = 0xE7;
      MSDelay(20);
      led = 0xC3;
      MSDelay(20);
      led = 0x81;
      MSDelay(20);
      led = 0x00;
      MSDelay(20);
     }
    }
    else
    {
     lcdcmd(0x0C); // LCD test will blink continuously
     MSDelay(100);
     lcdcmd(0x08);
     MSDelay(100);
    }
   }
  }
  else if(count < 2)
  {
   lcd_puts("TRY AGAIN!!");
   MSDelay(400);
   lcdcmd(0x01);
   lcd_puts("ENTER PASSWORD:");
   lcdcmd(0xC0);
   check = 0;
   count++;
  }
  else if(count == 2)
  {
   lcdcmd(0x01);
   lcd_puts("LAST CHANCE!!");
   MSDelay(400);
   lcdcmd(0x01);
   lcd_puts("ENTER PASSWORD:");
   lcdcmd(0xC0);
   check = 0;
   count++;
  }
  else if(count > 2)  // if wrong attempt is more than three
  {
   Master_count++;
   lcdcmd(0x01);
   lcd_puts("SYSTEM LOCKED!!");
   MSDelay(1000);
   lcdcmd(0x01);
   lcd_puts("Rebooting...");
   MSDelay(1000);
   lcdcmd(0x01);
   lcd_puts("ENTER PASSWORD:");
   lcdcmd(0xC0);
   count=0;
   check=0;
  }
 }
 return Master_count;
}

void main()
{
 alarm = 0; // make it 0 as by default its on.
 led = 0x00;
 lcdcmd(0x38);  // initialize LCD
 MSDelay(50);

 lcdcmd(0x0E);  // display ON, cursor OFF
 MSDelay(50);

 lcdcmd(0x01); // clear display screen
 MSDelay(50);

 lcdcmd(0x80);            
 MSDelay(50);

 lcd_puts("   Welcome to   ");
 lcdcmd(0xC0);
 lcd_puts("  CVS Security  ");
 MSDelay(1500);

 P1=0xF0;  // defines input for column and output for rows of Matrix keypad

 lcdcmd(0x01);
 lcdcmd(0x80);
 lcd_puts("ENTER PASSWORD:");
 MSDelay(50);
 lcdcmd(0xC0);
 while(1) //System will shut down if 2 attempts after REBOOT fails.
 {
  keypad();
  Master_count_actual = Master_count;
  if(Master_count_actual > 1)
  {
   break;
  }
 }
 lcdcmd(0x01);
 lcd_puts(" AUTHORISATION ");
 lcdcmd(0xC0);
 lcd_puts("   FAILED!!!   ");
 MSDelay(1000);
 lcdcmd(0x01);
 lcd_puts("SHUTTING DOWN...");
 MSDelay(1500);
 lcdcmd(0x08);
 while(1);          
} // main closed
The project setup will look something like this: