balli2000in
Published © GPL3+

DIY Smart Lock: Arduino + Rack & Pinion + Keypad

Password-protected door lock with 3D printed bolt mechanism, keypad input, LCD display & servo actuation — all on Arduino UNO.

IntermediateProtip2 hours85
DIY Smart Lock: Arduino + Rack & Pinion + Keypad

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
I2C 16x2 Arduino LCD Display Module
DFRobot I2C 16x2 Arduino LCD Display Module
×1
4x4 Keypad
×1
Buzzer
Buzzer
×1
5 mm LED: Red
5 mm LED: Red
×1
LED, Green
LED, Green
×1
Resistor 330 ohm
Resistor 330 ohm
×2
SG90 Micro-servo motor
SG90 Micro-servo motor
×1

Software apps and online services

Arduino IDE
Arduino IDE
Solid Edge 2025 Community Edition

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free
Solder Flux, Soldering
Solder Flux, Soldering

Story

Read more

Custom parts and enclosures

3D Encloser Files

Schematics

Circuit Diagram for this Project

Code

Final_Working_code_Smart_lock.ino

C/C++
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <Keypad.h>

// --- LCD Configuration ---
LiquidCrystal_I2C lcd(0x27, 16, 2);

// --- Servo ---
Servo lockServo;

// --- Keypad Configuration ---
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] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 9, 8};  // Note: matches first code's pin order

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

// --- Pin Definitions ---
#define RED_LED    12
#define GREEN_LED  13
#define SERVO_PIN  10
#define BUZZER     11

// --- Password Logic ---
String correctPassword = "1234";
String enteredPassword = "";

void setup() {
  Serial.begin(9600);

  // Initialize LCD
  lcd.init();
  lcd.backlight();

  // Initialize pins
  pinMode(RED_LED,   OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(BUZZER,    OUTPUT);

  // Initialize Servo
  lockServo.attach(SERVO_PIN);
  lockServo.write(90);  // Locked position

  // Default state: locked
  digitalWrite(RED_LED,   HIGH);
  digitalWrite(GREEN_LED, LOW);

  // --- Startup Test (from first code) ---
  lcd.print("System Testing");

  // LED Test
  lcd.setCursor(0, 1);
  lcd.print("Testing LEDs... ");
  digitalWrite(RED_LED, HIGH);  delay(500);
  digitalWrite(RED_LED, LOW);
  digitalWrite(GREEN_LED, HIGH); delay(500);
  digitalWrite(GREEN_LED, LOW);

  // Buzzer Test
  lcd.clear();
  lcd.print("Buzzer: BEEP");
  digitalWrite(BUZZER, HIGH); delay(150);
  digitalWrite(BUZZER, LOW);

  // Servo Test
  lcd.clear();
  lcd.print("Servo: Testing");
  lockServo.write(0); delay(1000);
  lockServo.write(90);  delay(1000);

  lcd.clear();
  lcd.print("Test Complete!");
  delay(1000);

  // Set locked state and show prompt
  digitalWrite(RED_LED, HIGH);
  resetSystem();

  Serial.println("--- System Online: Waiting for Input ---");
}

void loop() {
  char key = keypad.getKey();  // Uses Keypad library (no manual scan needed)

  if (key) {
    Serial.print(key);

    if (key == '#') {
      Serial.println("\n--- SUBMIT BUTTON PRESSED ---");
      processPassword();
    }
    else if (key == '*') {
      Serial.println("\n--- CLEAR BUTTON PRESSED ---");
      resetSystem();
    }
    else {
      if (enteredPassword.length() < 8) {
        enteredPassword += key;

        // Show asterisk for each character
        lcd.setCursor(enteredPassword.length() - 1, 1);
        lcd.print('*');

        Serial.print("Typed: ");        Serial.print(key);
        Serial.print(" | Buffer: ");    Serial.println(enteredPassword);

        // Key press beep
        digitalWrite(BUZZER, HIGH); delay(50);
        digitalWrite(BUZZER, LOW);
      }
    }
  }
}

void processPassword() {
  Serial.print("Entered: [");   Serial.print(enteredPassword); Serial.println("]");
  Serial.print("Expected: [");  Serial.print(correctPassword); Serial.println("]");

  lcd.clear();
  enteredPassword.trim();

  if (enteredPassword == correctPassword) {
    Serial.println(">>> RESULT: MATCH FOUND! <<<");
    lcd.print("Access Granted!");

    digitalWrite(RED_LED,   LOW);
    digitalWrite(GREEN_LED, HIGH);

    lockServo.write(0);  // Unlock
    delay(3000);
    lockServo.write(90);   // Lock again

    digitalWrite(GREEN_LED, LOW);
    digitalWrite(RED_LED,   HIGH);
  }
  else {
    Serial.println(">>> RESULT: NO MATCH! <<<");
    lcd.print("Wrong Password!");

    for (int i = 0; i < 4; i++) {
      digitalWrite(BUZZER, HIGH); delay(150);
      digitalWrite(BUZZER, LOW);  delay(150);
    }
  }

  resetSystem();
}

void resetSystem() {
  enteredPassword = "";
  lcd.clear();
  lcd.print("Enter Password:");
  lcd.setCursor(0, 1);
}

Credits

balli2000in
3 projects • 0 followers

Comments