ESP32 S3 UNO - Keypad 3x4

This ESP32 S3 Uno keypad 3x4 tutorial shows you how to connect a 3x4 membrane keypad to the ESP32 S3 Uno form board and read each key with Arduino code. You see every pressed key in the Serial Monitor. Then you turn the keypad into a simple password lock that tells you if the code you typed is right or wrong.

In this tutorial, you will:

  1. Wire a 3x4 keypad to the ESP32 S3 Uno
  2. Install the DIYables_Keypad library in the Arduino IDE
  3. Read the pressed key and print it to the Serial Monitor
  4. Check a keypad password with the * and # keys
ESP32 S3 Uno keypad 3x4

Hardware Preparation

1×ESP32 S3 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 3x4
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 .

Overview of 3x4 Keypad

A 3x4 keypad is a cheap and easy way to let people type numbers into your project. Knowing how it is built helps you understand the wiring and the code.

The keypad has 12 thin membrane buttons. They sit in a grid of rows and columns, which is called a matrix. Each button is called a key. When you press a key, it connects one row line to one column line. The library scans the rows and columns very fast to find which key made the connection.

The keypad itself has no chip and needs no power pin. It is only a set of switches, so it works fine with the 3.3V logic of the ESP32 S3 Uno.

Pinout

The keypad has 7 pins in one row. They come in two groups.

Row pins

4 pins connect to the rows: R1, R2, R3 and R4.

Column pins

3 pins connect to the columns: C1, C2 and C3.

3x4 Keypad Pinout

ESP32 S3 Uno Pinout

The image below shows the pinout diagram of the ESP32 S3 Uno form board. Use it to find the Uno header pins (D3 to D9) and their GPIO numbers when you wire the keypad.

ESP32 S3 Uno pinout diagram

Wiring Diagram

Connect the 7 keypad pins to seven digital pins in a row, from D9 down to D3. The diagram and the table show each connection.

The wiring diagram between ESP32 S3 Uno Keypad 3x4

This image is created using Fritzing. Click to enlarge image

Keypad Pin ESP32 S3 Uno Pin
R1 D9 (GPIO46)
R2 D8 (GPIO21)
R3 D7 (GPIO14)
R4 D6 (GPIO3)
C1 D5 (GPIO20)
C2 D4 (GPIO19)
C3 D3 (GPIO17)

WARNING

This wiring uses two boot strapping pins of the ESP32 S3 Uno: D9 (GPIO46) for row R1 and D6 (GPIO3) for row R4. The board reads these pins at power-up to choose its boot mode. A pressed key can pull them to a column line and change that level. So do not hold any key while you power on, reset or upload code. If the board does not start or the upload fails, unplug the keypad, upload again, and then plug it back in.

ESP32 S3 Uno Code

This sketch scans the keypad all the time. When you press a key, it prints that key to the Serial Monitor. The pin arrays in the code use GPIO numbers, and they match the wiring table above.

#include <DIYables_Keypad.h> // DIYables_Keypad library const int ROW_NUM = 4; // four rows const int COLUMN_NUM = 3; // three columns char keys[ROW_NUM][COLUMN_NUM] = { { '1', '2', '3' }, { '4', '5', '6' }, { '7', '8', '9' }, { '*', '0', '#' } }; byte pin_rows[ROW_NUM] = { 46, 21, 14, 3 }; // ESP32 S3 Uno D9, D8, D7, D6 - connect to the row pinouts of the keypad byte pin_column[COLUMN_NUM] = { 20, 19, 17 }; // ESP32 S3 Uno D5, D4, D3 - connect to the column pinouts of the keypad 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 3x4 example"); } void loop() { char key = keypad.getKey(); if (key) { Serial.println(key); } }

Detailed Instructions

  1. New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
  2. Wire it up as shown in the diagram.
  3. Connect the board to your computer with a USB Type-C cable.
  4. Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
  5. Open the Library Manager by clicking the Libraries icon on the left side of the Arduino IDE.
  6. Search the library. Type DIYables_Keypad in the search box and find the keypad library by DIYables.io.
  7. Install it by clicking the Install button.
  • Search for DIYables_Keypad created by DIYables.io and click the Install button.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 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
ESP32S3 Dev Module on COM15
1
  1. Copy the code above and paste it into the Arduino IDE.
  2. Upload the code by clicking the Upload button.
  3. Open the Serial Monitor and set it to 9600 baud.
  4. Press some keys on the keypad.
  5. Check the output in the Serial Monitor.
  6. Tip: if a key prints nothing or the wrong character, check that the row and column wires are not swapped.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
Keypad 3x4 example 3 6 9 4 * #
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Keypad and Password

A keypad is a great input for a door lock or a secret code. To make it work, you give two keys a special job, and all the other keys become part of the password.

Start key

The "*" key starts the password from the beginning. Press it to clear what you typed so far.

Enter key

The "#" key ends the password. When you press it, the code checks your input.

How the logic works

  1. If the key is not "*" or "#", the code adds it to the password you are typing.
  2. If the key is "#", the code compares your input with the saved password, prints the result, and then clears your input.
  3. If the key is "*", the code clears your input so you can start again.

Keypad - Password Code

The sketch below stores the password "1234". It builds your input one key at a time and checks it when you press "#". Change the password line to set your own code, and put your action (open a lock, turn on an LED) where the comment says "DO YOUR WORK HERE".

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-keypad-3x4 */ #include <DIYables_Keypad.h> // DIYables_Keypad library const int ROW_NUM = 4; //four rows const int COLUMN_NUM = 3; //three columns char keys[ROW_NUM][COLUMN_NUM] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9'}, {'*','0','#'} }; byte pin_rows[ROW_NUM] = {46, 21, 14, 3}; // ESP32 S3 Uno D9, D8, D7, D6 - connect to the row pinouts of the keypad byte pin_column[COLUMN_NUM] = {20, 19, 17}; // ESP32 S3 Uno D5, D4, D3 - connect to the column pinouts of the keypad DIYables_Keypad keypad = DIYables_Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); const String password = "1234"; // change your password here String input_password; void setup(){ Serial.begin(9600); Serial.println("Keypad 3x4 password"); input_password.reserve(32); // maximum input characters is 33, change if needed } void loop(){ char key = keypad.getKey(); if (key){ Serial.println(key); if(key == '*') { input_password = ""; // clear input password } else if(key == '#') { if(password == input_password) { Serial.println("password is correct"); // DO YOUR WORK HERE } else { Serial.println("password is incorrect, try again"); } input_password = ""; // clear input password } else { input_password += key; // append new character to input password string } } }
  1. Upload the code above to the ESP32 S3 Uno.
  2. Open the Serial Monitor at 9600 baud.
  3. Type a wrong code: press "1", "2", "3", "4", "5", "6", then "#".
  4. Type the right code: press "1", "2", "3", "4", then "#".
  5. Read the results in the Serial Monitor.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
Keypad 3x4 password 1 2 3 4 5 6 # password is incorrect, try again 1 2 3 4 # password is correct
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Video Tutorial

Watch the video below to see this ESP32 S3 Uno project step by step.

Additional Knowledge

These guides help you go one step further with your keypad:

FAQ

Does a 3x4 keypad work with the 3.3V ESP32 S3 Uno?

Yes. The keypad is only a grid of switches with no chip inside, so it has no voltage of its own. The library uses the ESP32 S3 Uno pins and their internal pull-ups, so all signals stay at 3.3V. You do not need a level shifter or resistors.

Why are the pin numbers in the code different from the D pins on the board?

On the ESP32 S3 Uno, you write GPIO numbers in code, not the Uno header labels. For example, D9 is GPIO46 and D3 is GPIO17. That is why the code uses {46, 21, 14, 3} for the rows and {20, 19, 17} for the columns.

Can I use other pins for the keypad?

Yes. Any pin that works as a digital input and output is fine. Change the numbers in pin_rows and pin_column to the GPIO numbers you use. Avoid D0 and D1 (GPIO44 and GPIO43), because the USB serial uses them.

Is the code different from the UNO R4 version?

The logic and the DIYables_Keypad library are the same. Only the pin numbers change, because the ESP32 S3 Uno uses GPIO numbers. You also pick the ESP32S3 Dev Module board in the Arduino IDE.

What if my project needs more pins than the Uno headers give?

The ESP32 S3 Uno has two extra rows of holes with more GPIOs. GPIO15 and GPIO16 are I/O with PWM and analog. GPIO45 and GPIO35 to GPIO42 are I/O with PWM. GPIO47 and GPIO48 are output only with PWM. Solder pin headers to use them and write the GPIO number in code. For a keypad, GPIO15, GPIO16 and GPIO35 to GPIO42 are good choices. Do not use GPIO47 or GPIO48 for keypad rows, because the keypad needs input pins. Two warnings: GPIO47 and GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely. Also, GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.

How do I change the password?

Edit the line const String password = "1234"; and put your own digits between the quotes. Upload the code again. Do not use "*" or "#" in the password, because these keys have special jobs.

Troubleshooting

Problem Possible Cause Solution
No COM port in the Arduino IDE USB driver missing or board not in upload mode Install the CP210x or CH340 driver. Or hold BOOT, press RESET, then release BOOT and upload again
Upload fails or the board does not start A key is pressed or a boot pin (D6 or D9) is pulled at power-up Do not touch the keypad during reset. Unplug the keypad, upload, then plug it back in
Nothing prints when a key is pressed Loose wire or wrong pin in the code Check all 7 wires and make sure the code uses the GPIO numbers from the wiring table
Wrong character is printed Row or column wires are swapped Compare the keypad pin order R1 to C3 with the wiring table and fix the order
One full row or column does not work That wire is broken or not seated Reseat or replace the jumper wire for that row or column
Strange symbols in the Serial Monitor Wrong baud rate Set the Serial Monitor to 9600 baud
Compile error DIYables_Keypad.h not found Library is not installed Install DIYables_Keypad from the Library Manager
Password is always incorrect Old input was not cleared or extra keys were typed Press * first to clear, then type the password and press #

※ 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!