Arduino - Keypad 1x4

In this tutorial, we will learn how to use a keypad 1x4 with an Arduino. In detaile, we will learn:

Arduino Keypad 1x4

Hardware Required

1×Arduino Uno
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×Keypad 1x4
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Recommended) Breadboard Shield For Arduino Uno
1×(Recommended) Enclosure For Arduino Uno

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables.

About Keypad 1x4

A 1x4 keypad is composed of four membrane buttons arranged in a single row. It is commonly used for user input in projects such as passcode entry, menu navigation, or control interfaces.

Pinout

The 1x4 keypad has 5 pins, which do not correspond directly to the key labels in order. Specifically:

  • 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: is a common pin that connects to all keys
Keypad 1x4 Pinout
image source: diyables.io

Wiring Diagram

Arduino Keypad 1x4 Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Arduino Code

Each key on the 1x4 keypad functions as a button. This means we can use the digitalRead() function to check the status of each key. However, in practice, like with any button, we must handle the issue of bouncing, where a single press might be mistakenly detected as multiple presses. To avoid this, we need to debounce each key. This task becomes challenging when trying to debounce four keys without blocking other parts of the code. Fortunately, the ezButton library simplifies this process.

#include <ezButton.h> #define KEY_NUM 4 // the number of keys #define PIN_KEY_1 3 // The Arduino pin connected to the key 1 #define PIN_KEY_2 2 // The Arduino pin connected to the key 2 #define PIN_KEY_3 5 // The Arduino pin connected to the key 3 #define PIN_KEY_4 4 // The Arduino 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; }

Quick Steps

  • Connect Arduino to the keypad 1x4
  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and 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 above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Open Serial Monitor
  • Press keys on keypad 1x4 one by one
  • See the result in Serial Monitor
COM6
Send
1 2 3 4
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

The Best Arduino Starter Kit

※ OUR MESSAGES