17 / 20
Keypad door lock
Scan a 3 × 4 keypad and open a servo lock with the right code.
Run this experiment →Why it’s here
Enter 1234 and # to unlock; use * to clear and try a wrong code.
How it works
Scan a 3 × 4 keypad and open a servo lock with the right code. Four rows and three columns form twelve intersections. Scanning identifies the closed intersection. This guided model targets the original ESP32 and Arduino-ESP32 3.x; radio, acoustics, loads and mechanics are simplified.
Try it
- Open this experiment
- Enter 1234 and # to unlock; use * to clear and try a wrong code.
Connections
- 13,14,27,32 / 25,26,33 — Four rows / three columns
- GPIO 18 — Latch servo signal
Example sketch
#include <Keypad.h>
#include <ESP32Servo.h>
char keys[4][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'},{'*','0','#'}};
byte rows[4]={13,14,27,32}, columns[3]={25,26,33};
Keypad keypad=Keypad(makeKeymap(keys),rows,columns,4,3);
Servo latch; String entry; unsigned long unlockedAt=0;
// Educational lock; not an access-control security product.
void setup() {
latch.setPeriodHertz(50); latch.attach(18, 500, 2400); latch.write(0);
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '*') { entry = ""; latch.write(0); }
else if (key == '#') {
bool valid = entry == "1234"; latch.write(valid ? 90 : 0);
unlockedAt = valid ? millis() : 0; entry = "";
} else if (entry.length() < 8) entry += key;
}
if (unlockedAt && millis() - unlockedAt > 3000) { latch.write(0); unlockedAt = 0; }
}Check your understanding
Why do twelve keys need only seven wires?
Show the answer
Four rows and three columns form twelve intersections. Scanning identifies the closed intersection.