electronic lock using 8051

Electronic digital lock using the 8051 microcontroller and keypad

In this article, we will make an electronic digital lock using the 4×3 keypad and 8051 microcontroller, basically, an electronic lock is password protected lock and it is an application of a keypad.
Door lock system is an example of the electronic lock and it’s protected by a password, which only unlocks with a specific password and operated by electricity with the help of assessing control system (MCU).

Project Description

Here I have created an electronic lock using the 8051 microcontrollers and 4×3 keypad.
In this project keypad use as the input device and a 16×2 alphanumeric LCD use as the output device. When the user presses the keys of keypad then microcontroller read the value of the pressed key and display it on LCD.
Here I have already saved the password in the program when the user enters the value from the keypad, then we simply store these entered value into a buffer and compare it with the stored password.
If they are matched together then we display a “WELCOME” message on LCD and move the motor to unlock the electronic lock. Instead of, if the user-entered value does not match with stored password then display “WRONG PASSWORD” message on LCD.

e.g.
Suppose in the program I have stored the password value “12345”. So when the user wants to unlock the electronic lock, then he has to enter the value “1234” from the keypad. However, if the user does not enter the value “12345”, then he will be unable to unlock the electronic lock.

Required components for electronic lock

S.N COMPONENT
1. Microcontroller (AT89s52)
2. LCD 16×2
3. Keypad 4×3
4. Oscillator (12 MHz)
5. Ceramic capacitor (22 PF – 2)
6. Cell (power supply)
7. ULN 2003A
8. LED
9. Stepper motor
10. Connecting wire
11. Resistor(10,1.2  k-ohm)

Circuit connection of digital electronic lock

In below image, I have described the simple connection of numeric keypad, LCD and a stepper motor with the 8051 micro-controller.

Connection of lcd and keypad

If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.

Your free trial is waiting

Sample program for electronic lock using the 8051 microcontroller

 

/*Hardware
    --------
    At89c51 @ 12MHz external crystal.



     LCD

         VSS -  GND
         VDD - +5V
         VEE - connect with ground

         RS -  P1.0
         RW -  P1.1
         E  -  P1.2

         LCD_D0 - P3.0
         LCD_D1 - P3.1
         LCD_D2 - P3.2
         LCD_D3 - P3.3

         LCD_D4 - P3.4
         LCD_D5 - P3.5
         LCD_D6 - P3.6
         LCD_D7 - P3.7

        LED+ - +5V
        LED- - GND

      KEYPAD

         COL_1 -   P2.0
         COL_2 -   P2.1
         COL_3 -   P2.2

         ROW_1 -   P2.4
         ROW_2 -   P2.5
         ROW_3 -   P2.6
         ROW_4 -   P2.7

 STEPPER MOTOR

 COIL1 -P1.4
 COIL2 -P1.5
 COIL3 -P1.6
 COIL4 -P1.7



   */
//Program Start from here
#include<reg51.h>
#include<string.h>


// macro
#define COL1 0
#define COL2 1
#define COL3 2

#define MAX_ROW 3

//LCD
#define LCD P3 /*Attached Lcd on Port*/

sbit rs = P1^0; /*Configure RS Pin*/
sbit rw = P1^1; /*Configure R/W pin*/
sbit e  = P1^2; /*Configure Enable pin*/

//KEYPAD
#define KEYPAD P2

sbit COL_1 =  P2^0;
sbit COL_2 =  P2^1;
sbit COL_3 =  P2^2;

sbit ROW_1 =  P2^4;
sbit ROW_2 =  P2^5;
sbit ROW_3 =  P2^6;
sbit ROW_4 =  P2^7;

//Stepper Motor
#define STEPPER_MOTOR P1


/*Function to write command on Lcd*/
void LcdCommand(const char cCommand);

/*Function to write command on Lcd*/
void LcdData(const char cData);
/*Function to display message on Lcd*/
void DisplayMessage(const char *pszMessage);

/*Function To Initialize Lcd*/
void LcdInit(void);

/*Function to Provide delay*/
void Delay(unsigned int);

/*Reset The Row*/
void DoResetRow(short siRowNumber);

/*Check Columnn*/
char Check_Column(void);

/*Read Col Status*/
char ReadColStatus(void);

/*move stepper motor forword*/
void SteeperMotarForword(void);

/*move stepper motor backword*/
void SteeperMotarBackword(void);

//Main
int  main()
{

    char ucData=0;
    unsigned char cLen =0;
    int iLenPassword =0;
    char acBuffer[8]= {0};
    const char *pcPassword= "12345";// Password

    KEYPAD = 0x0F; //Make Row O/p & Col I/p
    LCD    = 0x00;  //Make Lcd O/p
    rs = rw = e =0; //O/p
    STEPPER_MOTOR =0x00; //make o/p


    iLenPassword = strlen(pcPassword); //Calculate length of password

    LcdInit(); //initialize the lcd

    LcdCommand(0x80); /*Address of DDRAM*/

    DisplayMessage("Enter Password:");

    LcdCommand(0xc0); /*Address of DDRAM*/

    while(1)
    {

        if(cLen < iLenPassword)
        {

            ucData = ReadColStatus(); /*Read column Status*/

            LcdData(ucData); /*Display Enter Character On Lcd*/

            acBuffer[cLen] = ucData; /*Store Enter value in Buf*/

            cLen++;

        }
        else
        {

            if(!strncmp(pcPassword,acBuffer,iLenPassword))
            {

                LcdCommand(0x01); /*Clear the lcd*/
                LcdCommand(0x80); /*Address of DDRAM*/
                DisplayMessage("Door is Opening..");
                SteeperMotarForword();
                LcdCommand(0x01); /*Clear the lcd*/
                LcdCommand(0x85); /*Address of DDRAM*/
                DisplayMessage("Welcome");
                Delay(700);
                LcdCommand(0x01); /*Clear the lcd*/
                LcdCommand(0x80); /*Address of DDRAM*/
                DisplayMessage("Door is Closing..");
                SteeperMotarBackword();
                cLen=0;
                LcdCommand(0x01); /*Clear the lcd*/
                LcdCommand(0x80); /*Address of DDRAM*/
                DisplayMessage("Enter Password:");
                LcdCommand(0xc0); /*Address of DDRAM*/


            }
            else
            {

                LcdCommand(0x01); /*Clear the lcd*/
                LcdCommand(0x80); /*Address of DDRAM*/

                DisplayMessage("Access Denied..");
                Delay(100);

                LcdCommand(0x01); /*Clear the lcd*/
                LcdCommand(0x80); /*Address of DDRAM*/

                DisplayMessage("Wrong Password");
                Delay(300);

                LcdCommand(0x01); /*Clear the lcd*/
                LcdCommand(0x80); /*Address of DDRAM*/
                DisplayMessage("Enter Password:");
                LcdCommand(0xc0); /*Address of DDRAM*/
                cLen=0;

            }


        }
    }
    return 0;
}
/*Function to write command on Lcd*/
void LcdCommand(const char cCommand)
{

    rs = 0;
    rw = 0;
    e  = 1;
    LCD = cCommand;
    Delay(1);
    e=0;

}

/*Function to write command on Lcd*/
void LcdData(const char cData)
{

    rs = 1;
    rw = 0;
    e  = 1;
    LCD = cData;
    Delay(1);
    e=0;

}

/*Function to Display message on Lcd*/
void DisplayMessage(const char *pszMessage)
{

    while(*pszMessage!='\0')   //Till Null character
    {

        LcdData(*pszMessage);
        pszMessage++;
    }
}


/*Function to Provide Delay*/
void Delay(unsigned int i)
{
    int j,k;
    for(j=0; j<i; j++)
        for(k=0; k<1275; k++);
}


/*Initialise the LCD_DATA*/
void LcdInit(void)
{

    LcdCommand(0x01);
    Delay(2);
    LcdCommand(0x38);
    Delay(2);
    LcdCommand(0x06);
    Delay(2);
    LcdCommand(0x0c);
    Delay(2);
}
//Read the status of column
char ReadColStatus(void)
{

    char ucData='p';

    while('p' == ucData)
    {

        ucData = Check_Column();

    }
    return ucData;
}

//To check the status of coloumn
char Check_Column(void)
{

    short siColNumber=0;
    const unsigned char ucaKeyPad[4][3] =           //Key Pad 4x3
    {
        {'1','2','3'},
        {'4','5','6'},
        {'7','8','9'},
        {'*','0','#'}
    };

    for(siColNumber = 0; siColNumber <= MAX_ROW; siColNumber++)
    {

        DoResetRow(siColNumber); /*Reset the row*/

        if(COL_1 == 0)
        {
            Delay(30);
            return ucaKeyPad[siColNumber][COL1];
        }
        if(COL_2 == 0)
        {
            Delay(30);
            return ucaKeyPad[siColNumber][COL2];
        }
        if(COL_3 == 0)
        {
            Delay(30);
            return ucaKeyPad[siColNumber][COL3];
        }
    }
    return 'p';
}

void DoResetRow(short siRowNumber)
{

    ROW_1=ROW_2=ROW_3=ROW_4= 1; /*Make all row high*/

    switch(siRowNumber)
    {
    case 0:
        ROW_1 =0;
        break;
    case 1:
        ROW_2 =0;
        break;
    case 2:
        ROW_3 =0;
        break;
    case 3:
        ROW_4 =0;
        break;
    }
}


/*Function to move the stepper forword*/
void SteeperMotarForword(void)
{
    short int siLoop =0;

    while (siLoop < 9)
    {

        STEPPER_MOTOR &=0x0f;
        STEPPER_MOTOR |=0xc0;
        Delay(10);
        STEPPER_MOTOR &=0x0f;
        STEPPER_MOTOR |=0x60;
        Delay(10);
        STEPPER_MOTOR &=0x0f;
        STEPPER_MOTOR |=0x30;
        Delay(10);
        STEPPER_MOTOR &=0x0f;
        STEPPER_MOTOR |=0x90;
        Delay(10);
        siLoop++;

    }


}

/*Function to move the stepper backword*/
void SteeperMotarBackword(void)
{

    short int siLoop = 0;

    while (siLoop < 9)
    {

        STEPPER_MOTOR &=0x0f;
        STEPPER_MOTOR |=0x30;
        Delay(10);
        STEPPER_MOTOR &=0x0f;
        STEPPER_MOTOR |=0x60;
        Delay(10);
        STEPPER_MOTOR &=0x0f;
        STEPPER_MOTOR |=0xc0;
        Delay(10);
        STEPPER_MOTOR &=0x0f;
        STEPPER_MOTOR |=0x90;
        Delay(10);

        siLoop++;

    }

}

 

Working of electronic lock

Check out the below video to understand the working of the electronic lock.

Your opinion matters

Here I have tried to explain the interfacing of keypad and LCD and tried to create a sample project. I would like to know your thought about the above-discussed topic, so please don’t forget to write the comment in a comment box.

Recommended Post