Log #106 · Nov 16, 2026
4x4 Keypad with Arduino: Wiring, Scanning and a PIN Code Lock
How a 4x4 matrix membrane keypad works, how to wire its 8 pins, how to scan it yourself without a library (and safely), how to debounce key presses, and a complete PIN code lock with lockout after wrong attempts.
The thin 4x4 membrane keypad is one of the cheapest ways to give a project real input: numbers, letters and two symbol keys for codes, menus and settings. It has 16 keys but only 8 wires, thanks to a clever trick called a matrix. Understanding that trick lets you read it without any library, and it's the same idea used in computer keyboards. This guide covers the wiring, the scanning method, and a complete PIN code lock.
How a key matrix works
Inside the keypad there are 4 row wires and 4 column wires. Each key sits at a crossing, and pressing it connects that row to that column. Nothing else.
To find which key is pressed, the Arduino:
- Sets all columns as inputs with pull-ups, so they read HIGH.
- Pulls one row LOW.
- Reads the columns. If one reads LOW, the key at that row and column is pressed.
- Releases that row and repeats for the next one.
Scanning all four rows takes microseconds, so it feels instant. Sixteen keys need only 4 + 4 = 8 pins instead of 16.
Pinout and wiring
Hold the keypad with the keys facing you and the ribbon at the bottom. On the common membrane keypads the 8 pins, left to right, are:
| Keypad pin | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Line | R1 | R2 | R3 | R4 | C1 | C2 | C3 | C4 |
| Arduino | D9 | D8 | D7 | D6 | D5 | D4 | D3 | D2 |
Male jumper wires plug straight into the ribbon's connector. No resistors are needed, because the column pull-ups are inside the Arduino. Some keypads use a different order; if the keys come out scrambled, the test sketch below will show you which way to swap.
Scanning without a library
const byte ROWS = 4, COLS = 4;
const byte rowPins[ROWS] = {9, 8, 7, 6};
const byte colPins[COLS] = {5, 4, 3, 2};
const char KEYS[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
char scanKeypad() { // returns the key held now, or 0
for (byte r = 0; r < ROWS; r++) {
pinMode(rowPins[r], OUTPUT); // drive only this row...
digitalWrite(rowPins[r], LOW);
delayMicroseconds(5); // let the line settle
for (byte c = 0; c < COLS; c++) {
if (digitalRead(colPins[c]) == LOW) {
pinMode(rowPins[r], INPUT); // ...and release it again
return KEYS[r][c];
}
}
pinMode(rowPins[r], INPUT);
}
return 0;
}
void setup() {
for (byte r = 0; r < ROWS; r++) pinMode(rowPins[r], INPUT);
for (byte c = 0; c < COLS; c++) pinMode(colPins[c], INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
char k = scanKeypad();
if (k) Serial.println(k);
delay(100);
}
Why rows not being scanned are set to INPUT instead of HIGH: if someone presses two keys in the same column, a row driven HIGH and a row driven LOW would be connected directly through the two keys, shorting two output pins together. Leaving the inactive rows floating (INPUT) makes that impossible. This is what the popular Keypad library does internally too.
Upload it, open the Serial Monitor and press each key. If, for example, pressing 1 prints D, your rows and columns are reversed or mirrored: swap the order in rowPins or colPins until every key matches.
Getting one character per press
The test sketch prints a key repeatedly while you hold it, and contacts bounce for a few milliseconds when pressed. For entering a code you want exactly one character per press. This function only reports a key when the reading has been stable for 30 ms and has just changed (the idea behind button debouncing):
char getKey() { // returns each key once, 0 otherwise
static char last = 0, stable = 0;
static unsigned long changedAt = 0;
char k = scanKeypad();
if (k != last) { // reading changed: restart the timer
last = k;
changedAt = millis();
}
if (millis() - changedAt > 30 && k != stable) {
stable = k; // new stable state
return k; // a key (or 0 on release)
}
return 0;
}
Project: PIN code lock
Type a code, press # to check it, * to clear. The green LED lights for three seconds when the code is right. Three wrong codes in a row lock the keypad for 30 seconds. Connect a green LED to D10 and a red LED to D11, each through a 220 Ω resistor to GND. Paste the scanKeypad() and getKey() functions and the ROWS/COLS, pin and KEYS definitions from above in place of the comment.
// --- ROWS, COLS, rowPins, colPins, KEYS, scanKeypad() and getKey() from above go here ---
const int GREEN = 10, RED = 11;
const char SECRET[] = "1234"; // change this
const byte MAX_LEN = 8;
char entry[MAX_LEN + 1];
byte len = 0;
byte wrong = 0;
unsigned long lockedUntil = 0;
void clearEntry() {
len = 0;
entry[0] = '\0';
}
void flash(int pin, int ms) {
digitalWrite(pin, HIGH);
delay(ms);
digitalWrite(pin, LOW);
}
void setup() {
for (byte r = 0; r < ROWS; r++) pinMode(rowPins[r], INPUT);
for (byte c = 0; c < COLS; c++) pinMode(colPins[c], INPUT_PULLUP);
pinMode(GREEN, OUTPUT);
pinMode(RED, OUTPUT);
Serial.begin(9600);
clearEntry();
Serial.println("Enter code, # to confirm, * to clear");
}
void loop() {
if (millis() < lockedUntil) { // locked out: ignore keys
digitalWrite(RED, (millis() / 500) % 2);
return;
}
digitalWrite(RED, LOW);
char k = getKey();
if (!k) return;
if (k == '*') {
clearEntry();
Serial.println("\nCleared");
} else if (k == '#') {
if (strcmp(entry, SECRET) == 0) {
Serial.println("\nOpen");
wrong = 0;
flash(GREEN, 3000); // drive a lock or relay here
} else {
wrong++;
Serial.println("\nWrong code");
flash(RED, 500);
if (wrong >= 3) {
Serial.println("Locked for 30 s");
lockedUntil = millis() + 30000UL;
wrong = 0;
}
}
clearEntry();
} else if (len < MAX_LEN) { // digits and letters
entry[len++] = k;
entry[len] = '\0';
Serial.print('*'); // don't show the code itself
}
}
How it works:
- Keys are collected into the
entrystring, up to 8 characters. strcmp()compares the entry with the secret code; it returns 0 when they match.- After three wrong attempts,
lockedUntilis set 30 seconds ahead and the red LED blinks until then. - The Serial Monitor prints
*for each key, like a real lock that hides the digits.
To control a real lock, replace the green LED with a transistor-driven solenoid as in the solenoid guide, or combine it with RFID for two-factor entry like the RFID door lock.
Using the Keypad library instead
If you prefer a library, Keypad by Mark Stanley and Alexander Brevig (in the Library Manager) does the same scanning and debouncing. Its getKey() returns each key once per press, so the lock code above works with it after replacing the two helper functions. Writing the scan yourself, though, means you understand what happens when something goes wrong.
Troubleshooting
- Wrong characters: row/column order differs on your keypad. Swap entries in
rowPinsorcolPins. - A whole row or column never works: a loose jumper or a cracked trace in the ribbon. Check continuity with a multimeter between the ribbon pin and the key.
- Random keys without pressing: columns aren't set to
INPUT_PULLUP. - Some combinations of three keys show a fourth "phantom" key: that's called ghosting, normal in a matrix without diodes. Only handle one key at a time.
— TheSolderBench · found a mistake? tell me