FluxBench.AIShared project · read-only

Keypad Door Lock

ESP32 DevKit (WROOM-32)·Arduino C++·Updated Jul 27, 2026·by FluxBench TeamIncludes Lab + Schematic + PCB

Code-entry door lock: a 4×4 membrane keypad drives an SG90 servo latch — type 2468 then # and the latch swings open for 5 seconds, with buzzer feedback on every key and a lock-state LED. In the Lab simulator you can click the keypad keys live. Bundled Lab bench, schematic and routed PCB included.

sketch.ino
// Keypad Door Lock — 4×4 keypad + servo latch + buzzer + status LED
// Wiring (matches the Lab bench, Schematic and PCB tabs):
//   Keypad rows R1-R4 = GPIO 13, 12, 14, 27; cols C1-C4 = GPIO 26, 25, 33, 32
//   Servo latch on GPIO18 (5 V), piezo buzzer on GPIO19, lock LED on GPIO2
// Enter 2468 then # to unlock — the latch re-locks itself after 5 seconds.
// * clears the entry. In the Lab, click the keypad keys while running.

#include <Keypad.h>
#include <ESP32Servo.h>

const byte ROWS = 4, COLS = 4;
char keys[4][4] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[4] = {13, 12, 14, 27};
byte colPins[4] = {26, 25, 33, 32};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

Servo latch;
char secret[8] = "2468";
char entry[8] = "";
int idx = 0;
bool unlocked = false;
unsigned long unlockedAt = 0;

void setLock(bool open) {
  unlocked = open;
  latch.write(open ? 90 : 0);       // 90° = latch open, 0° = locked
  digitalWrite(2, open ? HIGH : LOW);
  if (open) Serial.println(">> UNLOCKED (relocks in 5 s)");
  else Serial.println(">> LOCKED");
}

void setup() {
  Serial.begin(115200);
  pinMode(2, OUTPUT);
  latch.attach(18);
  setLock(false);
  Serial.println("Enter code then # (secret: 2468, * clears)");
}

void loop() {
  char k = keypad.getKey();
  if (k) {
    tone(19, 880, 30);              // key click
    if (k == '#') {
      if (strcmp(entry, secret) == 0) {
        tone(19, 1760, 200);        // happy beep
        setLock(true);
        unlockedAt = millis();
      } else {
        tone(19, 220, 400);         // sad buzz
        Serial.print("Access denied: ");
        Serial.println(entry);
      }
      idx = 0;
      entry[0] = 0;
    } else if (k == '*') {
      idx = 0;
      entry[0] = 0;
      Serial.println("(cleared)");
    } else if (idx < 7) {
      entry[idx] = k;
      idx++;
      entry[idx] = 0;
      Serial.print("entry: ");
      Serial.println(entry);
    }
  }
  if (unlocked && millis() - unlockedAt > 5000) {
    setLock(false);                 // auto re-lock
  }
  delay(20);
}

Built with FluxBench.AI — the AI workbench for embedded projects.