Arduino library 路 Input

Keypad: reading a 4x4 membrane keypad

A membrane keypad has no electronics in it, only a grid of wires: four rows and four columns on a 4x4 pad, with each key joining one row to one column when pressed. The Keypad library, by Mark Stanley and Alexander Brevig, scans that grid. It drives one column at a time, reads which rows follow, and turns the result into the character printed on the key.

Built in on Mokxi: #include <Keypad.h> and it builds, with nothing to install. Free, and no account needed.

What Keypad does

The 4x4 pad has eight pins and the 4x3 phone-style pad seven. Looking at the front, they usually run rows first then columns, but cheap pads differ, so the safest approach is to wire them in order and fix the keymap if the wrong keys come out.

getKey() is the call to use in loop(). It returns at once, with the key just pressed or NO_KEY when nothing new happened, so the rest of the sketch keeps running. The library debounces each key for you, so one press is one character.

Library Manager nameKeypad
AuthorMark Stanley, Alexander Brevig
Include#include <Keypad.h>
Boardsevery board Mokxi compiles for
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesKeypad, 4x4, Keypad, 4x3 telephone, Keypad, 5x4 calculator

Install it

It builds on every board Mokxi compiles for.

In Mokxi it is built in: #include <Keypad.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Keypad" by Mark Stanley, Alexander Brevig from the Arduino IDE鈥檚 Library Manager (Sketch, Include Library, Manage Libraries), or add Keypad to lib_deps in PlatformIO. Mokxi follows the calls of release 3.1.1, so a sketch written here builds there too.

ESP32-C3 路 Arduino Uno 路 Arduino Nano 路 Arduino Mega 2560 路 Arduino Leonardo 路 SparkFun Pro Micro 路 Arduino Micro 路 ESP32-C6 路 ATtiny85 路 Raspberry Pi Pico 路 Raspberry Pi Pico W 路 Seeed XIAO SAMD21 路 STM32 路 STM32 Nucleo-F411RE 路 STM32 Blue Pill 路 micro:bit V2 路 Arduino Uno R4 路 ESP32 DevKit 路 ESP32-S3-DevKitC-1 路 ESP8266 NodeMCU 路 Arduino Pro Mini 3.3 V 路 Seeed XIAO ESP32-C3 路 ESP32-C3 SuperMini 路 Seeed XIAO ESP32-C6 路 ESP32-DevKitC V4 路 LOLIN32 Lite 路 Wemos D1 R32 路 Cheap Yellow Display 路 TTGO T-Display 路 Seeed XIAO ESP32-S3 路 Arduino Nano ESP32 路 Arduino Nano RP2040 Connect 路 Seeed XIAO RP2040 路 Waveshare RP2040-Zero 路 Arduino Uno R4 WiFi

The calls you will use

In the order a sketch meets them.

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS)

Creates the keypad from a 2D array of the characters on its keys, the row pins and the column pins.

char keys[4][4] = {
  {'1','2','3','A'}, {'4','5','6','B'},
  {'7','8','9','C'}, {'*','0','#','D'} };
byte rowPins[4] = {9, 8, 7, 6};
byte colPins[4] = {5, 4, 3, 2};

char key = keypad.getKey()

The key pressed since the last call, or NO_KEY (zero). Test it with if (key) before using it.

keypad.waitForKey()

Stops the sketch until a key is pressed and returns it. Handy in a menu, but nothing else runs while it waits.

keypad.getState()

The state of the last key: IDLE, PRESSED, HOLD or RELEASED, for telling a tap from a long press.

keypad.setHoldTime(1000)

How many milliseconds a key must be held before its state becomes HOLD.

keypad.addEventListener(keypadEvent)

Calls your function, with the key, every time any key changes state, so presses and releases can be handled in one place.

Example: Print the key pressed on a 4x4 keypad

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino 路 Arduino Uno R3
#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup() {
  Serial.begin(9600);
  Serial.println("Press a key");
}

void loop() {
  char key = keypad.getKey();
  if (key) {
    Serial.print("Key: ");
    Serial.println(key);
  }
}

Wiring

  • Arduino Uno R3 9 to Keypad, 4x4 R1
  • Arduino Uno R3 8 to Keypad, 4x4 R2
  • Arduino Uno R3 7 to Keypad, 4x4 R3
  • Arduino Uno R3 6 to Keypad, 4x4 R4
  • Arduino Uno R3 5 to Keypad, 4x4 C1
  • Arduino Uno R3 4 to Keypad, 4x4 C2
  • Arduino Uno R3 3 to Keypad, 4x4 C3
  • Arduino Uno R3 2 to Keypad, 4x4 C4

Open it in the editor, wired and ready to run

Common errors and how to fix them

Pressing 1 prints 4, or keys come out in a different order

The row and column pins are swapped or reversed against your pad. Swap the rowPins and colPins arrays, or reverse one of them, until each key prints its own label.

Some keys never register

Rows or columns are on pins 0 and 1, which Serial uses, or one wire is loose. Move the keypad to other pins and press each key of the dead row or column to find the bad wire.

'makeKeymap' was not declared in this scope

The library header is missing or misspelled. Add #include <Keypad.h> at the top of the sketch and install Keypad by Mark Stanley and Alexander Brevig from the Library Manager.

Try Keypad without installing anything

Open the example, change the sketch, and keep your version in a free account.