ESP32 - Keypad 1x4

In this guide, we will discover how to use a 1x4 keypad with an ESP32. We will cover:

ESP32 Keypad 1x4

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Keypad 1x4
1×Jumper Wires
1×(Optional) 9V Power Adapter for ESP32
1×(Recommended) ESP32 Screw Terminal Adapter

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to Keypad 1x4

A 1x4 keypad has four membrane buttons lined up in a row. It is often used to let users enter data, like passwords, navigate menus, or control devices.

Pinout

The 1x4 keypad has 5 pins. The arrangement of these pins does not match the order of the key labels.

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

Wiring Diagram

ESP32 Keypad 1x4 Wiring Diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

ESP32 Code

Each key on the 1x4 keypad works like a button. This lets us use the digitalRead() function to check each key's status. But, just like other buttons, these keys can 'bounce'. This means a single press might be detected as many presses. To fix this, we must debounce each key. Doing this for four keys without interrupting other code parts can be tough. Luckily, the ezButton library makes it easier.

#include <ezButton.h> #define KEY_NUM 4 // the number of keys #define PIN_KEY_1 26 // The ESP32 pin GPIO26 connected to the key 1 #define PIN_KEY_2 25 // The ESP32 pin GPIO25 connected to the key 2 #define PIN_KEY_3 14 // The ESP32 pin GPIO14 connected to the key 3 #define PIN_KEY_4 27 // The ESP32 pin GPIO27 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; }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Connect the ESP32 to the 1x4 keypad.
  • Connect the ESP32 board to your PC via a USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Navigate to the Libraries icon on the left bar of the Arduino IDE.
  • Search “ezButton”, then find the button library by ArduinoGetStarted.com
  • Click Install button to install ezButton library.
Arduino button library
  • Copy the code and open it in Arduino IDE
  • Click the Upload button in Arduino IDE to send the code to ESP32
  • Open Serial Monitor
  • Press each key on the 1x4 keypad
  • Check the results in Serial Monitor
COM6
Send
1 2 3 4
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ OUR MESSAGES