PinoutSearch

Read a 4x4 Keypad (Arduino)

Read keypresses from a 16-button matrix keypad — the input for door locks, calculators, and menus.

Beginner20 min

What you need

Wiring

Hover a pin or a wire to trace the connection.

Code

keypad.ino
// Print each key pressed on a 4x4 matrix keypad.
// Install the "Keypad" library by Mark Stanley.
#include <Keypad.h>

const byte ROWS = 4, 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); }

void loop() {
  char k = keypad.getKey();
  if (k) Serial.println(k);
}

Steps

  1. Wire rows R1–R4 to D9–D6 and columns C1–C4 to D5–D2 (check your keypad's pin order).
  2. Install the "Keypad" library.
  3. Upload, open the Serial Monitor, and press keys to see them printed.