All articles

    The 4×4 Keypad with an Arduino: 16 Buttons on 8 Pins

    How a matrix keypad works: rows and columns and the scanning idea, wiring with 8 pins and finding their order with a multimeter, suitable pins on the Uno and ESP32, the Keypad library and long presses, entering a number, a password project that locks after wrong attempts, scanning without a library, ghosting, and saving pins with an I2C module.

    Updated 6 min read

    16 buttons, and if you wire each one to its own pin, the Arduino runs out of pins. The 4×4 keypad solves this with a clever idea: just 8 pins.

    The rows are scanned one after another, and from the row and column we know the button
    The rows are scanned one after another, and from the row and column we know the button

    The idea: a matrix

    The buttons are arranged in 4 rows × 4 columns. Each button, when pressed, connects a row to a column.

    The Arduino performs a fast scan:

    1. It pulls one row LOW and leaves the rest.
    2. It reads the columns (which have pull-ups). If a column goes LOW, the button at their intersection is pressed.
    3. It moves on to the next row, and so on, hundreds of times a second.

    The idea scales well: 16 buttons on 8 pins, and 64 buttons (8×8) on only 16 pins. It's the same thing inside a computer keyboard.

    Wiring

    The keypad has 8 wires. Usually the first 4 are the rows and the last 4 the columns (check your model). Connect them, for example, to pins 9 down to 2.

    Not sure of the order? Put a multimeter in continuity mode (the beeper), press button 1, and find the two pins that beep: they're the first row and the first column. Repeat with 5 and 2 and you'll work out the rest.

    There's also a 3×4 keypad (without A, B, C and D) with 7 pins, and the same code works once you change COLS to 3.

    Which pins?

    • On the Uno: avoid 0 and 1 (Serial), and the analog pins A0–A5 work as ordinary digital pins if you need them.
    • On the ESP32: pins 34–39 are input-only and have no internal pull-up, so they're no good for rows or columns. Use ordinary pins such as 13, 12, 14, 27, 26, 25, 33 and 32.

    The code (the Keypad library)

    #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 kp = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
    
    void setup() { Serial.begin(9600); }
    
    void loop() {
      char k = kp.getKey();
      if (k) Serial.println(k);
    }

    The library does the scanning and the debouncing by itself. And getKey() doesn't wait: if no button is pressed it returns zero immediately, so the program carries on. (There's waitForKey(), which waits, but it stops everything like delay.)

    Long presses

    The library tells you which state a button is in: pressed, held (HOLD), or released:

    void setup() {
      kp.setHoldTime(1000);          // one second = long press
      kp.addEventListener(onKey);
    }
    
    void onKey(KeypadEvent k) {
      if (kp.getState() == HOLD && k == '*') {
        // long press on *: clear everything typed
      }
    }
    
    void loop() { kp.getKey(); }     // must be called for events to fire

    Entering a number

    You want the user to type a temperature such as 27 and press #:

    String num = "";
    
    void loop() {
      char k = kp.getKey();
      if (!k) return;
      if (k >= '0' && k <= '9' && num.length() < 3) num += k;
      else if (k == '#') { int v = num.toInt(); /* use v */ num = ""; }
      else if (k == '*') num = "";
    }

    num.length() < 3 stops anyone typing a long number that breaks the program. And if that number is a setting, save it in the EEPROM so it isn't lost with the power.

    Project: a simple password

    String input = "";
    const String PASS = "1234";
    
    void loop() {
      char k = kp.getKey();
      if (!k) return;
      if (k == '#') {
        if (input == PASS) { /* open */ }
        input = "";
      } else if (k == '*') {
        input = "";
      } else {
        input += k;
      }
    }

    Show asterisks on a display, and drive the lock with a transistor.

    Make it harder to break

    • Lock out after 3 wrong attempts for 30 seconds (with millis, not delay, so the display keeps working).
    • Limit the input length so nobody types 1000 digits.
    • Store the password in EEPROM and provide a way to change it (for example: the old password, then A, then the new one twice).
    • Have the door open for only a few seconds and lock again by itself. A relay or a MOSFET drives the electric lock.

    This is a good learning project, but not a real security system: anyone who opens the box and joins two wires will open it.

    Without a library

    To understand what's happening, here's the whole scan:

    char scan() {
      for (int r = 0; r < 4; r++) {
        digitalWrite(rowPins[r], LOW);
        for (int c = 0; c < 4; c++) {
          if (digitalRead(colPins[c]) == LOW) {
            digitalWrite(rowPins[r], HIGH);
            return keys[r][c];
          }
        }
        digitalWrite(rowPins[r], HIGH);
      }
      return 0;
    }
    // in setup: rows OUTPUT and HIGH, columns INPUT_PULLUP

    This returns the button for as long as it's held, so you still need debouncing and to detect when it was first pressed. The library does all of that.

    Not enough pins?

    • A PCF8574 module on I2C puts the whole keypad on two pins (with a suitable library). You can share them with the display on the same bus at a different address.
    • Or an analog keypad: each button gives a different voltage on a single analog pin. Cheaper, but it detects only one button at a time. The same idea as the analog ladder in the buttons article.

    If two buttons are pressed together

    In a simple matrix, pressing 3 buttons in certain positions can make a fourth button appear pressed when it isn't (ghosting). For an ordinary keypad that's not a problem, because the user presses one button at a time. And if you build your own matrix and need many simultaneous presses (such as a music pad), put a diode in series with each button.

    Common faults

    ProblemUsual cause
    Wrong character (2 instead of 4)Rows and columns swapped, or the pin order in the code is wrong
    A whole row doesn't workThe row wire is disconnected or the pin is wrong
    No characters at allThe rows and columns are on pins that don't match the code
    The character repeatsReading without the library and without debouncing
    Works on the Uno but not the ESP32Pins from 34–39

    FAQ

    Is the membrane keypad (the thin stick-on one) the same as the button one? Exactly the same wiring and code. The membrane type is thinner, but has a shorter life under heavy use.

    Can I change the printed characters? Yes, change the keys array to any characters or symbols you want.

    Circuits Nerd
    © 2026 Circuits Nerd. All rights reserved.