A keypad code lock on an Arduino Uno
- 192parts on the bench
- 25boards running now
- 1.00xreal time, on every board
A 4x4 membrane keypad has sixteen keys, eight wires and no chip inside it at all, so reading one is a nice first lesson in making a microcontroller do the work. The circuit above is a code lock: type 1234 and press #, and an RGB LED turns green while a relay pulls in for three seconds, the way it would release a door strike. A wrong code flashes the LED red. Every key you press winks blue so you know it landed.
It is running on a simulated Arduino Uno. Open it in the editor to click the keys, and watch the serial monitor report what you typed.
What you need
- An Arduino Uno
- A 4x4 membrane keypad
- An RGB LED (common cathode) and three 220 ohm resistors
- A 5 V single-channel relay module
- An LED and a 220 ohm resistor for the relay to switch
- A breadboard and jumper wires
Wiring
How a keypad is read
Inside the keypad are four printed row traces and four column traces. Under each key a carbon pad shorts the row it sits on to the column it sits in. That is the entire part, which means the board has to ask which key is down, one row at a time.
The scan goes like this. Every column pin is an input with its pull-up on, so they all read high. The sketch drives one row low and reads the four columns. If a column reads low, the key where that row and column cross is held. Then that row is released, the next one is driven low, and so on, hundreds of times a second. The example’s keypad helper does exactly this and hands back one character per press.
void loop() {
char c = pad.pressed();
if (c == 0) {
delay(5);
return;
}
if (c == '#') {
if (matches()) open(); else refuse();
clearTyped();
return;
}
if (c == '*') {
clearTyped();
return;
}
if (count < 7) {
typed[count++] = c;
typed[count] = 0;
}
}What the relay is for
A board pin can light an LED. It cannot and should not try to switch a door strike, a lamp on the mains or a motor. A relay module takes a logic level on IN and closes a contact that shares nothing with the board’s 5 volts. Its COM terminal is the moving arm, NO is where the arm goes when the coil is energized, and NC is where it rests. In the example, COM is on the supply and NO feeds the door lamp.
The simulated relay is a proper relay rather than a switch that flips instantly: it takes 10 milliseconds to pull in and 5 to drop out, with neither contact made while the arm is traveling. A sketch that toggles a relay very fast does nothing here, just as it does nothing on the bench.
Try it in the editor
Open the circuit in the editor and change CODE to a code of your own, and CODE_LEN to its length. Try a code with letters in it: the A to D keys are just more characters.
Then select the relay and change its trigger property from high to low. Now the relay is energized whenever pin 13 is low, so the door lamp is lit while the lock is shut and goes out when the code is accepted. That is what happens when a sketch written for one kind of relay board meets the other kind, and the fix is one line in the sketch or one property on the part.
For a louder lock, add the piezo from the parts bin on a spare pin and beep on every key press.
Common mistakes
Rows and columns the wrong way around. The scan still runs, but the keys come out transposed, so 2 reads as 4. If the characters are wrong but consistent, swap the two groups of four.
Driving a row high instead of low. With two keys held in the same column, that puts a driven high straight against a driven low. On real hardware that is how a pin gets damaged; the simulator does nothing to protect you either.
Pressing three keys that form the corners of a rectangle. The fourth corner reads as pressed too, because current finds a way around through the three that are closed. This is called ghosting, cheap membrane keypads have no diodes to stop it, and the simulated one reproduces it.
Assuming the relay is active high. Most cheap blue relay boards are active low, switching when IN goes to ground. The simulated relay has a trigger property for exactly this: set it to match the tutorial you are following.
Questions
Do I need the Keypad library?
No. The scan is a few lines of code, and the example uses Mokxi’s own keypad helper, which does the scan and returns one character per press. The popular Keypad.h library is not one of the headers the browser compiler carries.
Does the keypad need debouncing?
A real membrane key chatters for a millisecond or two, so real code settles before accepting a key. The simulated keypad closes cleanly, so that part of the lesson belongs to the pushbutton, which does bounce.
Can I use a 4x3 keypad?
Yes. It has three columns instead of four. The bench has a 4x3 telephone keypad and a 5x4 calculator keypad as well, each with an example on the Uno.
How do I change the code?
Change the CODE string and CODE_LEN at the top of the sketch. A real lock would store it in EEPROM so it survives a reset, which EEPROM.h supports on the Uno.
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.