Arduino Nano ESP32 - Keypad 1x4

In this guide, we will learn how to use a 1x4 keypad with an Arduino Nano ESP32. We will cover the following details:

Arduino Nano ESP32 Keypad 1x4

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×Keypad 1x4
1×Jumper Wires
1×Breadboard
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano
1×(Recommended) Power Splitter For Arduino Nano ESP32

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 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 has four buttons in a row. People use it to enter passwords, move through 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 is connected to key 2
  • Pin 2 is connected to key 1
  • Pin 3 is connected to key 4
  • Pin 4 is connected to key 3
  • Pin 5 connects to all keys and is common
Keypad 1x4 Pinout
image source: diyables.io

Wiring Diagram

The wiring diagram between Arduino Nano ESP32 and Keypad 1x4

This image is created using Fritzing. Click to enlarge image

Arduino Nano ESP32 Code

Every key on the 1x4 keypad acts as a button, so we can use the digitalRead() function to check each key's status. Like other buttons, we need to address the bouncing issue, where one press might register as several presses. To prevent this, it's important to debounce each key. Debouncing four keys at once without interrupting other code parts can be tough. Luckily, the ezBbutton library makes this easier.

#include <ezButton.h> #define KEY_NUM 4 // the number of keys #define PIN_KEY_1 D5 // The Arduino Nano ESP32 pin connected to the key 1 #define PIN_KEY_2 D4 // The Arduino Nano ESP32 pin connected to the key 2 #define PIN_KEY_3 D7 // The Arduino Nano ESP32 pin connected to the key 3 #define PIN_KEY_4 D6 // The Arduino Nano ESP32 pin 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

To get started with Arduino Nano ESP32, follow these steps:

  • If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
  • Connect Arduino Nano ESP32 to the 1x4 keypad
  • Connect the Arduino Nano ESP32 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano ESP32 board and its corresponding COM port.
  • Click on the Libraries icon on the left side of the Arduino IDE.
  • Search for ezButton and locate the button library from Arduino Nano ESP32GetStarted.com
  • Click the Install button to add the ezButton library.
Arduino Nano ESP32 button library
  • Copy the code and open it in Arduino IDE
  • Click the Upload button in Arduino IDE to upload the code to your Arduino Nano ESP32
  • Open the Serial Monitor
  • Press each key on the 1x4 keypad one at a time
  • Check the results on the Serial Monitor
COM6
Send
1 2 3 4
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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