top of page
Writer's pictureSanskruti Ashtikar

Digital Code Lock Using 8051 Microcontroller

Updated: 3 days ago

Introduction


A digital code lock system is a secure and efficient way to control access to restricted areas or devices. This system uses a keypad for inputting a code, a microcontroller to process the input, and an output device (like an electric lock or a solenoid) to indicate whether the code entered is correct. This article explains the design and implementation of a digital code lock using the 8051 microcontroller.





Components Required


  1. 8051 Microcontroller

  2. 4x4 Keypad

  3. 16x2 LCD Display

  4. Relay Module or Solenoid Lock

  5. Power Supply

  6. Connecting Wires

  7. Resistors, Capacitors, and other passive components


System Overview


The digital code lock system consists of a 4x4 keypad for inputting the code, an LCD to display the status, and a relay module to control the lock mechanism. The 8051 microcontroller interfaces with the keypad to read the input code, compares it with the predefined code, and controls the relay based on the result.


Circuit Diagram


Digital Code Lock System Circuit Diagram:



Note: Add the circuit diagram image as described above.


Working Principle


  1. Keypad Interface:

  2. The 4x4 keypad is connected to the microcontroller's input ports.

  3. Each key press generates a unique combination of row and column signals.

  4. Code Verification:

  5. The microcontroller reads the key pressed and stores the input.

  6. Once the complete code is entered, the microcontroller compares it with the predefined code.

  7. Output Control:

  8. If the entered code matches the predefined code, the microcontroller activates the relay to unlock the door.

  9. The status (correct or incorrect code) is displayed on the LCD.


Circuit Design


1. Keypad Interface:

  • Connect the rows of the 4x4 keypad to one set of the microcontroller's input pins.

  • Connect the columns of the keypad to another set of the microcontroller's input pins.

2. LCD Display Interface:

  • Connect the data pins of the LCD to the designated ports of the 8051 microcontroller.

  • Connect the control pins (RS, RW, E) of the LCD to the appropriate microcontroller pins.

3. Relay Module Interface:

  • Connect the control pin of the relay module to one of the digital output pins of the microcontroller.


Programming the 8051 Microcontroller


The program for the 8051 microcontroller is written in Embedded C. It involves initializing the keypad and LCD, reading the keypad inputs, verifying the code, and controlling the relay.



Code:

#include <reg51.h>
// Define connections
sbit RS = P2^0;
sbit RW = P2^1;
sbit EN = P2^2;
sfr LCD_DATA = 0xA0; // LCD data port
sbit RELAY = P2^3;
// Define keypad connections
sbit R1 = P1^0;
sbit R2 = P1^1;
sbit R3 = P1^2;
sbit R4 = P1^3;
sbit C1 = P1^4;
sbit C2 = P1^5;
sbit C3 = P1^6;
sbit C4 = P1^7;
unsigned char code[] = "1234"; // Predefined code
unsigned char input[5];
unsigned char i = 0;
void delay(unsigned int ms) {
    unsigned int i, j;
    for(i = 0; i < ms; i++) {
        for(j = 0; j < 1275; j++);
    }
}
void lcd_cmd(unsigned char cmd) {
    LCD_DATA = cmd;
    RS = 0;
    RW = 0;
    EN = 1;
    delay(1);
    EN = 0;
}
void lcd_data(unsigned char data) {
    LCD_DATA = data;
    RS = 1;
    RW = 0;
    EN = 1;
    delay(1);
    EN = 0;
}
void lcd_init() {
    lcd_cmd(0x38); // 2 lines, 5x7 matrix
    lcd_cmd(0x0C); // Display on, cursor off
    lcd_cmd(0x06); // Increment cursor
    lcd_cmd(0x01); // Clear display
}
void lcd_string(unsigned char *str) {
    while(*str) {
        lcd_data(*str++);
    }
}
unsigned char read_keypad() {
    C1 = 1; C2 = 1; C3 = 1; C4 = 1;
    R1 = 0; R2 = 1; R3 = 1; R4 = 1;
    if(C1 == 0) { delay(100); while(C1 == 0); return '1'; }
    if(C2 == 0) { delay(100); while(C2 == 0); return '2'; }
    if(C3 == 0) { delay(100); while(C3 == 0); return '3'; }
    if(C4 == 0) { delay(100); while(C4 == 0); return 'A'; }


    R1 = 1; R2 = 0; R3 = 1; R4 = 1;
    if(C1 == 0) { delay(100); while(C1 == 0); return '4'; }
    if(C2 == 0) { delay(100); while(C2 == 0); return '5'; }
    if(C3 == 0) { delay(100); while(C3 == 0); return '6'; }
    if(C4 == 0) { delay(100); while(C4 == 0); return 'B'; }
    R1 = 1; R2 = 1; R3 = 0; R4 = 1;
    if(C1 == 0) { delay(100); while(C1 == 0); return '7'; }
    if(C2 == 0) { delay(100); while(C2 == 0); return '8'; }
    if(C3 == 0) { delay(100); while(C3 == 0); return '9'; }
    if(C4 == 0) { delay(100); while(C4 == 0); return 'C'; }
    R1 = 1; R2 = 1; R3 = 1; R4 = 0;
    if(C1 == 0) { delay(100); while(C1 == 0); return '*'; }
    if(C2 == 0) { delay(100); while(C2 == 0); return '0'; }
    if(C3 == 0) { delay(100); while(C3 == 0); return '#'; }
    if(C4 == 0) { delay(100); while(C4 == 0); return 'D'; }
    return '\0'; // No key pressed
}
void main() {
    unsigned char key;
    lcd_init();
    lcd_cmd(0x80); // Move to first line
    lcd_string("Enter Code:");
    while(1) {
        key = read_keypad();
        if(key != '\0') {
            lcd_cmd(0xC0); // Move to second line
            lcd_data(key);
            input[i++] = key;
            if(i == 4) {
                input[i] = '\0'; // Null-terminate the string
                i = 0; // Reset the index
                if(strcmp(input, code) == 0) {
                    lcd_cmd(0x01); // Clear display
                    lcd_string("Correct Code!");
                    RELAY = 1; // Activate relay to unlock
                    delay(5000); // Keep the relay on for 5 seconds
                    RELAY = 0; // Deactivate relay
                    lcd_cmd(0x01); // Clear display
                    lcd_cmd(0x80); // Move to first line
                    lcd_string("Enter Code:");
                } else {
                    lcd_cmd(0x01); // Clear display
                    lcd_string("Incorrect Code!");
                    delay(2000); // Display the message for 2 seconds
                    lcd_cmd(0x01); // Clear display
                    lcd_cmd(0x80); // Move to first line
                    lcd_string("Enter Code:");
                }
            }
        }
    }
}




Conclusion


A digital code lock system using the 8051 microcontroller is a secure and effective way to control access. This article outlines the components, circuit design, and programming required to build such a system. With further enhancements like multiple user codes and real-time clock integration, this system can be adapted for more complex security applications.


Want us to guide you through your project or make the project for you ?






Create Various Projects

Check out our Free Arduino Projects Playlist - Arduino Projects 

Check out our Free Raspberry Pi Projects Playlist - Raspberry Pi Projects 

Check out our Free TinkerCAD Projects Playlist - TinkerCAD Projects 

Check out our Free IoT Projects Playlist - IoT Projects 

Check out our Free Home Automation Projects Playlist - Home Automation Projects 

Check out our Free NodeMCu Projects Playlist - NodeMCu Projects 



3 views0 comments

Related Posts

See All

Comments


bottom of page