Keypads are everywhere: door locks, alarm panels, vending machines. The 4×4 membrane keypad has 16 keys but only 8 wires, because the keys are arranged in a grid of 4 rows and 4 columns. In this lesson you read the keys and build a secret-code lock.
What you'll learn
- How a key matrix works
- How to use the Keypad library
- How to compare a typed code with a secret code
What you need
- UNO R3 board and USB cable
- 4×4 membrane keypad
- 8 male-to-male jumper wires
Step 1: Install the library
Open Tools → Manage Libraries, search for Keypad and install the one by Mark Stanley and Alexander Brevig.
Step 2: Wiring
Hold the keypad with the keys facing you. The 8 pins on the ribbon connector are, from left to right, the 4 rows (R1 to R4) and then the 4 columns (C1 to C4). Plug the jumper wires straight into the connector.

| Keypad pin | UNO pin |
|---|---|
| R1 | 9 |
| R2 | 8 |
| R3 | 7 |
| R4 | 6 |
| C1 | 5 |
| C2 | 4 |
| C3 | 3 |
| C4 | 2 |
Step 3: Print the keys
#include <Keypad.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);
void setup() {
Serial.begin(9600);
Serial.println("Press a key");
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.print("Key pressed: ");
Serial.println(key);
}
}
Open the Serial Monitor at 9600 baud and press a few keys.
Step 4: A secret-code lock
Type the code and finish with #. Press * to start again.
#include <Keypad.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);
const String SECRET = "1234";
String typed = "";
void setup() {
Serial.begin(9600);
Serial.println("Enter the code, then press #");
}
void loop() {
char key = keypad.getKey();
if (!key) return;
if (key == '#') {
if (typed == SECRET) {
Serial.println("Unlocked!");
} else {
Serial.println("Wrong code, try again");
}
typed = "";
} else if (key == '*') {
typed = "";
Serial.println("Cleared");
} else {
typed += key;
Serial.print("*"); // hide the digits
}
}
How it works
Each key sits where one row wire crosses one column wire. The library switches the rows on one at a time and checks which column answers. That tells it exactly which key is pressed, using only 8 pins instead of 16.
Try this next
- Combine this with lesson 3: turn the servo to 90° when the right code is entered, like a real lock.
- Add the LED from lesson 1 and blink it three times on a wrong code.
Troubleshooting
-
The wrong keys appear: the connector may be reversed. Swap the order of the wires, or change the order in
rowPinsandcolPins. - Nothing appears: check that the Serial Monitor speed is 9600 baud and the library is installed.