30 Days Lost in Space → Help Center

Components

Keypad (4x4)

Sixteen buttons, but only eight pins. The trick is a clever row-and-column scanning technique, and once you get it, you can build password locks and menu systems.

What is a Matrix Keypad?

Sixteen buttons that need only 8 pins. The trick: the buttons aren't wired individually. Each one sits where a row line crosses a column line, 4 rows × 4 columns = 16 crossings, and pressing a key connects its row to its column.

Your Kit's Keypad

The 30 Days keypad is a green circuit board with sixteen real push buttons, printed S1 to S16, and an 8-pin header down its left edge. The pin names are printed beside the header: from the top, C4, C3, C2, C1, R1, R2, R3, R4. Each button bridges one row line to one column line while you hold it down.

How Matrix Scanning Works

The Keypad library runs this every time you call getKey():

  1. Set the four row pins as inputs, held HIGH by the board's internal pull-ups
  2. Drive column 1 LOW and read all four rows: a row that reads LOW means the key at (that row, column 1) is down
  3. Release column 1 and repeat for columns 2, 3 and 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

Follow the Day 11 mission wiring:

| Keypad pins | HERO pins | |-------------|-----------| | R1, R2, R3, R4 | 5, 4, 3, 2 | | C1, C2, C3, C4 | 6, 7, 8, 9 |

Read down the header, C4 to R4, and the eight wires land on pins 9, 8, 7, 6, 5, 4, 3, 2 in order.

No resistors needed: the library uses the board's internal pull-ups.

Later missions move some wires to make room for other parts, so always copy the pin lines from the top of that day's sketch. Day 14 moves C4 to pin 13 (pin 9 drives the RGB LED's blue leg that day), and Days 28 and 29 put the rows on 9, 8, 7, 6 and the columns on 10, 11, 12, 13.

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 see a fourth "ghost" key at the corner. (Real keyboards add a diode per key to prevent this.) 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 | The keys become a keyboard: 15 notes, and D to stop | | 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: the row or column wires are in reverse order. Press 1: A means the column wires are backwards, * means the row wires are. 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, 5, 9 and D still read right, but 2 reads as 4 and A as *. Swap the two groups of four wires
  • Double letters from one press: raise setDebounceTime()