What is a Matrix Keypad?
Sixteen buttons that need only 8 pins. The trick: buttons aren't wired individually — they sit at the intersections of a grid. 4 row wires + 4 column wires = 16 crossing points, and pressing a key connects its row wire to its column wire.
Inside the Membrane
Your keypad is three thin plastic sheets. The top and bottom sheets have conductive ink traces (rows on one, columns on the other); the middle sheet is a spacer with holes at each key position. Press a key and the two traces touch through the hole. No springs, no metal parts — which is why it's flat, cheap, and rated for millions of presses.
How Matrix Scanning Works
The Keypad library runs this dance every time you call getKey():
- Drive row 1 LOW, leave the other rows HIGH
- Read all four column pins — a LOW column means the key at (row 1, that column) is down
- Repeat for rows 2, 3, 4
A full scan takes well under a millisecond. To your code, it just feels like asking "anything pressed?"
Key Layout
[1] [2] [3] [A]
[4] [5] [6] [B]
[7] [8] [9] [C]
[*] [0] [#] [D]
Wiring
The 8-pin ribbon carries the 4 row lines and the 4 column lines. Follow the Day 11 mission wiring:
| Keypad lines | HERO pins | |--------------|-----------| | Rows 1–4 | 5, 4, 3, 2 | | Columns 1–4 | 6, 7, 8, 9 |
No resistors needed — the library uses the board's internal pull-ups.
The Keypad Library
#include <Keypad.h>
| Function | What it does | |----------|--------------| | getKey() | Non-blocking: returns the pressed key, or NO_KEY if none. Call it every loop | | waitForKey() | Blocking: freezes everything until a key is pressed | | getState() | PRESSED, HOLD, RELEASED, IDLE — for hold-to-repeat tricks | | setDebounceTime(ms) | Adjust contact-bounce filtering (default 10ms) |
getKey() is the one you'll live in — it plays nicely with displays, buzzers, and everything else in the loop.
The Multi-Press Limit
The matrix can't reliably see combinations. Two keys in the same row or column are fine, but press three keys forming an L and the scan can hallucinate a fourth "ghost" key at the corner. (Real keyboards add a diode per key to prevent this; membrane keypads don't.) For this course it never matters — the missions read one key at a time.
Where You'll Use It
| Mission | What it does | |---------|--------------| | Day 11 | Your control panel comes online | | Day 12 | Sixteen keys become a piano | | Day 13–14 | Security codes and the advanced security panel | | Day 28–29 | Landing gear command input |
Common Mistakes
- Every key reads as the wrong key, in a mirrored pattern: row or column pin order is reversed — re-check against the table above
- One entire row or column is dead: that single wire is loose. The grid pattern of the failure tells you exactly which wire
- Rows and columns swapped: 1 reads as 5, 2 as 6… swap the two pin groups
- Double letters from one press: raise
setDebounceTime()