A real lock you can build on your desk. Type the secret code on the keypad and press #, and the servo swings the latch open. Press * to lock it again. You can fit it to a small box made of card or wood to protect your treasures.
What you need
- UNO R3 board and USB cable (UNO R3 Starter Kit)
- 4×4 membrane keypad (UNO R3 Starter Kit)
- Servo motor (UNO R3 Starter Kit)
- 11 jumper wires
Wiring

| From | To |
|---|---|
| Keypad R1, R2, R3, R4 | UNO pins 9, 8, 7, 6 |
| Keypad C1, C2, C3, C4 | UNO pins 5, 4, 3, 2 |
| Servo signal (orange) | UNO pin 10 |
| Servo power (red) | UNO 5V |
| Servo ground (brown) | UNO GND |
Build it step by step
- Install the Keypad library by Mark Stanley and Alexander Brevig (Tools → Manage Libraries).
- Plug the 8 keypad wires into pins 9 to 2, in the order shown.
- Connect the servo to pin 10, 5V and GND.
- Upload the code. The servo moves to the locked position (0°).
- Type 1234 and press # to unlock. Press * to lock again.
- Optional: glue a small arm to the servo horn so it blocks the lid of a box when locked.
The code
#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 latch;
const int SERVO_PIN = 10;
const int LOCKED_ANGLE = 0;
const int OPEN_ANGLE = 90;
const String SECRET = "1234";
String typed = "";
int wrongTries = 0;
void lockLatch() {
latch.write(LOCKED_ANGLE);
Serial.println("Locked");
}
void setup() {
Serial.begin(9600);
latch.attach(SERVO_PIN);
lockLatch();
Serial.println("Enter the code, then press #");
}
void loop() {
char key = keypad.getKey();
if (!key) return;
if (key == '*') {
typed = "";
lockLatch();
} else if (key == '#') {
if (typed == SECRET) {
latch.write(OPEN_ANGLE);
Serial.println("Unlocked!");
wrongTries = 0;
} else {
wrongTries++;
Serial.print("Wrong code (");
Serial.print(wrongTries);
Serial.println(")");
if (wrongTries >= 3) {
Serial.println("Too many tries, wait 10 seconds");
delay(10000);
wrongTries = 0;
}
}
typed = "";
} else {
typed += key;
Serial.print("*");
}
}
How it works
The Keypad library scans the 4 rows and 4 columns to find which key is pressed. Each key is added to typed until you press #, and then the sketch compares it with SECRET. A correct code moves the servo to 90°, which opens the latch. After three wrong codes the lock waits 10 seconds, which makes guessing slower.
Make it your own
- Change
SECRETto your own code. Letters like A to D work too. - Add an LED or the active buzzer from the sensor kit to signal a wrong code.
- Adjust
LOCKED_ANGLEandOPEN_ANGLEto fit your box.
Troubleshooting
- The servo jitters or the board resets: use a good USB port, or power the servo from a separate 5 V supply with its GND connected to the UNO's GND.
-
Wrong keys appear: check the keypad wire order, or swap the numbers in
rowPinsandcolPins.
Learn the basics first
New to these parts? These lessons explain them step by step: Read a 4×4 keypad and Control a servo motor.
0 comments