Electronic cabinet lock based on microcontroller design

1. Preface

With the continuous development of modern society, the application of electronic cabinet locks is becoming more and more widespread. Traditional mechanical cabinet locks have some inconveniences, such as keys being easily lost and passwords easily leaked. Designing an electronic cabinet lock system based on a microcontroller has become an interesting and meaningful project.

The electronic cabinet lock system uses an electromagnetic lock as the switch of the cabinet lock, and controls the switch state of the electromagnetic lock through a relay. The user can enter the password through the matrix keyboard to unlock, and the password data will be displayed through the LCD1602 liquid crystal display. At the same time, the system also supports the functions of entering a password to verify the lock and change the password. When the user successfully enters the correct password and unlocks the door, the system will sound a buzzer.

This electronic cabinet locking system is designed to improve the safety and convenience of cabinet locks. Compared with traditional mechanical cabinet locks, electronic cabinet locks have the following advantages:

【1】Password security: Electronic cabinet locks use passwords as the unlocking method, which is more secure and reliable than traditional keys. Users can set more complex passwords as needed, effectively preventing password leaks and illegal unlocking.

【2】Convenient and easy to use: Users only need to enter the password through the matrix keyboard to unlock. There is no need to carry keys or memorize complex mechanical operating steps. The operation is simple and convenient.

【3】Password change function: Users can change the password at any time as needed, which improves the flexibility and maintainability of the cabinet lock.

[4] Prompt sound prompt: The system emits a prompt sound through a buzzer, allowing users to get clear feedback when entering the password and unlocking successfully, which improves the user experience.

The design of the electronic cabinet lock system is not only practical, but also provides a very good practical project for beginners learning embedded system design and microcontroller programming. Through this project, you can learn and master related knowledge and technologies such as input and output control, key scanning, LCD display, and buzzer control of microcontrollers. It also involves the design and implementation of password input and verification algorithms, which exercises logical thinking and programming abilities.

Through this electronic cabinet lock system project, you can experience the charm of modern electronic technology, improve the safety and convenience of cabinet locks, and provide users with a better experience. image-20230823171105869

image-20230823170732930

image-20230823170655846

2. Introduction to hardware selection

In terms of hardware selection, according to needs, the following is the final hardware selection of electronic cabinet locks:

[1] Main control chip: STC89C52 microcontroller is a commonly used 8-bit microcontroller with rich peripheral resources and large storage capacity. It is suitable as the main control chip of electronic cabinet locks.

【2】Electromagnetic lock: Choose a suitable electromagnetic lock as the switch of the cabinet lock to ensure that it can provide sufficient safety and reliability. Consider using a 12V electromagnetic lock to meet power and control signal requirements.

【3】Relay: Use a relay to control the on and off of the electromagnetic lock to ensure signal isolation and current amplification.

【4】Matrix keyboard: Select the applicable matrix keyboard to enter the password. Choose a 4×4 matrix keyboard with 16 keys and support for numeric and function keys.

【5】LCD1602 liquid crystal display: As an interface for password input and status display, LCD1602 has two rows and 16 columns of character display, which can clearly display the entered password and related prompt information.

【6】Buzzer: used to sound prompts such as successful unlocking and incorrect password input.

[7] Power module: It is very important to have a stable and reliable power supply. Choose to use an AC/DC 5/12V adapter for power supply.

3. Overall design idea

The software design logic and ideas are as follows:

【1】Initialization: At the beginning of the program, perform system initialization settings, including configuring IO ports, timers, peripherals, etc. At the same time, the password storage area, LCD1602 display screen, buzzer, etc. need to be initialized.

【2】Password input and verification: Read the password entered by the user through the matrix keyboard. A fixed-length password can be used, such as 4 characters. Each time the user presses a numeric key, it is added to the password buffer, and the corresponding “*” character is displayed on the LCD1602 to indicate that it has been entered. When the length of the entered password reaches the preset length, the password verification operation can be triggered.

【3】Password verification: Convert the number in the password buffer into a string form and compare it with the preset correct password. If the password is entered correctly, the unlocking operation is performed; otherwise, the password error prompt is processed.

【4】Unlocking operation: When the password verification is successful, the control relay is turned on and off to open or close the electromagnetic lock. At the same time, the buzzer will sound a successful unlocking sound, and the unlocking success message will be displayed on LCD1602.

【5】Change password: Provides the function of changing password. After successfully verifying the password, the user can enter a new password to modify it. After the modification is completed, the new password will be stored for next verification.

【6】Status display: Display relevant status information on LCD1602 in real time, such as incorrect password input prompts, successful password modification prompts, etc.

【7】System protection: In order to protect system security, you can set security policies, such as limit on the number of incorrect password inputs, lock time, etc. When the upper limit of the number of errors is reached or the lock time is reached, the system will automatically perform corresponding protection processing.

【8】Interrupt service: Use timer interrupts and other methods to perform key detection and LCD1602 refresh operations to improve the real-time performance of the system.

[9] Loop detection: Design a main loop function to continuously detect the key input of the matrix keyboard, perform functions such as unlocking, password verification, password modification, and status display.

4. Project code

#include <reg51.h>
#include <intrins.h>

#define PASSWORD_LENGTH 4 // Password length
#define MAX_ATTEMPTS 3 // Maximum number of attempts

sbit Buzzer = P1^0; // Buzzer control pin
sbit ElectromagneticLock = P1^1; // Electromagnetic lock control pin

unsigned char password[PASSWORD_LENGTH] = {<!-- -->1, 2, 3, 4}; // Initial password
unsigned char enteredPassword[PASSWORD_LENGTH]; // Entered password
unsigned char attempts = 0; // Number of attempts

//delay function
void delay(unsigned int ms) {<!-- -->
    unsigned int i, j;
    for (i = 0; i < ms; i + + )
        for (j = 0; j < 110; j + + )
            ;
}

//Initialize LCD1602
void LCD_Init() {<!-- -->
    LCD_WriteCommand(0x38); //Set 8-bit data bus, 2-line display, 5x8 dot matrix characters
    LCD_WriteCommand(0x0c); //Display on, cursor off
    LCD_WriteCommand(0x06); //The cursor moves to the right but the characters do not move
    LCD_WriteCommand(0x01); // Clear screen
}

//Write command to LCD1602
void LCD_WriteCommand(unsigned char cmd) {<!-- -->
    LCD_RS = 0;
    LCD_RW = 0;
    LCD_EN = 1;
    P0 = cmd;
    _nop_();
    _nop_();
    LCD_EN = 0;
}

//Write data to LCD1602
void LCD_WriteData(unsigned char dat) {<!-- -->
    LCD_RS = 1;
    LCD_RW = 0;
    LCD_EN = 1;
    P0 = dat;
    _nop_();
    _nop_();
    LCD_EN = 0;
}

// Display string on LCD1602
void LCD_ShowString(unsigned char x, unsigned char y, unsigned char *str) {<!-- -->
    unsigned char i = 0;
    if (x < 16) {<!-- -->
        if(y==0)
            LCD_WriteCommand(0x80 + x);
        else if (y == 1)
            LCD_WriteCommand(0xc0 + x);
        while (str[i] != '\0') {<!-- -->
            LCD_WriteData(str[i]);
            i + + ;
        }
    }
}

//Initialize matrix keyboard
void Keypad_Init() {<!-- -->
    Keypad_Row1 = 1;
    Keypad_Row2 = 1;
    Keypad_Row3 = 1;
    Keypad_Row4 = 1;
}

//Read matrix keyboard key value
unsigned char Keypad_Read() {<!-- -->
    unsigned char row, col;
    unsigned char keyVal;

    for (col = 0; col < 4; col + + ) {<!-- -->
        Keypad_Col1 = 1;
        Keypad_Col2 = 1;
        Keypad_Col3 = 1;
        Keypad_Col4 = 1;
        switch (col) {<!-- -->
            case 0:
                Keypad_Col1 = 0;
                break;
            case 1:
                Keypad_Col2 = 0;
                break;
            case 2:
                Keypad_Col3 = 0;
                break;
            case 3:
                Keypad_Col4 = 0;
                break;
        }
        for (row = 0; row < 4; row + + ) {<!-- -->
            if (Keypad_Row1 == 0) {<!-- -->
                delay(5);
                if (Keypad_Row1 == 0) {<!-- -->
                    while (Keypad_Row1 == 0)
                        ;
                    keyVal = row * 4 + col + 1;
                    return keyVal;
                }
            }
            if (Keypad_Row2 == 0) {<!-- -->
                delay(5);
                if (Keypad_Row2 == 0) {<!-- -->
                    while (Keypad_Row2 == 0)
                        ;
                    keyVal = row * 4 + col + 5;
                    return keyVal;
                }
            }
            if (Keypad_Row3 == 0) {<!-- -->
                delay(5);
                if (Keypad_Row3 == 0) {<!-- -->
                    while (Keypad_Row3 == 0)
                        ;
                    keyVal= row * 4 + col + 9;
                    return keyVal;
                }
            }
            if (Keypad_Row4 == 0) {<!-- -->
                delay(5);
                if (Keypad_Row4 == 0) {<!-- -->
                    while (Keypad_Row4 == 0)
                        ;
                    keyVal = row * 4 + col + 13;
                    return keyVal;
                }
            }
        }
    }

    return 0xFF; // Returning 0xFF means no button is pressed
}

// Check whether the entered password is consistent with the set password
bit CheckPassword() {<!-- -->
    unsigned char i;
    for (i = 0; i < PASSWORD_LENGTH; i + + ) {<!-- -->
        if (enteredPassword[i] != password[i])
            return 0; // Passwords are inconsistent
    }
    return 1; // Passwords are consistent
}

// enter password
bit EnterPassword() {<!-- -->
    unsigned char i;
    unsigned char key;
    for (i = 0; i < PASSWORD_LENGTH; i + + ) {<!-- -->
        while ((key = Keypad_Read()) == 0xFF)
            ;
        enteredPassword[i] = key;
        LCD_WriteData('*');
        delay(300);
    }
    return CheckPassword();
}

// change Password
void ChangePassword() {<!-- -->
    unsigned char i;
    LCD_ShowString(0, 1, "Enter New Password");
    for (i = 0; i < PASSWORD_LENGTH; i + + ) {<!-- -->
        while ((enteredPassword[i] = Keypad_Read()) == 0xFF)
            ;
        LCD_WriteData('*');
        delay(300);
    }
    for (i = 0; i < PASSWORD_LENGTH; i + + )
        password[i] = enteredPassword[i];
    LCD_ShowString(0, 1, "Password Changed ");
    delay(1000);
    LCD_ShowString(0, 1, "Enter Password: ");
}

// Unlock
void Unlock() {<!-- -->
    LCD_ShowString(0, 1, "Unlocking...");
    Buzzer = 1; // Make a beep
    ElectromagneticLock = 0; // Unlocked state
    delay(2000);
    Buzzer = 0; // Turn off the buzzer
    ElectromagneticLock = 1; // Locked state
    LCD_ShowString(0, 1, "Enter Password: ");
}

// main function
void main() {<!-- -->
    LCD_Init(); //Initialize LCD1602
    Keypad_Init(); // Initialize matrix keyboard

    LCD_ShowString(0, 0, "Electronic Lock");
    LCD_ShowString(0, 1, "Enter Password: ");

    while (1) {<!-- -->
        if (EnterPassword()) {<!-- -->
            Unlock(); // Password is correct, unlock
            attempts = 0; // Clear the number of attempts
        } else {<!-- -->
            attempts + + ; // Increase the number of attempts by one
            if (attempts >= MAX_ATTEMPTS) {<!-- -->
                LCD_ShowString(0, 1, "Max Attempts Exceeded");
                Buzzer = 1; // Sound the alarm sound
                delay(2000);
                Buzzer = 0; // Turn off the alarm sound
                attempts = 0; // Clear the number of attempts
            } else {<!-- -->
                LCD_ShowString(0, 1, "Wrong Password ");
                delay(1000);
                LCD_ShowString(0, 1, "Enter Password: ");
            }
        }

        while ((Keypad_Read()) != 0xFF)
            ; // Wait for the key to be released

        if (Keypad_Read() == '#') {<!-- -->
            ChangePassword(); // Enter '#' to enter password change mode
        }
    }
}