Black Pill capacitive touch keypad
Build it step by step
0 of 7 doneWire the programmer
SWDIO, SWCLK, GND and 3.3V.
Wire the sensor and display
SDA to PB7, SCL to PB6, power at 3.3 V.
Make the pads
Stick twelve foil squares on card and attach a short wire to each.
Connect the pads
Pads 0 to 11 on the sensor, one pad each.
Install the libraries
Adafruit MPR121, Adafruit SSD1306 and Adafruit GFX.
Upload the sketch
The display shows the title and an empty line.
Touch the pads
The digits appear on the display.
How do you build a black Pill capacitive touch keypad?
A Black Pill reads an MPR121 touch sensor over I2C, treats foil pads as keys and shows the typed digits on an OLED. You need STM32 Black Pill (STM32F411CEU6), ST-Link V2 programmer (clone), MPR121 capacitive touch sensor, SSD1306 OLED Display and Breadboard 830 Tie Points. It takes 7 steps and about 150 minutes.
- Level
- Intermediate
- Time
- About 150 min
- Parts
- 5
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| ST-Link GND | Board GND | Ground |
| ST-Link SWDIO | Board DIO | Debug data |
| ST-Link SWCLK | Board DCLK | Debug clock |
| ST-Link 3.3V | Board 3.3 pin | Power from the programmer (do not also plug in USB) |
| MPR121 VIN and OLED VCC | Black Pill 3.3 pin | Power at 3.3 V |
| MPR121 GND and OLED GND | Black Pill GND | Common ground |
| MPR121 SDA and OLED SDA | Black Pill PB7 | Shared I2C data |
| MPR121 SCL and OLED SCL | Black Pill PB6 | Shared I2C clock |
| Twelve foil pads | MPR121 pads 0 to 11, one wire each | Touch keys |
Code
A touch pad is a patch of metal that forms one plate of a tiny capacitor, with your finger as the other plate. Your finger changes the capacitance a little, and the MPR121 chip measures that change on twelve pads at once and reports which are touched, through a register you read over I2C. Because the chip tracks a slow baseline, it ignores slow changes such as humidity or a nearby hand and responds only to a real touch. The program reads a 12-bit number where each bit is a pad, finds the bits that changed from not touched to touched since the last read, and adds the matching character to the typed text. Foil squares about 3 centimetres across taped to a sheet of card, each with a short wire to the board, make a good keypad. Keep the wires short and separated, as long wires pick up noise, and do not touch the pads while the board starts, because the sensor sets its baseline then.
Runs on a virtual STM32 Blue Pill in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <Wire.h>
#include <Adafruit_MPR121.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_MPR121 cap;
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const char KEYS[12] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '*', '0', '#'};
String typed = "";
uint16_t lastTouched = 0;
void draw() {
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(1);
oled.setCursor(0, 0);
oled.print("Touch keypad (* clear)");
oled.setTextSize(2);
oled.setCursor(0, 28);
oled.print(typed);
oled.display();
}
void setup() {
Wire.begin(); // I2C1 uses PB7 for SDA and PB6 for SCL
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while (true) {} }
if (!cap.begin(0x5A)) { while (true) {} } // the usual MPR121 address
cap.setThresholds(12, 6); // touch and release limits
draw();
}
void loop() {
uint16_t now = cap.touched(); // one bit per pad
uint16_t pressed = now & ~lastTouched; // pads that were just touched
lastTouched = now;
for (int i = 0; i < 12; i++) {
if (pressed & (1 << i)) {
if (KEYS[i] == '*') typed = "";
else if (typed.length() < 10) typed += KEYS[i];
draw();
}
}
delay(20);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Touch pad 0 | 1 appears |
| Touch pads 0, 1, 2 | 123 appears |
| Touch the star pad | Clears the line |
| Hold a pad | One character only |
| Touch two pads at once | Both characters appear |
If it does not work
Characters appear on their own
Keep your hands away from the pads at start-up, and shorten the wires.
Nothing happens
Check the sensor address is 0x5A and the I2C wires are on PB7 and PB6.
Hard to trigger
Lower the touch threshold, for example 8 instead of 12.
Several pads fire at once
Space the pads further apart and keep wires apart.
Display blank
Check the I2C address 0x3C and the 3.3 V power.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the black Pill capacitive touch keypad?
STM32 Black Pill (STM32F411CEU6), ST-Link V2 programmer (clone), MPR121 capacitive touch sensor, SSD1306 OLED Display and Breadboard 830 Tie Points.
How long does the black Pill capacitive touch keypad take to build?
About 150 minutes for a intermediate maker, in 7 steps.
How do I know the black Pill capacitive touch keypad works?
Touch pad 0: 1 appears. Touch pads 0, 1, 2: 123 appears. Touch the star pad: Clears the line.
Characters appear on their own: what should I check?
Keep your hands away from the pads at start-up, and shorten the wires.
Nothing happens: what should I check?
Check the sensor address is 0x5A and the I2C wires are on PB7 and PB6.
Hard to trigger: what should I check?
Lower the touch threshold, for example 8 instead of 12.
Several pads fire at once: what should I check?
Space the pads further apart and keep wires apart.
