Skip to main content
A standard 4x4 keypad. Great for numeric input, e.g. security pin code.

Pin names

R1
Row
Row 1 (top row)
R2
Row
Row 2
R3
Row
Row 3
R4
Row
Row 4 (bottom row)
C1
Column
Column 1 (left)
C2
Column
Column 2
C3
Column
Column 3
C4
Column
Column 4 (right)

Attributes

columns
string
default:"4"
Number of columns: “3” or “4”
keys
array
Labels for the keys
You can change the key labels as you like. The first four items in the array set the labels for the first row of keys, the next four items set the labels for the second row of keys, etc. Unicode characters are supported, so you can use special characters, accented letters, superscript/subscript (e.g. Xⁿ or A₁), and even emojis.

Arduino code example

The example below uses the Keypad library for Arduino. The key names set in the keys array define the values that keypad.getKey() returns. They don’t have to match the actual key labels (but it can be confusing if they don’t), and they must contain exactly one ASCII character.
#include <Keypad.h>

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

uint8_t colPins[COLS] = { 5, 4, 3, 2 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4

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

void setup() {
  Serial.begin(9600);
}

void loop() {
  char key = keypad.getKey();

  if (key != NO_KEY) {
    Serial.println(key);
  }
}
You can also try this example on Wokwi.
You can use keyboard shortcuts to activate the buttons on the keypad. Click on the keypad once (now the keypad is in focus), and you can press 0…9/A/B/C/D/#/* in the keyboard to activate the corresponding key.

Simulator examples

Build docs developers (and LLMs) love