Arduino UNO R4 - Keypad 1x4

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

Arduino UNO R4 Keypad 1x4

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×Keypad 1x4
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino UNO R4
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic 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 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 used for entering codes, moving through menus, or controlling interfaces in different projects.

Pinout

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

  • Pin 1 links to key 2.
  • Pin 2 links to key 1.
  • Pin 3 links to key 4.
  • Pin 4 links to key 3.
  • Pin 5 connects commonly to all keys.
Keypad 1x4 Pinout
image source: diyables.io

Wiring Diagram

The wiring diagram between Arduino UNO R4 Keypad 1x4

This image is created using Fritzing. Click to enlarge image

Arduino UNO R4 Code

Each key on the 1x4 keypad works like a button. We can use the digitalRead() function to check how each key is pressed. But like with any button, we face the problem of bouncing - where a single press might look like many presses. To solve this, we must debounce each key. Debouncing four keys without stopping other parts of the code can be hard. Luckily, the ezLink library makes it easier.

#include <ezButton.h> #define KEY_NUM 4 // The number of keys #define PIN_KEY_1 3 // The Arduino UNO R4 pin connected to the key 1 #define PIN_KEY_2 2 // The Arduino UNO R4 pin connected to the key 2 #define PIN_KEY_3 5 // The Arduino UNO R4 pin connected to the key 3 #define PIN_KEY_4 4 // The Arduino UNO R4 pin connected to the key 4 ezButton keypad_1x4[KEY_NUM] = { 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++) { int key_state = keypad_1x4[i].getState(); // the state after debounce if (keypad_1x4[i].isPressed()) return (i + 1); } return 0; }

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Connect Arduino UNO R4 to a 1x4 keypad according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port..
  • Click on the Libraries icon found on the left side of the Arduino IDE.
  • Type "ezButton" in the search box, and locate the button library provided by "ArduinoGetStarted.com".
  • Press the Install button to add the ezButton library.
Arduino UNO R4 button library
  • Copy the code and open it using Arduino IDE
  • Click the Upload button in Arduino IDE to send the code to Arduino UNO R4
  • Open Serial Monitor
  • Press each key on the 1x4 keypad
  • Check the results on 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!