0
0
AutocadHow-ToBeginner · 4 min read

Arduino Project for Password Door Lock: Simple Code & Guide

An Arduino password door lock uses a Keypad to enter a code and a Servo motor to control the lock. The Arduino checks the entered password and opens the door by turning the servo if the password is correct.
📐

Syntax

This project uses the Keypad library to read password input and the Servo library to control the lock mechanism. The main parts are:

  • Keypad: Reads button presses from a matrix keypad.
  • Servo: Controls the lock by rotating to open or close positions.
  • password[]: Stores the correct password sequence.
  • inputPassword[]: Stores the user's entered password.
  • checkPassword(): Compares entered password with the correct one.
arduino
#include <Keypad.h>
#include <Servo.h>

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns

char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; // Connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

Servo lockServo;

char password[5] = {'1','2','3','4', '\0'}; // Correct password
char inputPassword[5]; // Stores user input
byte inputIndex = 0;

bool checkPassword() {
  for (byte i = 0; i < 4; i++) {
    if (inputPassword[i] != password[i]) {
      return false;
    }
  }
  return true;
}
💻

Example

This example shows a complete Arduino sketch for a password door lock. It reads keypad input, checks the password, and moves a servo to unlock or lock the door.

arduino
#include <Keypad.h>
#include <Servo.h>

const byte ROWS = 4;
const byte COLS = 4;

char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

Servo lockServo;

char password[5] = {'1','2','3','4', '\0'};
char inputPassword[5];
byte inputIndex = 0;

void setup() {
  Serial.begin(9600);
  lockServo.attach(10); // Servo connected to pin 10
  lockServo.write(0); // Lock closed position
  Serial.println("Enter 4-digit password:");
}

bool checkPassword() {
  for (byte i = 0; i < 4; i++) {
    if (inputPassword[i] != password[i]) {
      return false;
    }
  }
  return true;
}

void loop() {
  char key = keypad.getKey();
  if (key) {
    if (key == '#') { // # to submit password
      if (inputIndex == 4) {
        if (checkPassword()) {
          Serial.println("Password correct. Door unlocked.");
          lockServo.write(90); // Open door
          delay(5000); // Keep door open for 5 seconds
          lockServo.write(0); // Lock door again
          Serial.println("Door locked.");
        } else {
          Serial.println("Wrong password. Try again.");
        }
      } else {
        Serial.println("Enter 4 digits before submitting.");
      }
      inputIndex = 0; // Reset input
    } else if (key == '*') { // * to clear input
      inputIndex = 0;
      Serial.println("Input cleared.");
    } else if (inputIndex < 4) {
      inputPassword[inputIndex] = key;
      inputIndex++;
      Serial.print("Digit entered: ");
      Serial.println(key);
    }
  }
}
Output
Enter 4-digit password: Digit entered: 1 Digit entered: 2 Digit entered: 3 Digit entered: 4 Password correct. Door unlocked. Door locked.
⚠️

Common Pitfalls

Common mistakes include:

  • Not resetting the input index after a wrong or correct password attempt, causing input errors.
  • Forgetting to attach the servo to the correct pin or not initializing it properly.
  • Not debouncing keypad inputs, which can cause multiple readings of one key press.
  • Using a password length different from the input buffer size, leading to incorrect checks.

Always clear the input after each attempt and verify wiring carefully.

arduino
/* Wrong way: Not resetting inputIndex after wrong password */
if (checkPassword()) {
  // unlock
} else {
  Serial.println("Wrong password.");
  // inputIndex not reset here causes errors
}

/* Right way: Reset inputIndex after every attempt */
if (checkPassword()) {
  // unlock
}
Serial.println("Attempt done.");
inputIndex = 0; // Reset for next input
📊

Quick Reference

Tips for building an Arduino password door lock:

  • Use a 4x4 matrix keypad for easy input.
  • Store the password in a fixed-size array.
  • Use # to submit and * to clear input.
  • Control a servo motor to lock/unlock the door.
  • Print messages to Serial Monitor for feedback.

Key Takeaways

Use a keypad and servo with Arduino to create a password door lock.
Check the entered password against a stored code before unlocking.
Reset input after each attempt to avoid errors.
Use '#' to submit and '*' to clear password input.
Provide user feedback via Serial Monitor for easier debugging.