How to wire a 4x4 keypad to an ESP32
To wire a 4x4 keypad to an ESP32, connect R1, R2, R3, R4 to GPIO 13 (D13), GPIO 12 (D12), GPIO 14 (D14), GPIO 27 (D27); and C1, C2, C3, C4 to GPIO 26 (D26), GPIO 25 (D25), GPIO 33 (D33), GPIO 32 (D32). A membrane keypad is sixteen switches with no power pin, so nothing on it can exceed the ESP32’s own 3.3 V levels and nothing needs shifting. The sketch below uses the Keypad (Keypad.h) library. It scans the rows and columns and prints each key you press.
- 3.3 V logic
- Library: Keypad.h
- 8 wires
4x4 keypad to ESP32 wiring table
Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.
| 4x4 keypad pin | ESP32 pin | Wire color | Why |
|---|---|---|---|
| R1, R2, R3, R4 | GPIO 13 (D13), GPIO 12 (D12), GPIO 14 (D14), GPIO 27 (D27) | amber | The rows: the library drives one low at a time. |
| C1, C2, C3, C4 | GPIO 26 (D26), GPIO 25 (D25), GPIO 33 (D33), GPIO 32 (D32) | green | The columns: inputs with pull-ups, read for each row. |
Power and logic levels for 4x4 keypad and ESP32
Logic level
The ESP32’s GPIO pins are 3.3 V logic and are not 5 V tolerant. Nothing wired to a pin may go above 3.3 V.
This pair
A membrane keypad has no power pin: it is sixteen switches at the crossings of four row wires and four column wires. Eight pins of the ESP32 scan it, using the chip’s internal pull-ups, so no resistors are needed.
Pins to leave alone
GPIO 34, 35, 36 (VP) and 39 (VN) are input only: they cannot drive a pin or take an internal pull-up. GPIO 0, 2, 5, 12 and 15 are strapping pins, read at power-on, and GPIO 6 to 11 are the flash chip.
The working code for the ESP32
Library: Keypad (Keypad.h). This is the sketch the circuit above runs.
#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] = {13, 12, 14, 27}; // R1 to R4
byte colPins[COLS] = {26, 25, 33, 32}; // C1 to C4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(115200);
Serial.println("Press a key");
}
void loop() {
char key = keypad.getKey(); // 0 when nothing new was pressed
if (key) {
Serial.print("Key: ");
Serial.println(key);
}
}
What you should see
Open the serial monitor at 115200 baud. The sketch prints Press a key and then a Key line with the character of each key you press. The library debounces the keys itself.
If the wrong characters come out, the rows and columns are swapped: R1 to R4 must go to the row pins in the sketch, C1 to C4 to the column pins.
The serial monitor prints this, recorded from the circuit above with the 5 key pressed after 300 ms:
Press a key
Key: 5Common mistakes wiring a 4x4 keypad to an ESP32
Rows and columns swapped
Swap the two arrays and every key prints its mirror across the diagonal, so 2 prints 4. If the wrong keys come out in a pattern, swap rowPins and colPins.
Pins listed in the wrong order
The connector order is R1 to R4 then C1 to C4 on most keypads, left to right with the keys facing you. List the pins in exactly that order or keys move around.
Running out of pins
A 4x4 keypad takes eight pins, which on the ESP32 leaves little room. An I2C port expander such as the PCF8574 scans it over two wires instead.
Build your own 4x4 keypad project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.