Projects / Black Pill capacitive touch keypad

Black Pill capacitive touch keypad

Intermediate2.5 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. STM32ST

    Wire the programmer

    SWDIO, SWCLK, GND and 3.3V.

  2. STM32

    Wire the sensor and display

    SDA to PB7, SCL to PB6, power at 3.3 V.

  3. STM32

    Make the pads

    Stick twelve foil squares on card and attach a short wire to each.

  4. STM32

    Connect the pads

    Pads 0 to 11 on the sensor, one pad each.

  5. STM32MPR12115.8 mASSD1306

    Install the libraries

    Adafruit MPR121, Adafruit SSD1306 and Adafruit GFX.

  6. STM32

    Upload the sketch

    The display shows the title and an empty line.

  7. STM32MPR121

    Touch the pads

    The digits appear on the display.

In short

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

FromToWhy
ST-Link GNDBoard GNDGround
ST-Link SWDIOBoard DIODebug data
ST-Link SWCLKBoard DCLKDebug clock
ST-Link 3.3VBoard 3.3 pinPower from the programmer (do not also plug in USB)
MPR121 VIN and OLED VCCBlack Pill 3.3 pinPower at 3.3 V
MPR121 GND and OLED GNDBlack Pill GNDCommon ground
MPR121 SDA and OLED SDABlack Pill PB7Shared I2C data
MPR121 SCL and OLED SCLBlack Pill PB6Shared I2C clock
Twelve foil padsMPR121 pads 0 to 11, one wire eachTouch 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.

CheckExpected
Touch pad 01 appears
Touch pads 0, 1, 2123 appears
Touch the star padClears the line
Hold a padOne character only
Touch two pads at onceBoth 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 →

Ask Lark AI

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.