Home / Projects

Project: make an ESP32 touch lamp with no buttons

A finger touching a foil pad wired to touch pin T0, GPIO 4, of an ESP32. The touch reading falls when you touch, and each tap steps an LED on GPIO 23 through off, dim, medium and bright
Beginner

Many lamps turn on when you touch the base. This project does the same with an ESP32: tap a piece of foil, a coin or just a bare wire, and an LED steps through off, dim, medium and bright, fading smoothly between them. There's no button. The ESP32 has touch sensing built in, and this project shows how it works and how to make it reliable.

Difficulty: beginner. Time: about 30 minutes. Works on: boards with the original ESP32 chip (ESP32-WROOM-32). The ESP32-S2 and S3 also have touch pins, but their readings rise when touched instead of falling, so the sketch would need changing. The C3 and C6 have no touch pins. This project is for a low-voltage LED only: never wire an ESP32 to a mains lamp.

What you need

  • An ESP32 development board and a USB data cable
  • One LED and a 220 Ω resistor (red, red, brown)
  • A touch pad: a square of kitchen foil, a coin, or just the end of a jumper wire
  • Two half-size breadboards and three jumper wires
Shopping list (6 items)

Some shop links are affiliate links (Amazon UK, Tooled Up): if you buy through one we may earn a small commission, at no extra cost to you. As an Amazon Associate I earn from qualifying purchases. It never changes which products we recommend, and no shop has paid to be included.

Everything for this build, in one place. Each link goes to a page we checked had the part in stock.

  • 1×
    ESP32 development board (ESP32-WROOM-32, 30 pins)A pack of two ELEGOO boards with USB-C and a CP2102 USB chip, so the spare can go in your next project. The board is wide: put it across two breadboards side by side so both rows of pins have free holes.
  • 1×
    USB-A to USB-C data cableThe boards come without a cable. These Anker cables carry data as well as charge, which uploading needs.
  • 1×
    5 mm LEDs, assorted coloursStandard LEDs, not the 'built-in resistor' kind.
  • 1×
    Resistor kit, 1/4 W (0.25 W) assorted valuesCovers every resistor in these projects.
  • 2×
    Half-size breadboards (400 points)Two side by side, with the ESP32 straddling the gap between them.
  • 1×
    Jumper wire kit (male-male, male-female and female-female)40 of each kind. Male-female wires go from a Raspberry Pi's pins to a breadboard.
  • Free
    Arduino IDEThe free Arduino editor. Nothing to buy.

Each button opens that shop's page for the part, checked when we wrote the list; if one shop is out of stock, try the next. Any equivalent part with the same value works.

How touch sensing works

Every object has some capacitance: it can store a little electric charge. Your body has far more than a piece of foil, so when you touch the pad, the pad's capacitance goes up. The ESP32 measures this by charging and discharging the pin over and over and counting how many cycles fit into a fixed time. More capacitance means slower cycles, so on the original ESP32 touchRead() gives a smaller number when you touch. You don't even need bare metal: touch works through thin tape or plastic, which is how real touch lamps hide their sensors.

The circuit

PartConnect to
Touch pad (foil, coin or bare wire end)GPIO 4 (touch pin T0)
LED long leg (anode), through 220 ΩGPIO 23
LED short leg (cathode)GND

With 3.3 V from the pin and about 2 V across a red LED, a 220 Ω resistor gives (3.3 − 2) ÷ 220 ≈ 6 mA. That's comfortably within what an ESP32 pin can supply. Check other LEDs with the LED resistor calculator.

  1. Unplug the ESP32 and put it across the two breadboards.
  2. Connect a jumper wire to GPIO 4 and tape its other end to a square of foil, or leave the wire end bare as the pad. Keep this wire short and away from the other wires.
  3. Wire the LED and resistor as in the table.

Setting up the Arduino IDE for the ESP32

If you've never used an ESP32 with the Arduino IDE, follow our ESP32 first steps guide first. In short: add Espressif's boards manager address in the IDE's preferences, install esp32 by Espressif Systems from the Boards Manager, then choose ESP32 Dev Module and your board's port. If an upload stops at "Connecting...", hold the board's BOOT button until the upload starts.

The sketch

Keep your hand away from the pad while the board starts, because it measures the untouched reading in the first half-second. Then open the Serial Monitor at 115200 baud and tap the pad. Each tap prints the reading and the new level.

// ESP32 touch lamp: tap a wire or a foil pad on GPIO 4 to step through off, dim, medium and bright.
const int TOUCH_PIN = T0;   // T0 is GPIO 4 on the original ESP32
const int LED_PIN = 23;
const int LEVELS[] = {0, 20, 80, 255};

int level = 0;          // 0 = off, 3 = brightest
int shown = 0;          // brightness right now, which fades towards the level
int baseline = 0;       // untouched reading
bool wasTouched = false;
unsigned long lastTap = 0;

int readTouch() {
  long sum = 0;
  for (int i = 0; i < 8; i++) sum += touchRead(TOUCH_PIN);  // average 8 readings to smooth out noise
  return sum / 8;
}

void setup() {
  Serial.begin(115200);
  delay(500);  // keep your hand away while it measures the untouched value
  baseline = readTouch();
  Serial.printf("Untouched reading: %d\n", baseline);
}

void loop() {
  int value = readTouch();
  bool touched = value < baseline * 7 / 10;  // on the original ESP32, the reading drops when you touch

  if (touched && !wasTouched && millis() - lastTap > 300) {
    level = (level + 1) % 4;
    lastTap = millis();
    Serial.printf("Tap (reading %d): level %d\n", value, level);
  }
  if (!touched) baseline = (baseline * 31 + value) / 32;  // follow slow changes in the room
  wasTouched = touched;

  int target = LEVELS[level];
  if (shown < target) shown++;
  if (shown > target) shown--;
  analogWrite(LED_PIN, shown);
  delay(2);
}

How it works

  • Smoothing. Touch readings are noisy, so readTouch() averages eight of them.
  • A threshold relative to the room. A fixed number would break when you use a bigger pad or a longer wire. Instead, the sketch counts it as a touch when the reading falls below 70 % of the untouched value, and keeps updating the untouched value slowly while nobody is touching, so the lamp copes with changes in temperature and humidity.
  • One tap, one step. The level only changes on the moment the pad goes from untouched to touched, so holding your finger on it doesn't keep cycling. A 300 ms lockout stops one touch counting twice.
  • Fading. analogWrite() sets the LED's brightness from 0 to 255 using PWM. Each pass of loop() moves the brightness one step towards the target, about every 2 ms, so a change from off to full takes about half a second. The levels are 0, 20, 80 and 255 rather than evenly spaced, because our eyes are much more sensitive to changes at low brightness.
  • Tested. We ran this sketch on a simulated ESP32 with taps, a long hold, a quick brush and a slowly drifting reading, checking that each counted tap moved one level and the LED faded to the right brightness.

Troubleshooting

  • It changes level by itself: the touch wire may be too long or near other wires. Shorten it, or lower the 7 in baseline * 7 / 10 to 6 so it needs a firmer touch.
  • Taps aren't noticed: look at the readings in the Serial Monitor. If touching only drops the number a little, use a bigger pad or raise the 7 to 8.
  • It only works while plugged into a laptop: touch sensing depends on a good ground. Some phone chargers float, so try a different USB supply.
  • The LED never lights: turn it round. The long leg goes towards GPIO 23.

Take it further

  • Use a strip of LEDs with a transistor or MOSFET for real light. Our guide to transistors as switches shows how.
  • Add a second pad on another touch pin to turn the lamp off in one tap.

Sources

The main references behind this guide. Numbers we worked out ourselves, such as the code outputs, come from running the code.