Quantcast
Viewing all articles
Browse latest Browse all 215

Digital Code Locker System using 8051 Microcontroller

Image may be NSFW.
Clik here to view.
combination-code-digital-locker-system-using-8051-microcontroller
Digital Code locker system using 8051
Microcontrollers form the most significant part of several security and surveillance systems which plays an important role in ensuring safety of our priced possession. Here we are about to see the Embedded design and programming of a Password based Digital locker system using 8051 Microcontroller. Despite the simple look this system can handle intrusions to offer high protection and also alerts the user in case anonymous person tries to break into the system.


16 x 2 LCD:





Image may be NSFW.
Clik here to view.
16-x-2-lcd-pin-diagram-interface-8051-microcontroller
16x2 LCD Pin diagram
16 x 2 LCD is most commonly used display in many applications, it consists of a 16 columns and 2 columns hence got the name 16 x 2 LCD. The LCD consists of a Data register which is used to write the data to be displayed and a Command register used to write the instructions needed to the display. I have written a brief article on Programming and Interfacing LCD with 8051 Microcontroller kindly take a look before getting started with this project.


4 x 3 KEYPAD:


Image may be NSFW.
Clik here to view.
4x3-keypad-interface-8051-microcontroller
4x3 keypad
The keypad part of this project was used to feed password input and to initialize the system. I have used a 4 x 3 configuration keypad a total of 12 keys in it, you can increase the  number of keys by altering the program and design. Read Programming and Interfacing of Keypad with 8051 Microcontroller to obtain complete knowledge about it.


DESIGN:

This system comprises a bit complex design in order to protect the safe from intruders and alarm the user in case any breach was detected. This design of this system comprises of three basic steps, they are

INITIALIZATION:

  • The system needs to be initialized by pressing the "*" key in the keypad.
  • This locker will not respond to any of the key press before initialization using "*" key.
  • It will Turn ON the LCD and count of trials is initialized to zero.
  • A command  "Enter Password" in the LCD and blink of cursor will convey that the system is ready for password input.

PASSWORD INPUT:

  • The strength of the password in our system is 4 characters.
  • The password input can be fed into the system by using keys from 1,2......9
  • As soon as you enter four characters controller will verify if the entered password matches with the system password.
  • Based on the password verification by the controller there will be two cases totally.

CASE 1:

  • If the password matches, LCD will display as Message "Locker Open" and relay will gets activated.
  • After the usage of the protected locker, you need to turn off the system by means of pressing "#" key in the keypad
  • The relay as the system will gets turned off.

CASE 2:

  • If the password doesn't match , LCD will display a Message "Wrong Password" in it and the trial count will be incremented by one in the microcontroller.
  • Then the user have to initialize the system by pressing "*" before making the next try.
  • If a user entered the correct password in the second attempt then the trail count will be back to zero and then the user have to lock the system after usage after pressing "#"
  • But if the user failed to enter correct password for three continuous attempts, then alarm will start to alert the security surrounding there. 
  • Then the system will not respond to any of the key inputs therefore the intruder cannot stop the alarm.
  • The alarm can be stopped only by turning off the system which should be accessible only to the orginal user.

TURNING SYSTEM OFF:

  • Now the Last thing after usage of the system is to turn them off using the "#" key.
  • This applies only in the case if the user entered correct password.
  • If the number of trial gets over the system enters into to alert mode and cannot be turned off using "#" during that period of time.

    WATCH THE SIMULATION VIDEO TO GET BETTER UNDERSTANDING:




    CODE:


    1. #include<stdio.h>
    2. #include<reg51.h>
    3. #include<stdlib.h>
    4. #include<string.h>
    5. void cmdwr(void);                             //LCD command write sub routine                   
    6. void datwr(void);                              //LCD data write sub routine
    7. void delay(void);                               //Delay function
    8. void check(void);                              //Password check function
    9. void answ(void);                               //Trial and result analysis function
    10. void asterisk(void);                           //Asterisk key input function
    11. void debounce_delay(void);               //Keypad debounce delay functiom
    12. void trl_chk(void);                           //trial Check function
    13. void init(void);                                 //system initialization
    14. sbit en=P3^2;                              
    15. sbit rs=P3^0;
    16. sbit rw=P3^1;
    17. sbit relay=P3^4;
    18. sbit alarm=P3^7;
    19. code unsigned char msg[14]="Enter Password";
    20. code unsigned char open_msg[11]="Locker Open";
    21. code unsigned char lock_msg[14]="Wrong Password";
    22. code unsigned char array1[5]={0x38,0x0f,0x06,0x01,0x80};
    23. code unsigned char array2[5]={0xc0,0xc0,0xc1,0xc2,0xc3};
    24. unsigned data passwd[5]={0,2,9,4,0};           //Locker password initial '0'is dummy
    25. unsigned data digit[5];
    26. unsigned char i,p,k,l,m,n,f,y;
    27. int count=0,num=0,trial=0;          //initializing count,trial and num to zero
    28. void main()
    29. {
    30.  relay=0;                                 //turning relay off
    31.  alarm=0;                               //turning alarm off
    32.  while(1)
    33.   {
    34.     P2=0xf7;                         //Initialization key scan
    35.     i=(P2&0xf0);
    36.      {
    37.       if(i==0xe0)
    38.        {
    39.          init();
    40.          goto LOOP;
    41.         }
    42.       }
    43.    }
    44. LOOP:while(1)                        //Entire key scan starts
    45. {
    46.   P2=0xfe;
    47.   i=(P2&0xf0);
    48.   {
    49.     if(i==0xe0)
    50.      {
    51.        debounce_delay();         //Delay to over come keypad bounce
    52.        count++;                     //incrementing count
    53.        asterisk();
    54.        digit[count]=1;           //Setting key value
    55.        check();
    56.        answ();  
    57.       } 
    58.     else if(i==0xd0)
    59.       {
    60.        debounce_delay();
    61.        count++;
    62.        asterisk();
    63.        digit[count]=2;
    64.        check();
    65.        answ();
    66.       }
    67.     else if(i==0xb0)
    68.       {
    69.        debounce_delay();
    70.        count++;
    71.        asterisk();
    72.        digit[count]=3;
    73.        check();
    74.        answ();
    75.       } 
    76.    }
    77.   P2=0xfd;
    78.   i=(P2&0xf0);
    79.   {
    80.     if(i==0xe0)
    81.       {
    82.        debounce_delay();
    83.        count++;
    84.        asterisk();
    85.        digit[count]=4;
    86.        check();
    87.        answ();
    88.       }
    89.     else if(i==0xd0)
    90.       {
    91.        debounce_delay();
    92.        count++;
    93.        asterisk();
    94.        digit[count]=5;
    95.        check();
    96.        answ();
    97.       }
    98.     else if(i==0xb0)
    99.       {
    100.        debounce_delay();
    101.        count++; 
    102.        asterisk();
    103.        digit[count]=6;
    104.        check();
    105.        answ();
    106.       }
    107.   }
    108.   P2=0xfb;
    109.   i=(P2&0xf0);
    110.    {
    111.     if(i==0xe0)
    112.      {
    113.       debounce_delay();
    114.       count++;
    115.       asterisk();
    116.       digit[count]=7;
    117.       check();
    118.       answ();
    119.      }
    120.     else if(i==0xd0)
    121.      {
    122.       debounce_delay();
    123.       count++;
    124.       asterisk();
    125.       digit[count]=8;
    126.       check();
    127.       answ();
    128.      }
    129.     else if(i==0xb0)
    130.      {
    131.       debounce_delay();
    132.       count++;
    133.       asterisk();
    134.       digit[count]=9;
    135.       check();
    136.       answ();
    137.      }
    138.   }
    139.   P2=0xf7;
    140.   i=(P2&0xf0); 
    141.    {
    142.    if(i==0xe0)
    143.     {
    144.       debounce_delay();
    145.       for(p=0;p<=4;p++)
    146.        {                                          
    147.         P1=array1[p];
    148.         cmdwr();
    149.         delay();
    150.        }
    151.       for(m=0;m<=13;m++)
    152.        {
    153.         P1=msg[m];                      //Enter password message display
    154.         datwr();
    155.         delay();
    156.        }
    157.      P1=0x3c;
    158.      cmdwr();
    159.      delay();
    160.      P1=0xc0;
    161.      cmdwr();
    162.      delay();
    163.      count=num=0;
    164.     }
    165.    else if(i==0xd0)
    166.     {
    167.      debounce_delay();
    168.      count++;
    169.      asterisk();
    170.      digit[count]=0;
    171.      check();
    172.      answ();
    173.     }
    174.   else if(i==0xb0)
    175.     {
    176.      debounce_delay();
    177.      relay=0;
    178.      num=count=0;
    179.      P1=0x08;
    180.      cmdwr();
    181.      delay();
    182.     }
    183.    }
    184.   }
    185.  }
    186. void datwr()
    187.  {
    188.   rs=1;rw=0;en=1;delay();en=0;            //steps to send data to lcd
    189.  }
    190. void cmdwr()
    191.  {
    192.   rs=0;rw=0;en=1;delay();en=0;          //steps to send command to lcd
    193.  }
    194. void delay()                                       //software delay
    195.  {    
    196.   for(k=1;k<200;k++);
    197.    {
    198.     for(l=1;l<=200;l++);
    199.    }
    200.  }
    201. void check()
    202.  {
    203.   switch(count)
    204.    {
    205.    case 1:if(digit[count]==passwd[count])
    206.            {
    207.             num=num+1;
    208.            }
    209.            break;
    210.   case 2:if(digit[count]==passwd[count])
    211.           {
    212.            num=num+1;
    213.           }
    214.            break;
    215.   case 3:if(digit[count]==passwd[count])
    216.          {
    217.           num=num+1;
    218.          }
    219.           break;
    220.   case 4:if(digit[count]==passwd[count])
    221.          {
    222.           num=num+1;
    223.          }
    224.          break;
    225.        }
    226. }
    227. void answ()
    228.  {
    229.  if(count==4)                                       //Checking count
    230.   {
    231.   if(num==4)                                       //Checking "num" value to verify password 
    232.      {
    233.       relay=1;
    234.       for(p=0;p<=4;p++)
    235.        {
    236.         P1=array1[p];
    237.         cmdwr();
    238.         delay();
    239.        }
    240.       for(f=0;f<=10;f++)
    241.        {
    242.         P1=open_msg[f];                   //Locker open msg
    243.         datwr();
    244.         delay();
    245.        }
    246.       count=num=0;
    247.       trial=0;
    248.       while(1)
    249.        {
    250.         P2=0xf7;
    251.         i=(P2&0xf0);
    252.          {
    253.           if(i==0xe0)
    254.            {
    255.             init();
    256.             break;
    257.            }
    258.           else if(i==0xb0)
    259.            {
    260.             debounce_delay();               
    261.             relay=0; 
    262.             num=count=0;
    263.             P1=0x01;
    264.             cmdwr();
    265.             delay();
    266.             P1=0x02;
    267.             cmdwr();
    268.             delay();
    269.            }
    270.          }
    271.        }
    272.       }
    273.   else 
    274.    {
    275.     relay=0;
    276.     for(p=0;p<=4;p++)
    277.      {                                                                           
    278.       P1=array1[p];
    279.       cmdwr();
    280.       delay();
    281.      }
    282.     for(n=0;n<=13;n++)
    283.      {
    284.       P1=lock_msg[n];                               //Locker closed message
    285.       datwr();
    286.       delay();
    287.      }
    288.     trial++;
    289.     trl_chk();
    290.     while(1)
    291.      {
    292.       P2=0xf7;
    293.       i=(P2&0xf0);
    294.      {
    295.       if(i==0xe0)
    296.        {
    297.         init();
    298.         break;
    299.        }
    300.      }
    301.    }
    302.   }
    303.  }
    304. }
    305. void asterisk()                             //Subroutine to display "*" in LCD
    306.  {
    307.   P1=array2[count];
    308.   cmdwr(); 
    309.   P1=0x2a;
    310.   datwr();
    311.  }
    312. void debounce_delay()                  //Debounce Delay generation using timers
    313.  {
    314.   for(y=0;y<=4;y++)
    315.    {
    316.     TMOD=0x01;
    317.     TH0=0x3C;
    318.     TL0=0xB0;
    319.     TR0=1;
    320.     while(TF0==0);
    321.     TR0=0;
    322.     TF0=0;
    323.    }
    324.  }
    325. void trl_chk()                          //Function to check trials
    326.  {
    327.   if(trial>2)                            //Verifying number of trials
    328.    {
    329.     P2=0x00;
    330.     while(1)
    331.      {
    332.       alarm=1;
    333.      }
    334.    }
    335.  }
    336. void init()                            //Function to initialize system
    337.  {
    338.   debounce_delay();
    339.   for(p=0;p<=4;p++) 
    340.    {                           
    341.     P1=array1[p];
    342.     cmdwr();
    343.     delay();
    344.    }
    345.   for(m=0;m<=13;m++)
    346.    {
    347.     P1=msg[m];
    348.     datwr();
    349.     delay();
    350.    }
    351.    P1=0x3c;
    352.    cmdwr();
    353.    delay(); 
    354.    P1=0xc0;
    355.    cmdwr();
    356.    delay();
    357.    count=num=0;
    358.  }
    For more reference download the Package of design and hex file of the above system. Get it here 

    NOTE:

    • The first '0' of the array "passwd" is dummy,  "2940" is the password for the above system.
    • In case you face with multiple inputs while pressing a key increase the debounce delay value by means of y value increment in the sub routine "debounce_delay".
    Like this project? Share this with others through social sites and don't forget to follow us for more projects. Feel free to post your queries and we are happy to help you. Thank you  for visiting and Enjoy Coding.
      Image may be NSFW.
      Clik here to view.

      Viewing all articles
      Browse latest Browse all 215

      Trending Articles