Interfacing Keypad

Matrix keypad scanning.

Mohith N
Updated: 19 March 2026
12 min read

A matrix keypad is a grid arrangement of push-button switches that allows multiple keys to be read using a minimal number of GPIO pins. Rather than requiring one GPIO pin per key, a matrix keypad with R rows and C columns requires only R+C pins to scan all R x C keys. A standard 4x4 keypad provides 16 keys using just 8 GPIO lines, making it the most practical input interface for embedded systems with limited pin availability.

4x4 Matrix Keypad Structure and GPIO ConnectionMatrix Key GridC1C2C3C4R1R2R3R4123A456B789C*0#DMCU GPIO AssignmentRows (Output / Driven)Row1 - GPIO_PA0Row2 - GPIO_PA1Row3 - GPIO_PA2Row4 - GPIO_PA3Columns (Input / Pull-up)Col1 - GPIO_PB0Col2 - GPIO_PB1Col3 - GPIO_PB2Col4 - GPIO_PB3Scanning: Drive row LOWRead all columns for LOW4x4 keypad: 16 keys using only 8 GPIO pins (4 rows + 4 cols)
Figure 1: 4x4 matrix keypad structure showing row-column grid, key positions, and MCU GPIO assignment for row-column scanning

Core Concept Explanation

In a matrix keypad, each key sits at the intersection of a row conductor and a column conductor. When a key is pressed, it electrically connects its row line to its column line. Without any scanning, all keys would appear to be in the same undifferentiated state. The scanning algorithm identifies which key is pressed by systematically activating one row at a time and observing which column line responds.

The microcontroller configures the row GPIO pins as digital outputs and the column GPIO pins as digital inputs with internal pull-up resistors enabled. During scanning, all row pins are initially HIGH. The scanner then drives one row LOW while keeping all other rows HIGH. It then reads all four column pins. If a key in that row is pressed, the corresponding column pin reads LOW because the key connects the driven-LOW row to that column, overriding the pull-up. A HIGH column reading means no key is pressed in that column for the currently active row.

After scanning all rows, the software uses a key map lookup table to translate the (row, column) pair into the corresponding key character or value. This approach is software-driven and works for any matrix size. The process repeats continuously at a rate fast enough to detect any key press but slow enough to allow debouncing between reads.

Mathematical Expression

The number of keys that can be scanned with R row pins and C column pins is simply R x C. This is the efficiency advantage of matrix scanning. For 8 GPIO pins split as 4 rows and 4 columns: Keys = 4 x 4 = 16. With direct connection (no matrix), 16 keys would need 16 GPIO pins plus common ground.

Scan rate and debounce timing are related mathematically. If each row is driven LOW for time t_row and there are R rows, the full scan cycle time is T_scan = R x t_row. For reliable debounce, T_scan should be between 5 ms and 20 ms. For a 4-row keypad with t_row = 3 ms, T_scan = 12 ms, which corresponds to a key scan rate of approximately 83 scans per second. This is sufficient for human key presses which last at least 50 ms.

Practical Understanding

Contact bounce occurs when a mechanical key is pressed: the contacts briefly make and break contact multiple times within 5 to 20 ms before settling. Without debouncing, a single key press may register multiple times. Software debouncing waits for the column reading to be stable for two or more consecutive scans before accepting it as a valid press. A common approach is to require the same key to be detected LOW for at least two consecutive scan cycles separated by 10 ms.

Ghost key detection is a concern in matrix keypads when multiple keys are pressed simultaneously. A ghost key appears when three corners of a rectangle in the matrix are pressed, causing the microcontroller to incorrectly read the fourth corner as also pressed. This occurs because current flows through the two pressed keys on the same column and falsely activates the unconnected row junction. Adding diodes in series with each key prevents ghost keys by blocking reverse current paths.

In the firmware, the key scanning function should be called inside a timer interrupt service routine at regular intervals rather than in the main polling loop. This ensures consistent debounce timing regardless of other code execution time. The timer period is typically set to 5 ms. Each call reads one row sequentially, completing a full scan every 20 ms for a 4-row keypad.

Example
Given:
4x4 matrix keypad (4 rows, 4 columns)
Row 2 driven LOW, other rows HIGH
Column readings: C1=HIGH, C2=LOW, C3=HIGH, C4=HIGH

Why this formula applies:
Pressed key = intersection of driven-LOW row and LOW-reading column.

Key map array:
  char keys[4][4] = {{'1','2','3','A'},
                     {'4','5','6','B'},
                     {'7','8','9','C'},
                     {'*','0','#','D'}};

Identification:
Active Row = Row 2 (index 1)
Low Column = C2 (index 1)
Key = keys[1][1] = '5'

Debounce check:
Wait 10ms, scan again.
If Row2-C2 still LOW: confirmed press.

Final Answer:
Key '5' is pressed and confirmed after debounce.
Exam Tip: In matrix keypad scanning, rows are always driven as outputs (one LOW at a time) and columns are read as inputs with pull-ups. The key press is identified at the (LOW row, LOW column) intersection. Ghost keys appear when 3 corners of a key rectangle are simultaneously pressed and can be prevented by adding series diodes to each key.
Matrix Keypad Scanning Algorithm Flowchart and DebounceStart / Reset scanSet all rows HIGH (idle)Drive Row[i] LOW (i=0 to 3)Read all 4 column pinsAny colLOW?YESRecord (row i, col j)NOWait 10ms, re-scanLookup key map[i][j]i++; if i=4 goto StartGhost Key Problem3 corners pressed:R1C1, R1C2, R2C1Ghost appears at:R2C2 (false detection)Fix: Add series diodeper key (1N4148)
Figure 2: Complete matrix keypad scanning algorithm flowchart with debounce steps and ghost key prevention

Mechanism and Key Points

  • Rows are configured as output push-pull, columns as input with internal pull-up enabled. Scanning drives each row LOW one at a time and reads all columns.
  • A column reads LOW only when a key at the intersection of the active (LOW) row and that column is physically pressed, connecting the row line to the column line.
  • Debounce implementation: after detecting a LOW column, wait 10 ms and re-scan. Accept the key press only if the same (row, column) pair reads LOW again.
  • Ghost keys occur when three keys forming an L-shape are pressed simultaneously. Current paths through two of those keys create a false LOW on the fourth junction column. Series diodes prevent this.
  • Timer interrupt based scanning (every 5 ms) is preferred over main-loop polling because it gives consistent timing regardless of other code execution and ensures reliable debounce.

Quick Revision

  • Matrix keypad: R rows + C columns = R x C keys. 4x4 keypad needs only 8 GPIO pins for 16 keys.
  • Rows driven as output (one LOW at a time). Columns configured as input with pull-up enabled.
  • Key detected at: (active LOW row, LOW column) intersection. Look up key using char map[row][col].
  • Debounce: wait 10 ms after first detection, confirm same key reads LOW again before accepting.
  • Ghost key: false detection at 4th corner when 3 corners of a key rectangle are pressed. Fix with series diode per key.
  • Scan cycle time = number of rows x time per row. For 4 rows at 3 ms each: cycle = 12 ms (83 scans/second).
  • Exam trap: Columns must have pull-up resistors. Without pull-up, column input floats when no key is pressed, causing random false readings.

Keypad Interfacing Quiz

Test your understanding of matrix keypad scanning techniques.

Question 1 of 3

Q1.A 4x4 matrix keypad has 16 keys but requires only 8 GPIO lines. During column scanning, if column 2 is driven LOW and the microcontroller reads row 3 as LOW, which key is pressed (assuming rows and columns are 0-indexed)?