Arduino Mega - Keypad 1x4

This guide shows how to use a 1x4 keypad with an Arduino Mega. We will cover:

Arduino Mega Keypad 1x4

Hardware Preparation

1×Arduino Mega
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

Or you can buy the following 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 in a line. You use it to type codes, move through menus, or control devices in different projects.

Pinout

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

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

Wiring Diagram

The wiring diagram between Arduino Mega Keypad 1x4

This image is created using Fritzing. Click to enlarge image

Arduino Mega Code

Each key on the 1x4 keypad acts like a button. We can use the digitalRead() function to see when a key is pressed. But like any button, it can bounce — one press may look like several presses. To fix this, we need to 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 Mega pin connected to the key 1 #define PIN_KEY_2 2 // The Arduino Mega pin connected to the key 2 #define PIN_KEY_3 5 // The Arduino Mega pin connected to the key 3 #define PIN_KEY_4 4 // The Arduino Mega 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 steps one by one.

  • Connect Arduino Mega to a 1x4 keypad using the diagram.
  • Connect the Arduino Mega to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Choose the correct board (Arduino Mega) and the COM port.
  • Click the Libraries button on the left side of the Arduino IDE.
  • In the search box, type "ezButton" and find the library from ArduinoGetStarted.com.
  • Click Install to add the ezButton library.
Arduino Mega button library
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Mega.
  • Open the Serial Monitor.
  • Press each key on the 1 by 4 keypad.
  • 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!