Keypad code lock with a servo latch

Type the secret code on a 4×4 keypad to swing a servo latch open, then press * to lock it again.
  • Level:Intermediate
  • Duration: 1 h
  • Ages 12+
  • Platform: Arduino
Community project EEMENTOR team
Keypad code lock with a servo latch – EEMENTOR STEM™ project

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

Breadboard wiring diagram: Keypad code lock with a servo latch
Breadboard view. Check each connection against the wiring table below.
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
FREE

Build the keypad part of this project in EEMENTOR Lab

Wire it yourself in your browser before you touch a real wire. No sign-up, nothing to install.

The parts for this circuit laid out in EEMENTOR Lab, ready to wireBuild it yourself
  1. Wire the parts using the wiring table above. Pins with no wire show red.
  2. Press Run, hold the mouse on a key, and watch the Serial monitor.
  3. Want the whole project? Add the other parts from the parts list, wire them from the table, then paste this lesson’s sketch into the Code tab and press Compile & Run.

EEMENTOR Lab is free and in beta. Results are a guide; a real build can differ slightly. About the Lab

Build it step by step

  1. Install the Keypad library by Mark Stanley and Alexander Brevig (Tools → Manage Libraries).
  2. Plug the 8 keypad wires into pins 9 to 2, in the order shown.
  3. Connect the servo to pin 10, 5V and GND.
  4. Upload the code. The servo moves to the locked position (0°).
  5. Type 1234 and press # to unlock. Press * to lock again.
  6. 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 SECRET to 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_ANGLE and OPEN_ANGLE to 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 rowPins and colPins.

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

Leave a comment

Please note, comments need to be approved before they are published.

More projects

  • Pulse visualiser

    • Level:Intermediate
    • Duration: 45 min
    • Platform: Arduino

    by EEMENTOR team

  • Flame alarm

    • Level:Beginner
    • Duration: 40 min
    • Platform: Arduino

    by EEMENTOR team

Community projects

Built something awesome? Share it.

Send us your project and we'll feature it in the hub, credited to you, so other makers can learn from it.

  1. Sign in and tell us about your build
  2. Our team reviews your submission
  3. We publish it in the hub, credited to you