CYTHEIA AIOpen the editor

Keypad Code Lock

Type a 4-digit code on a keypad and press # to unlock: a servo opens the latch and an LED lights for three seconds. Press * to start over.

Open this circuit and run it

Opens in your browser with the parts, wiring and code already in place. Press Run to watch it work, then change anything you like.

How it works

A 4×4 keypad has 16 keys but only eight wires: four rows and four columns. Pressing a key connects its row to its column, and the Keypad library finds which one by driving the rows one at a time and checking the columns. Here the rows are on pins 9 to 6 and the columns on pins 5 to 2. A servo on pin 10 acts as the latch, and an LED on pin 13 shows when the lock is open.

keypad.getKey() returns the key that was pressed, or nothing. Digits are collected into one number: each new digit multiplies what you have so far by 10 and adds the new digit (key - ‘0’ turns the character into its value), up to four digits, printing a * for each. Pressing # checks the number against the secret, 1234. The right code turns the servo to 90 degrees and lights the LED for three seconds before the servo returns to 0; a wrong one prints “Wrong code”. Either way the entry is cleared, and * clears it at any time. In the editor, click and hold a key to press it.

Parts

Wiring

Arduino code

// Keypad Code Lock
// Digits build up the code, # checks it, * clears it. The right code (1234)
// swings the servo latch open and lights the LED for three seconds.
// Click and hold a key on the keypad to press it.
#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 SECRET = 1234;
int entered = 0;     // the digits typed so far, as one number
int digits = 0;      // how many digits that is

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  latch.attach(10);
  latch.write(0);    // locked
  Serial.println("Enter the code, then press #");
}

void loop() {
  char key = keypad.getKey();
  if (key) {
    if (key >= '0' && key <= '9') {
      if (digits < 4) {
        entered = entered * 10 + (key - '0');
        digits++;
        Serial.print('*');
      }
    } else if (key == '#') {
      if (entered == SECRET) {
        Serial.println(" Unlocked!");
        latch.write(90);
        digitalWrite(13, HIGH);
        delay(3000);
        latch.write(0);
        digitalWrite(13, LOW);
        Serial.println("Locked again");
      } else {
        Serial.println(" Wrong code");
      }
      entered = 0;
      digits = 0;
    } else if (key == '*') {
      entered = 0;
      digits = 0;
      Serial.println(" Cleared");
    }
  }
}

Try changing it

Open this circuit and run it

Next challenge

More projects in Input & Controls