Silver woven stretch conductive fabric, scrunched

Build a Soft Touch Button From Conductive Fabric and an ESP32

Updated

One of my favourite things to show a new group of students is a button with no moving parts. You touch a patch of fabric, an LED turns on, and the room goes quiet for a second while everyone works out how it did that. The trick is that the ESP32 has capacitive touch built in, so a piece of conductive fabric can act as the button. No switch, no extra chip. In this tutorial we will build one in about half an hour.

What you need

Step 1: Prepare the fabric pad

Cut a square about 3 cm across. Smaller pads work, but a pad around the size of a fingertip or a little larger is easier to trigger reliably. Lay it on a piece of ordinary cotton or felt so it does not touch anything else. If you sew it down, use a regular thread around the edges and keep the conductive thread only where you need the connection. (don't forget you can cut it into any shape you want)

For a first test, skip the sewing. Just clip an alligator lead onto one corner of the fabric. You can make it permanent once you know it works.

Step 2: Wire it up

Connect the other end of the lead to GPIO 4 on the ESP32. On the original ESP32, GPIO 4 is touch channel T0, so it is a good place to start. The ESP32 has several touch-capable pins, including GPIO 2, 12, 13, 14, 15, 27, 32 and 33, but I like 4 because it is not one of the pins that affects the boot process. If you want a refresher on which pins to trust, the pinout video is here: Why your ESP32 pinout is confusing (and how to fix it).

You do not need a resistor or a second connection for the touch input. Your finger and the fabric form the circuit with the chip's own sensing hardware.

Step 3: Set up PlatformIO

Create a new project and use this platformio.ini:

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200

Step 4: The code

Put this in src/main.cpp:

#include <Arduino.h>

const int TOUCH_PIN = 4;   // T0
const int LED_PIN = 2;     // built-in LED on many boards
int threshold = 30;        // we will tune this

void setup() {
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  int value = touchRead(TOUCH_PIN);
  Serial.println(value);
  digitalWrite(LED_PIN, value < threshold ? HIGH : LOW);
  delay(50);
}

Upload it and open the serial monitor. You will see a stream of numbers. On the original ESP32, the reading drops when something conductive, like your finger, touches or approaches the pad.

Step 5: Tune the threshold

This is the step people skip, and it is the one that makes the button feel good. Watch the numbers while you leave the pad alone, and write down a typical value. Then touch the pad firmly and write down that value. Set your threshold somewhere between the two, closer to the touched number so that stray movement does not trigger it. Your numbers will differ from mine depending on the fabric size, the lead length and even the weather, so tune it with your own setup, not with a number from a tutorial.

Troubleshooting

  • Readings jump around: shorten the lead, and keep it away from other wires. Long leads act like antennas.
  • Nothing changes when you touch: check that the clip is actually in contact with the metallic surface of the fabric, and that you are on the right pin.
  • The board will not start: unplug anything from the strapping pins (0, 2, 5, 12 and 15) and try again.
  • It triggers when you walk past it: lower the threshold slightly, or average several readings.

Level up: three pads and a NeoPixel strip

One button is a neat trick. Three buttons is a controller. In this version, three fabric pads are wired to three touch pins, and each one runs its own light effect on a strip of NeoPixels: a colour wipe, a rainbow and a sparkle. Try it below first.

What you need (on top of the list above)

  • Two more pieces of conductive fabric, cut to the same size as your first pad
  • A Starter Strip: 10 WS2812b 'Neo' Strip. You can order it pre-soldered with soft silicone wire if you'd rather not solder
  • Three jumper wires for the strip

Wiring

  • Pad A to GPIO 4 (T0)
  • Pad B to GPIO 27 (T7)
  • Pad C to GPIO 32 (T9)
  • Strip 5V to the board's 5V pin (labelled VIN on some boards)
  • Strip GND to GND
  • Strip DIN to GPIO 18

None of these pins are strapping pins, so the board still boots normally with everything connected. Keep the pads at least 2 cm apart, and make sure the three threads or leads never touch each other, or one touch will trigger two pads. Ten pixels at the brightness set in the code are fine on USB power. For longer strips, use a separate 5V supply and connect its ground to the ESP32's ground.

platformio.ini

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
lib_deps = adafruit/Adafruit NeoPixel

The code

Replace src/main.cpp with this:

#include <Arduino.h>
#include <Adafruit_NeoPixel.h>

const int PIXEL_PIN = 18;              // strip DIN
const int NUM_PIXELS = 10;             // Starter Strip has 10 LEDs
const int TOUCH_PINS[3] = {4, 27, 32}; // T0, T7, T9

Adafruit_NeoPixel strip(NUM_PIXELS, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
int thresholds[3];

void setup() {
  Serial.begin(115200);
  strip.begin();
  strip.setBrightness(60);             // easy on USB power
  strip.show();

  // Hands off the pads: measure each one and set its threshold
  for (int i = 0; i < 3; i++) {
    long sum = 0;
    for (int n = 0; n < 20; n++) {
      sum += touchRead(TOUCH_PINS[i]);
      delay(10);
    }
    thresholds[i] = (sum / 20) * 7 / 10;
  }
}

int touchedPad() {
  for (int i = 0; i < 3; i++) {
    if (touchRead(TOUCH_PINS[i]) < thresholds[i]) return i;
  }
  return -1;                           // nothing touched
}

void colourWipe(unsigned long t) {     // Pad A
  int lit = min((int)(t / 80) + 1, NUM_PIXELS);
  for (int p = 0; p < NUM_PIXELS; p++) {
    strip.setPixelColor(p, p < lit ? strip.Color(237, 147, 177) : 0);
  }
}

void rainbow(unsigned long t) {        // Pad B
  for (int p = 0; p < NUM_PIXELS; p++) {
    uint16_t hue = t * 30 + p * (65536L / NUM_PIXELS);
    strip.setPixelColor(p, strip.gamma32(strip.ColorHSV(hue)));
  }
}

void sparkle() {                       // Pad C
  for (int p = 0; p < NUM_PIXELS; p++) {
    strip.setPixelColor(p, random(4) == 0 ? strip.Color(93, 202, 165) : 0);
  }
}

void loop() {
  static int lastPad = -1;
  static unsigned long start = 0;

  int pad = touchedPad();
  if (pad != lastPad) {                // a new touch restarts the effect
    start = millis();
    lastPad = pad;
  }
  unsigned long t = millis() - start;

  if (pad == 0) colourWipe(t);
  else if (pad == 1) rainbow(t);
  else if (pad == 2) sparkle();
  else strip.clear();
  strip.show();

  Serial.printf("A:%d B:%d C:%d\n", touchRead(TOUCH_PINS[0]),
                touchRead(TOUCH_PINS[1]), touchRead(TOUCH_PINS[2]));
  delay(pad == 2 ? 80 : 20);
}

This version measures each pad as the board starts and sets its threshold automatically at 70% of the untouched reading, so keep your hands off the pads for the first second after powering up. If a pad is too jumpy or not sensitive enough, change the 7 / 10 to 6 / 10 or 8 / 10, or tune each threshold by hand as in Step 5, using the readings in the serial monitor.

Where to take it next

Once your pads work, swap the effects for your own, or point one pad at an OLED message instead of the strip. You can sew more pads onto a sleeve or a glove and give each its own touch pin, which is the idea behind soft keypads and wearable controllers. If you want to try different materials before you commit, the 5 Samples: Conductive Fabric pack lets you compare how each one feels and responds.

When you finish yours, I would love to see it. Tell me what you ended up triggering with it.

Christine

Back to blog

Leave a comment