Adafruit MPR121: twelve capacitive touch inputs over I2C
Yes. You can use the Adafruit MPR121 library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Capacitive touch, MPR121). The example below, "Touched and released, twelve electrodes, with Adafruit MPR121", runs on the Arduino Uno R3.
Built in on Mokxi: #include <Adafruit_MPR121.h> and it builds, with nothing to install. Free, and no account needed.
What Adafruit MPR121 does
The MPR121 is a capacitive touch controller with twelve electrodes, so twelve pads of copper tape, fruit or conductive paint become buttons with just two I2C wires. It tracks each electrode’s capacitance itself, keeps a slowly moving baseline for each, and flags a touch when the reading drops far enough below it. That makes it a common pick for a touch piano, a talking poster or a fruit keyboard game.
Adafruit_MPR121 hides the register setup. begin() takes the address (0x5A, or 0x5B to 0x5D with the ADDR pin moved) and returns false when nothing answers. touched() returns all twelve states as one 16-bit number, one bit per electrode, so a sketch finds new touches by comparing it with the last value, exactly as the example does with lasttouched and currtouched.
For tuning, filteredData() and baselineData() give the raw numbers for an electrode, and setThresholds() moves the touch and release levels for all twelve at once. In Mokxi the part on A4 and A5 reports touches from the canvas and the example prints electrode 3 touched and released; the chip’s auto-configuration is written but not modeled, so each electrode’s readings are fixed values, touched or not.
| Library Manager name | Adafruit MPR121 |
|---|---|
| Author | Adafruit |
| Include | #include <Adafruit_MPR121.h> |
| Boards | every board Mokxi compiles for but ATtiny85 |
| In the simulator | Simulated: it runs against the part the way it does on the bench. |
| Parts it drives | Capacitive touch, MPR121 |
Install it
It builds on every board Mokxi compiles for but ATtiny85.
In Mokxi it is built in: #include <Adafruit_MPR121.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "Adafruit MPR121" by Adafruit from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Adafruit MPR121 to lib_deps in PlatformIO. Mokxi follows the calls of release 1.2.1, so a sketch written here builds there too.
What to know in the simulator: Auto-configuration is written to the chip but not modeled; the readings are fixed per electrode, touched or not.
- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- ESP32-S3-CAM
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi
The calls you will use
In the order a sketch meets them.
Adafruit_MPR121 cap = Adafruit_MPR121()
The touch controller object. The address is given to begin(), not here.
cap.begin(0x5A)
Resets and configures the chip at that address and returns false if it does not answer.
cap.touched()
The touch state of all twelve electrodes as bits 0 to 11 of a uint16_t. Test one with currtouched & _BV(i).
uint16_t now = cap.touched();
if ((now & _BV(3)) && !(last & _BV(3))) Serial.println("3 touched");
last = now;cap.filteredData(3)
Electrode 3’s current filtered reading. It drops when a finger comes close.
cap.baselineData(3)
The slowly tracked untouched level for electrode 3. A touch is the filtered value falling far enough below this.
cap.setThresholds(12, 6)
Sets the touch and release thresholds for every electrode. Lower numbers are more sensitive; keep release below touch.
Example: Touched and released, twelve electrodes, with Adafruit MPR121
On the Arduino Uno R3. It runs in the browser exactly as printed.
#include <Wire.h>
#include "Adafruit_MPR121.h"
Adafruit_MPR121 cap = Adafruit_MPR121();
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
void setup() {
Serial.begin(9600);
Serial.println("Adafruit MPR121 Capacitive Touch sensor test");
if (!cap.begin(0x5A)) {
Serial.println("MPR121 not found, check wiring?");
while (1);
}
Serial.println("MPR121 found!");
}
void loop() {
currtouched = cap.touched();
for (uint8_t i = 0; i < 12; i++) {
if ((currtouched & _BV(i)) && !(lasttouched & _BV(i))) {
Serial.print(i); Serial.println(" touched");
}
if (!(currtouched & _BV(i)) && (lasttouched & _BV(i))) {
Serial.print(i); Serial.println(" released");
}
}
lasttouched = currtouched;
delay(10);
}Wiring
- Arduino Uno R3 A4 to Capacitive touch, MPR121 SDA
- Arduino Uno R3 A5 to Capacitive touch, MPR121 SCL
- Arduino Uno R3 5V to Capacitive touch, MPR121 VIN
- Arduino Uno R3 GND to Capacitive touch, MPR121 GND
Common errors and how to fix them
MPR121 not found, check wiring?
begin() got no answer at the address. Check SDA to A4 and SCL to A5 on an Uno, that the breakout has power, and the ADDR pin: tied to ground is 0x5A, to 3.3 V is 0x5B.
Pads trigger by themselves or never release
Long wires or large pads add capacitance the default thresholds do not expect, and the baseline is captured at start. Keep leads short, do not touch pads during begin(), and raise the thresholds with setThresholds().
Every touch prints over and over while the finger stays on
The sketch prints whenever the bit is set instead of when it changes. Keep the previous touched() value and compare, so each touch and each release prints once.
Questions and answers
Can I use more than twelve pads?
Yes. Up to four MPR121 boards share one I2C bus at 0x5A to 0x5D, each with its own Adafruit_MPR121 object, for 48 pads.
Projects that use Adafruit MPR121
Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.
Keep going
Try Adafruit MPR121 without installing anything
Open the example, change the sketch, and keep your version in a free account.