ESP32 S3 - Keypad 1x4

This guide walks you through connecting and programming a 1x4 keypad with an ESP32 S3. You will learn how each key maps to a GPIO pin and how to use the ezButton library to cleanly handle debouncing without disrupting other parts of your sketch.

What you'll build:

  1. A 1x4 keypad circuit wired to the ESP32 S3 using jumper wires.
  2. Code that detects which key is pressed and prints its label to the Serial Monitor.
  3. A debounced keypad reader that eliminates false multiple-press readings.
  4. A foundation you can extend for password entry, menu navigation, or device control.

Hardware Preparation

1×ESP32 S3 WROOM N16R8
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 1x4
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 Keypad 1x4

A 1x4 keypad contains four membrane buttons arranged in a single row, making it a compact input device for embedded projects. It is commonly used to let users enter data such as passwords, navigate simple menus, or trigger device actions without a full keyboard.

Key Specifications

The 1x4 keypad connects through five pins and operates at 3.3 V logic levels, which matches the ESP32 S3's GPIO voltage perfectly. Each key acts as a momentary switch between its dedicated pin and the common pin, so no external pull-up or pull-down resistor is required when using the microcontroller's internal pull-up resistors.

Pinout

The 1x4 keypad exposes five pins whose order does not match the key labels printed on the membrane.

  1. Pin 1: links with key 2
  2. Pin 2: links with key 1
  3. Pin 3: links with key 4
  4. Pin 4: links with key 3
  5. Pin 5: connects to all keys and is the common pin
Keypad 1x4 Pinout
image source: diyables.io

Wiring Diagram

Connect each of the four key pins on the keypad to a dedicated GPIO pin on the ESP32 S3, and wire the common pin (Pin 5) to GND. The ESP32 S3's internal pull-up resistors will hold each GPIO HIGH until a key is pressed.

Safety Notes

Always connect the keypad's common pin to GND and enable internal pull-up resistors in code so the GPIO pins are never left floating. Avoid routing the keypad wires near high-current traces or relay coils, as electrical noise can cause false key readings.

| Keypad 1x4 | ESP32 S3 |

|:---|:---|

| Pin 1 (key 2) | GPIO5 |

| Pin 2 (key 1) | GPIO4 |

| Pin 3 (key 4) | GPIO7 |

| Pin 4 (key 3) | GPIO6 |

| Pin 5 (common) | GND |

The wiring diagram between ESP32 S3 Keypad 1x4

This image is created using Fritzing. Click to enlarge image

ESP32 S3 Code

Each key on the 1x4 keypad behaves like a momentary button, so the ESP32 S3 reads it with digitalRead(). However, physical keys exhibit contact bounce that can cause a single press to register as multiple events, so debouncing is essential. The following code uses the ezButton library to handle debounce for all four keys automatically, making the sketch clean and easy to extend.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-keypad-1x4 */ #include <ezButton.h> #define KEY_NUM 4 // the number of keys #define PIN_KEY_1 4 // The ESP32-S3 pin GPIO4 connected to the key 1 #define PIN_KEY_2 5 // The ESP32-S3 pin GPIO5 connected to the key 2 #define PIN_KEY_3 6 // The ESP32-S3 pin GPIO6 connected to the key 3 #define PIN_KEY_4 7 // The ESP32-S3 pin GPIO7 connected to the key 4 ezButton keypad_1x4[] = { ezButton(PIN_KEY_1), ezButton(PIN_KEY_2), ezButton(PIN_KEY_3), ezButton(PIN_KEY_4) }; void setup() { Serial.begin(9600); for (byte i = 0; i < KEY_NUM; i++) { keypad_1x4[i].setDebounceTime(100); // set debounce time to 100 milliseconds } } void loop() { int key = getKeyPressed(); if (key) { Serial.print("The key "); Serial.print(key); Serial.println(" is pressed"); } } int getKeyPressed() { for (byte i = 0; i < KEY_NUM; i++) keypad_1x4[i].loop(); // MUST call the loop() function first for (byte i = 0; i < KEY_NUM; i++) { // get key state after debounce int key_state = keypad_1x4[i].getState(); // the state after debounce if (keypad_1x4[i].isPressed()) return (i + 1); } return 0; }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Wire the keypad to the ESP32 S3 as shown in the wiring diagram above.
  3. Connect the ESP32 S3 board to your PC via a USB Type-C cable.
  4. Open Arduino IDE on your PC.
  5. Select the right ESP32 S3 board (e.g. ESP32S3 Dev Module) and the correct COM port.
  6. Click the Libraries icon on the left bar of the Arduino IDE.
  7. Search "ezButton", then find the button library by ArduinoGetStarted.com.
  8. Click Install button to install the ezButton library.
  • Search for ezButton created by ArduinoGetStarted.com 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
ezButton by ArduinoGetStarted.com
Button library supports debounce, pressed/released events and the press counting. It is easy to use with multiple buttons. The library can be used for push-button, momentary switches, toggle switch, magnetic contact switch (door sensor)... It is designed for not only beginners but also experienced users. More info
1.0.6
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32S3 Dev Module on COM15
1
  1. Copy the above code and paste it into Arduino IDE.
  2. Click the Upload button in Arduino IDE to upload the code to the ESP32 S3.
How to upload ESP32 S3 code on Arduino IDE
  1. Open Serial Monitor on Arduino IDE.
  2. Press each key on the 1x4 keypad and check the output in Serial Monitor.

Pro Tip: If a key registers multiple presses with a single press, increase the debounce interval in the ezButton settings to filter out mechanical bounce from your specific keypad membrane.

Serial Monitor Output

Open the Serial Monitor at 9600 baud to observe which keys the ESP32 S3 detects as you press them. The following readings show a typical session with timestamps:

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
[2026-03-10 09:14:01] 1 [2026-03-10 09:14:03] 2 [2026-03-10 09:14:05] 3 [2026-03-10 09:14:07] 4
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Applications

The 1x4 keypad gives the ESP32 S3 a simple but versatile input interface suitable for a wide range of embedded projects. Here are some common applications:

  1. Password Entry: Use four keys to enter a numeric PIN that unlocks a relay-controlled door lock or safe.
  2. Menu Navigation: Assign keys to up, down, select, and back actions for navigating an OLED display menu.
  3. Device Mode Selector: Switch between operational modes such as manual, automatic, timer, and off with a single keypress.
  4. Alarm Control: Arm and disarm a motion-sensor alarm system by pressing a designated key combination.
  5. Score Tracker: Increment or decrement a score counter displayed on an LCD screen during a tabletop game.

Video Tutorial

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

Challenges

Experimenting beyond the basic keypad reader is the best way to deepen your understanding of input handling on the ESP32 S3. Try these challenges to build confidence with more advanced techniques:

  1. Beginner: Modify the code so each key toggles a different LED connected to a GPIO pin on the ESP32 S3.
  2. Intermediate: Implement a 4-digit PIN system that unlocks a relay when the correct sequence is entered and prints "Access Granted" or "Access Denied" to the Serial Monitor.
  3. Advanced: Combine the 1x4 keypad with an OLED display to build a fully navigable menu that lets the user select and adjust device settings without a computer.

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