搜索代码

Build a Simple Electronic Lock with Keypad and LCD Using Arduino

Build a Simple Electronic Lock with Keypad and LCD Using Arduino

In this tutorial, you’ll learn how to create an electronic lock system using a 4x3 keypad, an I2C LCD, and an Arduino. The lock opens when the correct 4-digit PIN code is entered and automatically closes after a few seconds. The system is ideal for DIY lockers, cabinet doors, or simple security mechanisms.

This project provides visual feedback using an LCD display and requires only basic components, making it perfect for beginners. The video demonstration shows the full operation—from code entry to unlocking and auto-relocking.

Components Used

  • Arduino Uno (or compatible)

  • 4x3 Matrix Keypad

  • I2C 16x2 LCD Display

  • Relay Module (or digital pin output)

  • Jumper wires and breadboard

Wiring Instructions

  • Keypad connected to digital pins 2 through 8

  • I2C LCD connected via SDA (A4) and SCL (A5) on Arduino Uno

  • Lock control (e.g. relay or LED) on digital pin 13

📷 Image Caption: Wiring diagram showing 4x3 keypad wired to pins 2–8, I2C LCD connected to SDA/SCL, and digital output pin 13 controlling a relay.


How the Code Works

The system uses the Keypad and LiquidCrystal_I2C libraries to read user input and display messages. The correct passcode is hardcoded as:

char correctCode[5] = "0753";

When a user presses digits, they are appended to a buffer. Upon pressing #, the system compares the input with the stored code:

if (strcmp(code, correctCode) == 0) {
  // Unlock
}


If the code is correct:

  • The LCD displays "Door Open"

  • Pin 13 goes HIGH to trigger the relay

  • After 5 seconds, the door closes automatically

If the code is incorrect:

  • The LCD shows "Wrong Code"

  • User is prompted to try again

The LCD also shows what’s being entered and gives clear feedback after each attempt.

📚 Chapter Index

  1. [00:00] Introduction and Demo

  2. [00:06] PIN Entry and Unlocking

  3. [00:16] Auto-Lock After Delay

  4. [00:22] Handling Wrong Input

  5. [00:28] Overview and Welcome from Robojax


This is a great foundational project in embedded security. Customize the passcode, display, or output behavior to suit your specific needs. The source code and required libraries are provided below this article. Watch the video to see the full demonstration and assembly process!




370-Project: Electronic lock with keypad, code, and LCD
语言: C++
/*
Keypad with LCD to control electronic lock
watch full video:  https://youtu.be/PRIXqdjhQhg
View resouce page for this project: https://robojax.com/tutorial_view.php?id=389
written on Feb 07, 2025
www.Robojax.com

*/
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>

char text_correct[] = "Correct"; 
char text_wrong[] = "Wrong Code"; 
char text_code[] = "Code:"; 
char text_entered[] = "Entered:"; 
char text_open[] = "Door Open"; 

// Define keypad rows and columns
const byte ROWS = 4;
const byte COLS = 3;

// Define keypad keys
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

// Define keypad pins
byte rowPins[ROWS] = {2, 3, 4, 5}; // Connect to Arduino pins
byte colPins[COLS] = {6, 7, 8};  // Connect to Arduino pins
const int relayPIn =10;
// Create keypad object
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

// Create LCD object (adjust address if needed)
LiquidCrystal_I2C lcd(0x3f, 16, 2);

String INPUT_KEY = ""; // Now a String
String THE_KEY = "0753"; // Example, change as needed (only digits)
int timeToKeepOpen = 5000;
String enteredNumber = "";
bool inputComplete = false;

void setup() {
  Serial.begin(9600);
  pinMode(relayPIn, OUTPUT);//define a pin for relay to control the lock
  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.print(text_code); // Display "Code:"
}

void loop() {
  digitalWrite(relayPIn, LOW);//turn the relay OFF
  char key = keypad.getKey();

  if (key) {
    switch (key) {
      case '#':
        if (enteredNumber.length() > 0) {
          bool isValidNumber = true;
          for (char c : enteredNumber) {
            if (c < '0' || c > '9') {
              isValidNumber = false;
              break;
            }
          }

          if (isValidNumber) {
            INPUT_KEY = enteredNumber; // Assign the entered number string
            inputComplete = true;
          } else {
            lcd.clear();
            lcd.print("Invalid Input!");
            delay(2000);
            lcd.clear();
            lcd.print(text_code);
            enteredNumber = "";
            break;
          }

          lcd.clear();
          lcd.print(text_entered);
          lcd.print(INPUT_KEY); // Print the String
          Serial.print("Entered Number: ");
          Serial.println(INPUT_KEY);

          if (INPUT_KEY == THE_KEY && inputComplete) { // String comparison
            action();
          } else {
            lcd.setCursor(0, 1);
            lcd.print(text_wrong);
          }

          enteredNumber = "";
          inputComplete = false;
          delay(2000);
          lcd.clear();
          lcd.print(text_code);

        }
        break;

      case '*':
        if (enteredNumber.length() > 0) {
          enteredNumber.remove(enteredNumber.length() - 1);
          lcd.clear();
          lcd.print(text_code);
          lcd.print(enteredNumber);
        }
        break;

      default:
        if (enteredNumber.length() < 4) {
          enteredNumber += key;
          lcd.clear();
          lcd.print(text_code);
          lcd.print(enteredNumber);
        }
        break;
    }
  }
}

void action() {
  // Put your code here to be executed when INPUT_KEY matches THE_KEY
  lcd.clear();
  lcd.print(text_correct); // Display "Correct"
  Serial.println("Correct Key!");
  digitalWrite(relayPIn, HIGH);
  delay(timeToKeepOpen);
  lcd.setCursor (0,1);
  lcd.print(text_open); // Display "Door Open" (or your custom message)

}

资源与参考

尚无可用资源。

文件📁

其他文件