Spokes.wiki Search About
Created
Updated

Input Multiplexing

Reading more switches than you have pins. On a microcontroller the GPIO count is fixed by the package, and a project that runs out of pins before it runs out of buttons has to buy them back with something else — external components, flash bytes, analog headroom, or capability input-multiplexing-make.

Six named techniques, and none of them is free.

The options, and what each one costs

I²C port expanders (PCF8574, MCP23008, MCP23017) turn two bus lines into 8 or 16 pins. The cheapest in pins and the most expensive in code: 190 bytes of flash just to read an 8-bit expander, and the bus runs at 100 or 400 kHz.

Shift registers (74HC165 for input, 74HC595 for output) need three pins — latch, clock, data — reducible to one or two with effort, and cost less flash than an I²C driver. One chip does input or output, not both.

Matrix scanning wires switches at row/column crossings. It only pays at scale: a 2×3 matrix for six switches saves a single pin, while 4×4 saves eight. Detecting two presses at once requires a diode per switch, otherwise the matrix reports keys nobody touched.

Charlieplexing drives n(n−1) points from n pins by exploiting tri-state outputs. On two pins that is two switches, which is no gain at all, and it cannot reliably detect simultaneous presses however many pins you give it.

A resistive ladder puts switches across a divider and reads one ADC pin, decoding by voltage. Four resistors carry three switches. The cost moves into the analog domain: the worst-case gap between adjacent codes was 59 mV in the article's 3 V, 10-bit configuration, and the IF-ELSEIF decode chain took 162 bytes.

A diode ladder generates binary-weighted voltages from forward drops — Schottky BAS40 at 0.37 V, or silicon 1N4148 above 0.7 V. It buys resolution and pays in temperature: forward voltage drifts about −2 mV/°C, which an NTC in parallel can partly compensate.

The currency is never pins

Every row above is the same trade in a different unit. Pins are converted into flash bytes (I²C), board components (ladders, shift registers), analog noise margin (resistive), thermal stability (diode), or the plain loss of a feature — Charlieplexing and an undiode'd matrix both give up simultaneous presses, which is exactly what a handheld device with thumb controls needs.

On the parts this article targets — ATtiny-class MCUs with 1–2 kB of flash — the code cost is not a rounding error. An I²C expander's 190-byte read routine is roughly a tenth to a fifth of the entire program store, spent before the device does anything.

The other side of integration

microcontroller records that at the low end vendors compete on what analog the die swallowed, because integration deletes external parts. Input multiplexing runs the same trade backwards: it adds external parts and firmware to delete pins. Both are moves across the package boundary, and which direction is cheaper depends on whether the scarce thing this month is die area, board area, or program store — a concrete instance of the constraint thesis in embedded-systems.

microcontroller · embedded-systems · open-hardware · input-multiplexing-make