ESP8266 - Keypad 1x4

This tutorial will teach you how to operate a 1x4 keypad with an ESP8266. We will cover:

ESP8266 NodeMCU Keypad 1x4

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Keypad 1x4
1×Jumper Wires
1×(Optional) 9V Power Adapter for ESP8266
1×(Optional) ESP8266 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 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. We appreciate your support.

Overview of Keypad 1x4

A 1x4 keypad has four buttons in a row. It is often used for entering a password, moving through menus, or controlling devices.


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

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

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and Keypad 1x4

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

ESP8266 Code

Every key on the 1x4 keypad works as a button. We can use the digitalRead() function to check the status of each key. Like with any button, there might be bouncing issues where one press looks like many presses. To prevent bouncing, we need to debounce each key. Doing this for four keys without affecting other parts of the code can be difficult. Luckily, the ezButton library makes this easier.

#include <ezButton.h> #define KEY_NUM 4 // the number of keys #define PIN_KEY_1 D5 // The ESP8266 NodeMCU pin connected to the key 1 #define PIN_KEY_2 D6 // The ESP8266 NodeMCU pin connected to the key 2 #define PIN_KEY_3 D1 // The ESP8266 NodeMCU pin connected to the key 3 #define PIN_KEY_4 D2 // The ESP8266 NodeMCU 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 ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Connect the ESP8266 to the 1x4 keypad.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Click on the Libraries icon on the left side of the Arduino IDE.
  • Search for ezButton to find the button library from
  • Click the Install button to install the ezButton library.
ESP8266 NodeMCU button library
  • Copy the code and open it in Arduino IDE
  • Click the Upload button in Arduino IDE to upload the code to your ESP8266
  • Open the Serial Monitor
  • Press each key on the 1x4 keypad one after the other
  • Check the results on the Serial Monitor
1 2 3 4
Autoscroll Show timestamp
Clear output
9600 baud  


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