Illustration: a wooden box with a keypad, a servo latch inside and a lit green LED beside it.Home and IoT
Illustration (made with AI): roughly what the finished project looks like.

A Code Lock with a Keypad

Type the secret code and press #: with the right code the green light comes on and a servo slides the bolt open, with the wrong one the buzzer beeps three times.

Difficulty
Intermediate
Time
1 h
Age
12+
Parts
7, 7 on the site

Written by the RoboHub team. A question? Write to us.

Variations

Choose a variation and the parts list updates.

Instructions

  1. Install the library

    Find and install “Keypad” (Mark Stanley, Alexander Brevig) in the Library Manager.

  2. Connect the keypad

    The keypad has eight pins. The first four from the left are the rows: connect them to pins 9, 8, 7 and 6. The other four are the columns: pins 5, 4, 3 and 2.

    Of the keypad's eight pins, the first four from the left (R1 to R4) are the rows and the other four (C1 to C4) the columns. D2 is the board's digital pin 2, A0 is an analogue pin, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • 4×4 keypad R1 → Arduino D9
    • 4×4 keypad R2 → Arduino D8
    • 4×4 keypad R3 → Arduino D7
    • 4×4 keypad R4 → Arduino D6
    • 4×4 keypad C1 → Arduino D5
    • 4×4 keypad C2 → Arduino D4
    • 4×4 keypad C3 → Arduino D3
    • 4×4 keypad C4 → Arduino D2
  3. Servo, lights and buzzer

    Connect the servo's orange wire to pin 10, red to 5V and brown to ground. The green LED goes to A0 and the red one to A1 (both through 330 Ω resistors), and the buzzer's “+” to A2. The LEDs' short legs and the buzzer's “−” go to ground. The analogue pins work as ordinary digital pins too.

    Of the keypad's eight pins, the first four from the left (R1 to R4) are the rows and the other four (C1 to C4) the columns. D2 is the board's digital pin 2, A0 is an analogue pin, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • SG90 servo SIG → Arduino D10
    • SG90 servo + → Arduino 5V
    • SG90 servo − → Arduino GND
    • Green LED + → 330 ΩArduino A0
    • Green LED − → Arduino GND
    • Red LED + → 330 ΩArduino A1
    • Red LED − → Arduino GND
    • Buzzer + → Arduino A2
    • Buzzer − → Arduino GND
  4. Upload the code

    The secret code is on the CODE line, 1234 to begin with. * clears what you typed, # checks it. The part starting with #ifdef is for the Uno R4, where the Keypad library can't switch on the internal resistors by itself.

    Arduino C++
    #include <Keypad.h>
    #include <Servo.h>
    
    const byte ROWS = 4, 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};
    #ifdef ARDUINO_API_VERSION             // Uno R4: switch the keypad's pull-ups on the R4 way
    #undef pinMode
    class KeypadR4 : public Keypad {
     public:
      using Keypad::Keypad;
      void pin_mode(byte pin, byte mode) override { pinMode(pin, (PinMode)mode); }
    };
    KeypadR4 keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
    #else
    Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
    #endif
    
    const String CODE = "1234";            // your secret code
    const int SERVO = 10, GREEN = A0, RED = A1, BUZZER = A2;
    const int LOCKED = 0, UNLOCKED = 90;   // bolt angles
    Servo bolt;
    String typed = "";
    
    void setup() {
      pinMode(GREEN, OUTPUT);
      pinMode(RED, OUTPUT);
      bolt.attach(SERVO);
      bolt.write(LOCKED);
      digitalWrite(RED, HIGH);
    }
    
    void loop() {
      char key = keypad.getKey();
      if (!key) return;
      tone(BUZZER, 2000, 30);              // a click for every key
      if (key == '*') { typed = ""; return; }      // * clears
      if (key != '#') { typed += key; return; }    // # checks the code
    
      if (typed == CODE) {
        digitalWrite(RED, LOW);
        digitalWrite(GREEN, HIGH);
        bolt.write(UNLOCKED);
        tone(BUZZER, 2500, 300);
        delay(5000);                       // open for 5 seconds
        bolt.write(LOCKED);
        digitalWrite(GREEN, LOW);
        digitalWrite(RED, HIGH);
      } else {
        for (int i = 0; i < 3; i++) { tone(BUZZER, 400, 150); delay(250); }
      }
      typed = "";
    }
  5. The bolt

    Attach a small bolt to the servo arm: at 0 degrees it locks the door, at 90 it lets go. If it works the other way round on your box, swap the LOCKED and UNLOCKED numbers.

  • Sensors
    Beginner1 h10+

    Parking Sensor with Sound and Lights

    An ultrasonic sensor measures the distance. Three LEDs show how close the obstacle is, and the buzzer beeps faster the closer you get.

    6/6 parts on the site
  • Home and IoT
    Beginner30 min10+

    A Motion Alarm

    A PIR sensor notices a warm body moving. As soon as someone walks into the room, a siren sounds and a red light flashes.

    6/6 parts on the site
  • Home and IoT
    Beginner1 h10+

    A Smart Bin

    Bring your hand close and the lid lifts by itself. An ultrasonic sensor notices the hand, and a servo opens the lid and closes it three seconds after the hand has gone.

    5/5 parts on the site