ESP8266 - Keypad 1x4
This tutorial will teach you how to operate a 1x4 keypad with an ESP8266. We will cover:
- How to Connect a 1x4 Keypad to an ESP8266.
- How to Program an ESP8266 to Detect Which Keys Are Pressed on a 1x4 Keypad.
![ESP8266 NodeMCU Keypad 1x4](https://newbiely.com/images/cover/esp8266-keypad-1x4.jpg)
Hardware Preparation
1 | × | ESP8266 NodeMCU | |
1 | × | Micro USB Cable | |
1 | × | Keypad 1x4 | |
1 | × | Jumper Wires | |
1 | × | (Optional) 9V Power Adapter for ESP8266 | |
1 | × | (Optional) Screw Terminal Expansion Board for ESP8266 |
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
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.
Pinout
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](https://newbiely.com/images/tutorial/keypad-1x4-pinout.jpg)
Wiring Diagram
![The wiring diagram between ESP8266 NodeMCU and Keypad 1x4](https://newbiely.com/images/tutorial/esp8266-keypad-1x4-wiring-diagram.jpg)
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.
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 ESP8266GetStarted.com.
- Click the Install button to install the ezButton library.
![ESP8266 NodeMCU button library](https://newbiely.com/images/tutorial/esp8266-button-library.jpg)
- 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