Source: https://mokxi.com/libraries/keypad
Updated: 2026-11-02

Libraries / Input
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.

Open the example in the editor See the calls

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 name | Keypad

Author | Mark Stanley, Alexander Brevig

Include | #include <Keypad.h>

Boards | every board Mokxi compiles for

In the simulator | Simulated: it runs against the part the way it does on the bench.

Parts it drives | Keypad, 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’s 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 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

### 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.

## Projects that use Keypad

Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.

Membrane keypad on pins 9 to 2 A 4x4 membrane keypad with its rows on pins 9, 8, 7 and 6 and its columns on 5, 4, 3 and 2: the Keypad library prints every key that is pressed. Combination lock: keypad, servo bolt and buzzer Type 1234 on a keypad (rows 11 to 8, columns 7 to 4) and a servo on pin 12 draws the bolt for two seconds; each key clicks a buzzer on pin 13, and a wrong code sounds a long beep. Pot-of-gold treasure box Type the lucky code on a 4x4 keypad and a servo unlocks the treasure box, a ring of LEDs shimmers gold inside and a piezo plays a tune. Vending machine Four spiral slots on continuous servos, a keypad, coins and exact change, an LCD price list, a drop sensor that refunds a failed vend, sold out counts and a service code. Macro pad for streamers and coders A Pro Micro that plugs in as a keyboard: twelve keys of shortcuts, a volume knob, three layers with a lamp each and an OLED, with every keystroke it sends shown in the Serial Monitor. Escape-room puzzle box Five puzzles in ten minutes: a keypad code, a magnet key, the right RFID card, a tilt sequence and three flashes of light.

## Keep going

### Learn

- Arduino 4x4 Keypad Code Lock with a Relay

### Parts

- Keypad, 4x4
- Keypad, 4x3 telephone
- Keypad, 5x4 calculator

### More input libraries

- XPT2046_Touchscreen
- OneButton
- Encoder
- RotaryEncoder
- CapacitiveSensor
- IRremote
- Adafruit MCP23017 Arduino Library
- PCF8574 (Rob Tillaart) and PCF8574 library (Renzo Mischianti)

Every built-in library · Open the editor

## Try Keypad without installing anything

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

Start building free

Open the editor
