Capacitive touch, MPR121

The NXP MPR121: twelve capacitive touch electrodes on one I2C chip, here wired to twelve foil keys. Aliases: MPR121, 12-key touch sensor.

The chip charges each electrode and measures the voltage it reaches. A finger adds capacitance, so the reading falls; when it falls far enough below the electrode's baseline, the electrode is touched.

Pins

Pin What it does
VIN Supply, 3 V to 5 V.
GND Ground.
SCL I2C clock.
SDA I2C data.
IRQ Open drain, low when the touches change, let go when the status is read. Optional.
ADDR Pulled down on the board, for address 0x5A. Not used by the model.

Properties

address is the I2C address, 0x5A (90) by default; 0x5B to 0x5D are the others.

While it runs

Hold a key to put a finger on that electrode; several at once is fine. A key lights when the chip reports it touched, which is after its next sample, and only once a sketch has started the chip.

What the model gets right

From the MPR121 datasheet:

Stop and run. The chip starts stopped. Writing a number of electrodes to the electrode configuration register (0x5E) starts it, and while it runs it ignores writes to the other registers, which is why a library writes its settings first.

The thresholds. Each electrode has its own touch and release threshold (0x41 to 0x5A); touched when the baseline less the reading is over the touch threshold, released when it is under the release threshold.

The status and IRQ. Two bytes of touch status at 0x00, and IRQ low until they are read.

The sample interval. A new reading every 1 ms to 128 ms, as the second front-end register (0x5D) says: 16 ms after a reset, 1 ms once Adafruit's library sets it up.

What it does not model

The capacitance itself: an untouched electrode reads 700 counts and a finger takes 40 off, numbers chosen to clear the Adafruit library's default threshold of 12. The baseline does not drift, and the filter, debounce and auto-configuration registers are stored but do not change anything. Proximity sensing and the GPIO and LED drivers are not modeled.

Libraries

Adafruit MPR121 (Adafruit_MPR121.h): cap.begin(0x5A), then cap.touched() for the twelve bits.

Common mistakes

Writing a threshold while the chip runs. It is ignored; stop the chip first (the library does this for you).

Two MPR121s at the same address. Strap the second one's ADDR and change address.

See it in action

Touch piano plays a note on a piezo for each of the twelve keys. Open it at /templates.