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.
- Advanced
- Input & Controls
- 6 parts
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
- Arduino Uno
- 4x4 Keypad
- Servo Motor
- Resistor
- LED
- GND
Wiring
- 4x4 Keypad R1 → Arduino Uno D9
- 4x4 Keypad R2 → Arduino Uno D8
- 4x4 Keypad R3 → Arduino Uno D7
- 4x4 Keypad R4 → Arduino Uno D6
- 4x4 Keypad C1 → Arduino Uno D5
- 4x4 Keypad C2 → Arduino Uno D4
- 4x4 Keypad C3 → Arduino Uno D3
- 4x4 Keypad C4 → Arduino Uno D2
- Arduino Uno D10 → Servo Motor SIG
- Arduino Uno 5V → Servo Motor VCC (5V)
- Arduino Uno GND → Servo Motor GND
- Arduino Uno D13 → Resistor A
- Resistor B → LED Anode (+)
- LED Cathode (-) → GND
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
- Choose your own code by editing const int SECRET = 1234; (keep it to four digits).
- Keep the lock open longer by changing delay(3000) to delay(10000).
Next challenge
More projects in Input & Controls
- Button Controlled LEDPress a button to turn on an LED.
- Potentiometer ReadingRead a potentiometer and display its value on the serial monitor.
- Buzzer AlarmPress and hold the button to sound the alarm buzzer.
- Interrupt Button CounterAn interrupt toggles the LED the instant you press the button, even while loop() is busy…