Projects / Capacitive touch slider

Capacitive touch slider

Intermediate2 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. ESP32

    Make five pads

    Copper tape on card.

  2. ESP32

    Wire them to electrodes 0 to 4

    Short wires.

  3. ESP32

    Print the raw readings

    Check each pad responds.

  4. ESP32

    Add the position code

    Print the 0 to 1 value.

  5. ESP328.5 mA

    Add the LED ring

    Map the position to brightness.

  6. ESP32

    Test taps and slides

    Tune the 250 ms and 0.1 limits.

  7. ESP32

    Add a cover layer

    Re-test.

In short

How do you build a capacitive touch slider?

Copper pads on a PCB form a slider that sets volume or brightness. You need ESP32 DevKit V1, MPR121 capacitive touch sensor and WS2812B ring 16 LEDs. It takes 7 steps and about 120 minutes.

Level
Intermediate
Time
About 120 min
Parts
3

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
MPR121 SDA and SCLESP32 GPIO21 and GPIO22I2C (address 0x5A)
MPR121 3V3 and GNDESP32 3V3 and GNDPower
MPR121 electrodes 0 to 4Five copper pads in a row, short wiresThe slider
LED ring DINESP32 GPIO5Brightness indicator
LED ring 5V and GNDESP32 VIN (5 V) and GNDPower (USB supplies 5 V)

Code

The MPR121 measures how much each pad's capacitance rises when a finger comes near. Instead of treating each pad as a button, the sketch weights the pad numbers by how much each one changed, so a finger between pad 2 and pad 3 gives a position of about 2.5; that is how five pads become a smooth slider. A tap shorter than a quarter of a second toggles the ring on and off without changing the level. For good results keep the pads the same size, a little wider than a fingertip, with a small gap between them, and put them on a PCB or a piece of card with copper tape, with a thin covering of plastic or paper on top. Keep the sensor wires short and away from mains cables, because they act as antennas. Do not touch the pads while the board starts up, since that sets the baseline, and if the response drifts, restart it. The LED ring is limited to low brightness to keep USB current down. Arduino IDE with the ESP32 boards package 2.0.x, board "ESP32 Dev Module".

Runs on a virtual ESP32 Dev Module 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).

// Capacitive touch slider: five pads on an MPR121 give a smooth position that sets the brightness of an LED ring. A short tap toggles it.
// Libraries: Adafruit MPR121, Adafruit NeoPixel. Board: ESP32.
#include <Wire.h>
#include <Adafruit_MPR121.h>
#include <Adafruit_NeoPixel.h>
Adafruit_MPR121 cap;
Adafruit_NeoPixel ring(12, 5, NEO_GRB + NEO_KHZ800);
float level = 0.5;          // 0 to 1
bool on = true, wasTouch = false;
unsigned long touchStart = 0;
float startPos = 0, pos = 0;
void render() {
  ring.setBrightness(40);                  // keeps the current low
  int lit = on ? (int)(level * 12.0 + 0.5) : 0;
  for (int i = 0; i < 12; i++) ring.setPixelColor(i, i < lit ? ring.Color(255, 160, 40) : 0);
  ring.show();
}
void setup() {
  Serial.begin(115200);
  if (!cap.begin(0x5A)) { Serial.println("MPR121 not found"); while (true) {} }
  ring.begin();
  render();
}
void loop() {
  bool touching = (cap.touched() & 0x1F) != 0;
  if (touching) {
    float sw = 0, sp = 0;
    for (int i = 0; i < 5; i++) {
      int d = cap.baselineData(i) - cap.filteredData(i);   // bigger drop means finger nearer
      if (d < 0) d = 0;
      sw += d; sp += (float)d * i;
    }
    if (sw > 0) {
      pos = sp / sw / 4.0;                 // 0 at the first pad, 1 at the last
      if (!wasTouch) { touchStart = millis(); startPos = pos; }
      if (millis() - touchStart > 250 || fabs(pos - startPos) > 0.1) { level = pos; }   // a slide or hold sets the level
    }
  } else if (wasTouch) {
    if (millis() - touchStart < 250 && fabs(pos - startPos) < 0.1) on = !on;           // a short tap toggles
  }
  wasTouch = touching;
  render();
  delay(20);
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual ESP32 Dev Module with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

MPR121 not found

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Slide left to rightRing fills smoothly
Short tapToggles on and off
Hand near, not touchingNo false trigger
RebootBaseline re-learned

If it does not work

Position jumps

Pads are different sizes or wires are too long.

Does not respond

Check I2C address 0x5A and wiring.

Always on

Do not touch during start-up so the baseline is correct.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the capacitive touch slider?

ESP32 DevKit V1, MPR121 capacitive touch sensor and WS2812B ring 16 LEDs.

How long does the capacitive touch slider take to build?

About 120 minutes for a intermediate maker, in 7 steps.

How do I know the capacitive touch slider works?

Slide left to right: Ring fills smoothly. Short tap: Toggles on and off. Hand near, not touching: No false trigger.

Position jumps: what should I check?

Pads are different sizes or wires are too long.

Does not respond: what should I check?

Check I2C address 0x5A and wiring.

Always on: what should I check?

Do not touch during start-up so the baseline is correct.