Arduino Nano - Keypad 1x4

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

Arduino Nano Keypad 1x4

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
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

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 lined up in a row. It is often used to enter passwords, move through menus, or control devices.

Pinout

The 1x4 keypad has five pins. These pins are not arranged in the same order as the keys on the keypad.

  • 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 connected to all keys
Keypad 1x4 Pinout
image source: diyables.io

Wiring Diagram

The wiring diagram between Arduino Nano and Keypad 1x4

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code

Each key on the 1x4 keypad works as a button. This lets us use the digitalRead() function to check if a key is pressed. However, keys can sometimes bounce, meaning a single press appears as several presses. To fix this, we have to debounce each key. Debouncing four keys at once can be tough without stopping other code from running. Luckily, the ezButton library helps make this easier.

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

  • Connect the Arduino Nano to the 1x4 keypad.
  • Connect the Arduino Nano to the computer using a USB cable.
  • Open the Arduino IDE and choose the correct board and port.
  • Click on the Libraries icon on the left side of the Arduino IDE.
  • Type ezButton in the search box and locate the button library from Arduino NanoGetStarted.com.
  • Press the Install button to add the ezButton library.
Arduino Nano button library
  • Copy the code and open it in Arduino IDE
  • Click the Upload button in Arduino IDE to send the code to Arduino Nano
  • Open the Serial Monitor
  • Press each key on the 1x4 keypad individually
  • Check the results in 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!