How to make any lamp a touch lamp (safely, with an ESP32)

Build a low-voltage touch lamp with an ESP32: tap any metal or foil part to step through three brightness levels, hold to switch off. Plus the safe way to make a mains lamp touch-controlled.
  • Level:Beginner
  • Duration: 40 min
  • Ages 12+
  • Platform: ESP32

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Make any lamp a touch lamp – EEMENTOR STEM™ guide

Quick answer: to make a mains lamp (one that plugs into the wall) touch-controlled, use a certified plug-in touch dimmer or smart plug, and never open or rewire the lamp yourself. To build your own touch lamp, use an ESP32: its touch pins notice your finger through any piece of metal or kitchen foil, so a metal lamp base, a foil-wrapped jar or a coin can become the switch. Below is a safe, low-voltage lamp with three brightness levels, just like a shop-bought touch lamp.

What you need

  • From the ESP32 IoT Starter Kit: the ESP32 board, expansion board and USB cable, breadboard and jumper wires, 1 RGB LED and 3 resistors (220 Ω)
  • A touch surface: kitchen foil, a metal jar lid, a coin or a metal lamp base, plus tape
  • A lamp shade: a paper lantern, a frosted glass jar or a ping-pong ball over the LED
  • The free Arduino IDE with ESP32 support (see Set up the ESP32 in the Arduino IDE)
Two safe ways to get a touch lamp: for a lamp that plugs into the wall use a certified plug-in touch dimmer and never open it; or build a low-voltage ESP32 lamp where any metal or foil part is the switch.
Never open a mains lamp. This guide builds the low-voltage one on the right.

Steps

  1. Build the circuit from the wiring table. Leave the touch wire loose for now.
  2. Make the touch pad. Tape a piece of foil (about 5 × 5 cm) to the end of a long jumper wire, or press the wire against a metal jar lid or lamp base and tape it in place.
  3. Upload the sketch without touching anything. At start-up it measures the pad's normal value, so keep your hands away for two seconds.
  4. Test it. Open the Serial Monitor at 115200 baud and tap the pad. Each tap steps low → medium → high → off. Hold for one second to switch off at once.
  5. Make it a lamp. Put the LED under the shade and hide the board in the base. The foil can go under paper, fabric or thin plastic; the ESP32 still feels your finger through it.
How the ESP32 feels a touch: a finger on the foil connected to GPIO 4 adds capacitance, so touchRead() drops below a threshold of 70 percent of the untouched baseline.
How the sketch decides that you touched the pad.

Full code

// Touch lamp with 3 brightness levels, like a shop-bought touch lamp.
// Tap: low, medium, high, off. Hold for 1 second: off.
const int TOUCH_PIN = 4;          // touch pin T0, wired to foil or metal
const int RED_PIN = 25, GREEN_PIN = 26, BLUE_PIN = 33;
const int LEVELS[] = {0, 40, 120, 255};   // off, low, medium, high
const char* NAMES[] = {"off", "low", "medium", "high"};

int baseline;                     // pad reading when nobody touches it
int level = 0;
bool wasTouched = false;
bool heldOff = false;
unsigned long pressStart = 0;

void setLamp() {
  int b = LEVELS[level];
  analogWrite(RED_PIN, b);              // red + some green + a little blue
  analogWrite(GREEN_PIN, b * 3 / 4);    // = warm white
  analogWrite(BLUE_PIN, b * 2 / 5);
  Serial.print("Lamp: ");
  Serial.println(NAMES[level]);
}

void setup() {
  Serial.begin(115200);
  delay(2000);                    // hands off: measure the untouched pad
  long sum = 0;
  for (int i = 0; i < 20; i++) { sum += touchRead(TOUCH_PIN); delay(20); }
  baseline = sum / 20;
  Serial.print("Baseline: ");
  Serial.println(baseline);
  setLamp();
}

void loop() {
  bool touched = touchRead(TOUCH_PIN) < baseline * 0.7;   // a finger lowers the value

  if (touched && !wasTouched) {              // a touch starts
    pressStart = millis();
    heldOff = false;
  }
  if (touched && !heldOff && millis() - pressStart > 1000) {   // held 1 s
    level = 0;
    setLamp();
    heldOff = true;
  }
  if (!touched && wasTouched && !heldOff) {  // a short tap ends
    level = (level + 1) % 4;
    setLamp();
  }
  wasTouched = touched;
  delay(20);
}
Each tap steps the lamp from off to low, medium and high brightness and back to off; holding the pad for one second switches it straight off.
What the lamp and the Serial Monitor show with each tap.

Wiring

Breadboard wiring diagram: RGB LED on ESP32 GPIO 25, 26 and 33 through three 220 Ω resistors with the common leg to GND, and a foil touch pad on GPIO 4.
Breadboard view. Check each connection against the wiring table below.
From To
ESP32 GPIO 4 (touch pin T0) A long jumper wire to the foil or metal touch surface
ESP32 GPIO 25 220 Ω resistor, then the RGB LED's red leg
ESP32 GPIO 26 220 Ω resistor, then the green leg
ESP32 GPIO 33 220 Ω resistor, then the blue leg
RGB LED longest leg (common) ESP32 GND

This sketch is for an RGB LED with a common cathode (the long leg goes to GND), like the one in our kit. Nothing in this project connects to mains electricity: the USB cable powers everything.

FREE

Build this circuit in EEMENTOR Lab

Wire it yourself in your browser before you touch a real wire. No sign-up, nothing to install.

The ESP32 and the parts for this lesson laid out in EEMENTOR Lab, ready to wireBuild it yourself
  1. The ESP32 and this lesson’s parts are already placed. Wire them using the wiring table above. Pins with no wire show red.
  2. Missing a part? Add it from the parts list on the left.
  3. Press Run, then try the inputs: click, hold or slide them under Selected.
  4. Then try this lesson’s sketch: paste it into the Code tab and press Compile & Run.

EEMENTOR Lab is free and in beta. Results are a guide; a real build can differ slightly. About the Lab

Common problems

  • The lamp changes by itself: you touched the pad while it was measuring the baseline, or the touch wire moves. Press the EN (reset) button with your hands off the pad, and tape the wire down.
  • Nothing happens when you tap: check the wire goes to GPIO 4. Watch the Serial Monitor: if the value hardly drops when you touch, use a bigger piece of foil or a shorter wire.
  • It reacts before you touch it: the foil is too big or too close to other wires. Make it smaller, or change 0.7 to 0.6.
  • The colour looks pink or blue: change the green and blue amounts in setLamp() until the white looks right to you.
  • It doesn't work on an ESP32-S3: on the S3, touch values go up when touched, not down. Change < to > and 0.7 to 1.3.

FAQ

Can I turn my normal bedside lamp into a touch lamp?

Yes, without opening it: plug it into a certified touch dimmer adapter or a smart plug made for mains lamps. Never cut, open or rewire anything that plugs into the wall; mains electricity can kill.

How does a touch lamp know I touched it?

Your body can hold a little electric charge. When you touch the metal, the touch pin can store more charge, and the ESP32 measures that change. That's called capacitive sensing.

Why does the lamp have three brightness levels?

That copies most shop-bought touch lamps: each tap steps up a level and the next tap after the brightest switches it off. Add more numbers to LEVELS for more steps.

Does it work through glass or plastic?

Through thin paper, fabric or plastic, yes. Thick glass is usually too much. Put the foil on the inside of a thin shade or under the base's covering.

Can I do this with an Arduino UNO?

The UNO has no touch pins, so use the metal touch module from the 45-in-1 Sensor Kit instead. Our Make a touch-sensitive lamp lesson shows how.

Related lessons