ESP32 UNO - Keypad 4x4

Need 16 buttons but only have a handful of free pins? An ESP32 Uno keypad 4x4 setup gives you all 16 keys from just 8 wires. Hook the membrane matrix keypad to the ESP32 Uno form board, and every key you press shows up as a character in the Serial Monitor. After that, the same keypad becomes a small password entry pad.

By the end, you can:

  1. Connect a 4x4 matrix keypad to eight Uno header pins
  2. Install the DIYables_Keypad library from the Arduino IDE Library Manager
  3. Print each pressed key with a short Arduino sketch
  4. Check a typed code against a saved password
ESP32 Uno keypad 4x4

Components You Need

1×ESP32 Uno-form Board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Keypad 4x4
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Inside the 4x4 Keypad

After this part, you know how 16 keys share only 8 pins.

The pad holds 16 thin membrane switches placed in a grid. Engineers call this grid a matrix. Every switch in the grid counts as one key.

Feature Details
Number of keys 16 (digits 0-9, letters A-D, * and #)
Layout 4 rows by 4 columns
Connector 8 pins in one strip
Power pin none (passive switches only)
Typical use code entry, menus, number input

Pinout

The 8 pins form two groups of four.

Pin Group Labels Job
Rows R1, R2, R3, R4 one pin for each horizontal line of keys
Columns C1, C2, C3, C4 one pin for each vertical line of keys

A key press shorts exactly one row pin to one column pin. The library drives the lines in turn and watches which pair connects, so it knows the exact key.

4x4 Keypad Pinout

ESP32 Uno Pinout

The board shows Uno names like D2 or D9 on its headers, but your sketch needs the GPIO number behind each one. Look up the eight keypad pins in this diagram before wiring.

ESP32 Uno pinout diagram

How to Connect

When this step is done, all eight keypad lines sit on D2 to D9.

The wiring diagram between ESP32 Uno Keypad 4x4

This image is created using Fritzing. Click to enlarge image

Keypad Pin ESP32 Uno Pin
R1 D9 (GPIO13)
R2 D8 (GPIO12)
R3 D7 (GPIO14)
R4 D6 (GPIO27)
C1 D5 (GPIO16)
C2 D4 (GPIO17)
C3 D3 (GPIO25)
C4 D2 (GPIO26)

WARNING

Row R2 lands on D8 (GPIO12), a boot strapping pin of the ESP32 Uno. At start-up the chip checks this pin to set its flash voltage, and it must be LOW then. The keypad only has switches, so it is fine in most cases. Keep your fingers off the keys while you reset, power on or upload. If the board refuses to start or the upload stops, pull the R2 wire out of D8, upload, and put it back.

Good to know: The keypad has no supply pin, and the ESP32 Uno drives its lines at 3.3V, so no level shifter or resistor is needed.

ESP32 Uno Code

The sketch scans the pad all the time and prints a key the moment you press it.

#include <DIYables_Keypad.h> // keypad scanning library from DIYables const int ROW_NUM = 4; // the keypad has 4 rows const int COLUMN_NUM = 4; // the keypad has 4 columns char keys[ROW_NUM][COLUMN_NUM] = { {'1','2','3', 'A'}, {'4','5','6', 'B'}, {'7','8','9', 'C'}, {'*','0','#', 'D'} }; byte pin_rows[ROW_NUM] = {13, 12, 14, 27}; // R1-R4 on D9 (GPIO13), D8 (GPIO12), D7 (GPIO14), D6 (GPIO27) byte pin_column[COLUMN_NUM] = {16, 17, 25, 26}; // C1-C4 on D5 (GPIO16), D4 (GPIO17), D3 (GPIO25), D2 (GPIO26) DIYables_Keypad keypad = DIYables_Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); void setup(){ Serial.begin(9600); delay(1000); Serial.println("Keypad 4x4 example"); } void loop(){ char key = keypad.getKey(); if (key){ Serial.println(key); } }

Upload and Test

  1. First time with this board? Set it up with ESP32 Uno - Getting Started before you continue.
  2. Build the circuit by following the wiring table above.
  3. Plug in the USB Type-C cable between the ESP32 Uno and your computer.
  4. Select the board in Arduino IDE: pick ESP32 Dev Module and the COM port of the board (CH340).
  5. Open the Library Manager with the Libraries icon in the left bar of the Arduino IDE.
  6. Find the library: enter DIYables_Keypad in the search field and locate the entry made by DIYables.io.
  7. Add it to the IDE by pressing Install.
  • Search for DIYables_Keypad created by DIYables.io and click the Install button.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Library Manager
Type:
All
Topic:
All
DIYables_Keypad by DIYables.io
The library is designed for Arduino, ESP32, ESP8266... to use with keypad such as 3x4, 4x4 keypad. It also works with Arduino Uno R4 WiFi/Minima More info
1.0.1
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32 Dev Module on COM15
1
  1. Paste the sketch above into a new Arduino IDE window.
  2. Send it to the board by pressing Upload.
  3. Start the Serial Monitor and choose 9600 baud.
  4. Tap a few keys and read them in the Serial Monitor.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Keypad 4x4 example 7 0 D 5 # 9 B *
Ln 11, Col 1
ESP32 Dev Module on COM15
2
  1. Pro tip: press all 16 keys once, in order, right after the first upload. A missing character points straight to the one wire that is off.

Video Tutorial

See the ESP32 Uno build in action in the video below.

Code Explanation

This part maps each block of the first sketch to its job.

Code Part What It Does
keys[ROW_NUM][COLUMN_NUM] the character printed on each key, row by row
pin_rows[] = {13, 12, 14, 27} GPIOs behind D9, D8, D7 and D6 (rows R1 to R4)
pin_column[] = {16, 17, 25, 26} GPIOs behind D5, D4, D3 and D2 (columns C1 to C4)
DIYables_Keypad keypad(...) links the key map with the two pin lists
keypad.getKey() returns the pressed key, or 0 when nothing is pressed

The loop prints a character only when getKey() returns one, so holding a key gives a single line, not a flood.

Good to know: The two pin arrays hold GPIO numbers, not D labels. To move the keypad, edit only these two lines.

Keypad as a Password Pad

After this part, the keypad works like the code panel of a door lock.

Two keys get a special role, and the rest build the code.

Key Role
* clears the typed code and starts over
# ends the code and checks it
0-9 and A-D added one by one to the typed code

For each key press, the sketch picks one action:

  1. On "*", it empties the typed code.
  2. On "#", it compares the typed code with the saved one, prints the result, then empties the typed code.
  3. On any other key, it appends that key to the typed code.

Password Sketch

The saved code here is "1234A". Swap it for your own on the line const String password.

/* * This ESP32 Uno code was developed by newbiely.com * * This ESP32 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-uno/esp32-uno-keypad-4x4 */ #include <DIYables_Keypad.h> // keypad scanning library from DIYables const int ROW_NUM = 4; // the keypad has 4 rows const int COLUMN_NUM = 4; // the keypad has 4 columns char keys[ROW_NUM][COLUMN_NUM] = { {'1','2','3', 'A'}, {'4','5','6', 'B'}, {'7','8','9', 'C'}, {'*','0','#', 'D'} }; byte pin_rows[ROW_NUM] = {13, 12, 14, 27}; // R1-R4 on D9 (GPIO13), D8 (GPIO12), D7 (GPIO14), D6 (GPIO27) byte pin_column[COLUMN_NUM] = {16, 17, 25, 26}; // C1-C4 on D5 (GPIO16), D4 (GPIO17), D3 (GPIO25), D2 (GPIO26) DIYables_Keypad keypad = DIYables_Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); const String password = "1234A"; // the secret code - edit it to your own String input_password; void setup(){ Serial.begin(9600); Serial.println("Keypad 4x4 password"); input_password.reserve(32); // room for 32 typed keys, raise it for longer codes } void loop(){ char key = keypad.getKey(); if (key){ Serial.println(key); if(key == '*') { input_password = ""; // '*' wipes what was typed so far } else if(key == '#') { if(password == input_password) { Serial.println("password is correct"); // put the unlock action here (relay, servo, buzzer...) } else { Serial.println("password is incorrect, try again"); } input_password = ""; // start fresh for the next try } else { input_password += key; // add this key to the typed code } } }
  1. Upload this second sketch to the ESP32 Uno.
  2. Reopen the Serial Monitor and keep it on 9600 baud.
  3. Enter a wrong code: type "1234BC" and finish with "#".
  4. Enter the right code: type "1234A" and finish with "#".
  5. Read the replies in the Serial Monitor.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Keypad 4x4 password 1 2 3 4 B C # password is incorrect, try again 1 2 3 4 A # password is correct
Ln 11, Col 1
ESP32 Dev Module on COM15
2
  1. Pro tip: before a real lock project, add a counter that blocks input for 30 seconds after three wrong codes in a row.

ESP32 Uno vs UNO R4 for This Project

This table lists what changes when this keypad build moves from the UNO R4 to the ESP32 Uno.

Item Arduino UNO R4 ESP32 Uno
Logic voltage 5V 3.3V
Row pins in code 9, 8, 7, 6 13, 12, 14, 27
Column pins in code 5, 4, 3, 2 16, 17, 25, 26
Keypad library DIYables_Keypad DIYables_Keypad (no change)
Boot strapping pin in the wiring none D8 (GPIO12)
WiFi and Bluetooth WiFi on the R4 WiFi only WiFi plus Bluetooth Classic and BLE
Arduino IDE board Arduino UNO R4 WiFi ESP32 Dev Module

The wiring stays exactly the same, on D2 to D9. Only the numbers in the two pin arrays change, plus the care around D8 at boot.

Go Wireless: Keypad Log on Your Phone

Here you get an idea to see every key press away from the computer.

The ESP32 Uno has WiFi and Bluetooth built in. Instead of the Serial Monitor, send each key to a monitor page in your phone browser or to a Bluetooth app. That way you can watch who types a code on a lock panel, or log wrong attempts from across the room.

Troubleshooting

Problem Possible Cause Solution
The board has no COM port in Arduino IDE CH340 driver missing or a charge-only USB cable Install the CH340 driver and swap to a USB data cable
Upload ends with Failed to connect or Waiting for download The chip did not enter download mode Hold the BOOT button while the upload starts and let go when the percentage appears
Board does not boot or upload with the keypad plugged in D8 (GPIO12) is a boot strapping pin pulled the wrong way Unplug the R2 wire from D8 while uploading, then plug it back in
The Serial Monitor stays empty Baud rate is not 9600 or the connector is loose Pick 9600 baud and seat every jumper wire firmly
Four keys in one line never print One row or column wire is off or loose Find the line those keys share and check that wire
Keys print the wrong character Rows and columns are swapped or reversed Wire R1 to D9 and C4 to D2 exactly as in the table
Compile error saying DIYables_Keypad.h is missing Library not installed Install DIYables_Keypad from the Library Manager
The correct code is rejected Stray keys were typed first Press * to clear, type the code, then press #

FAQ

How does a 16-key matrix keypad know which key I pressed?

Each key sits where one row line crosses one column line. The library drives the lines one at a time and checks which row and column are connected. That crossing tells it the key, so 8 pins are enough for 16 keys.

Is it safe to wire this keypad straight to 3.3V pins?

Yes. The keypad is only switches and has no power input. The ESP32 Uno sends and reads 3.3V on the lines, so nothing on the pad can harm the board.

Do I need pull-up resistors for the keypad rows?

No. The DIYables_Keypad library turns on the internal pull-ups of the GPIOs it uses. D2 to D9 on the ESP32 Uno all have internal pull-ups, so you only need the eight jumper wires.

Why are the pin numbers in the code not 9, 8, 7, 6?

On the ESP32 Uno, the printed Uno label and the GPIO number differ. D9 maps to GPIO13, and D6 maps to GPIO27. The code always takes the GPIO number from the pinout diagram.

Can I plug the keypad into A2 to A5 instead?

Not for this keypad. A2 to A5 (GPIO35, 34, 36, 39) are input only and have no internal pull-up. The library must drive some lines as outputs and pull others up, so it needs full I/O pins like D2 to D13.

Why does the board sometimes not start when the keypad is connected?

R2 is on D8 (GPIO12), which the ESP32 Uno reads at boot to set its flash voltage. It must be LOW then. Do not hold a key during reset, and unplug that wire if uploads fail.

What is different from the UNO R4 code?

Only the numbers in pin_rows and pin_column. The library, the key map and the logic are the same. In Arduino IDE you also pick ESP32 Dev Module as the board.

Does the ESP32 Uno need a special USB driver?

Yes, it uses a CH340 USB-to-serial chip. Windows often installs it by itself, but if no COM port shows up, install the CH340 driver by hand.

My keypad build needs more pins than the Uno headers offer. What can I use?

The ESP32 Uno has a few extra holes near the power header: GPIO32 and GPIO33 (I/O, PWM, analog on ADC1, so they also work while WiFi is on) and GPIO15 (I/O, PWM, analog on ADC2). Add pin headers with a soldering iron, then use the plain GPIO number in your sketch. For a keypad lock, GPIO32/33 are good for a relay and a buzzer, or to take row R2 off D8. Two warnings: (1) GPIO15 and GPIO0 (the IO0 pin at the unused Uno NC position) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code, so use GPIO32/33 first. (2) Never use the six holes under the digital header (SD0 to SD3, CMD, CLK = GPIO6 to GPIO11). They are wired to the flash chip, and using them crashes the board.

How long can the password be, and which keys can it use?

The sketch reserves room for 32 keys, and you can raise that number. Use any of 0 to 9 and A to D. Leave out * and #, because they clear and submit the code.

Can I open a lock with this keypad and the ESP32 Uno?

Yes. Put a relay or a servo action where the comment in the password sketch marks the unlock spot. Because the ESP32 Uno has WiFi, you can also send an alert to your phone when a wrong code is typed.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!