Automatic dusk-to-dawn light
Lights switch on by themselves at dusk and off again in the morning.
Shop by category
Shop all componentsThe ESP32 has ten capacitive touch inputs built into its pins, so a touch switch needs only a wire or a piece of foil. When a finger touches the pad the pin's reading drops. The sketch compares it with a threshold, toggles a relay, and waits for the finger to lift so one touch makes one change. Put the foil behind a thin sheet of plastic or paper for a hidden switch.
Anything connected to 230 V mains must be installed by a qualified electrician in a closed, earthed enclosure. Test with a low-voltage lamp first.
Keep the touch pad well away from mains wiring.
Tick anything you already have and the total updates.
Follow the colours: red is power, dark grey is ground, blue is a signal.
USB phone charger. Anything connected to 230 V mains must be installed by a qualified electrician in a closed, earthed enclosure. Test with a low-voltage lamp first.
touch_lamp.inoVerify & upload in Electrolarkconst int TOUCH_PIN = 4; // T0
const int RELAY_PIN = 26;
int THRESHOLD = 30; // set halfway between untouched and touched
bool on = false, wasTouched = false;
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // most relay modules are OFF when HIGH
}
void loop() {
int v = touchRead(TOUCH_PIN);
bool touched = v < THRESHOLD;
if (touched && !wasTouched) {
on = !on;
digitalWrite(RELAY_PIN, on ? LOW : HIGH);
}
wasTouched = touched;
Serial.println(v);
delay(50);
}
Raise the sensitivity gap: lower THRESHOLD, or shorten the pad wire which acts as an antenna.
Check the printed values; THRESHOLD must be between the untouched and touched numbers. Newer ESP32 chips give much larger numbers.
Touch values change with the ground. Calibrate in the final set-up.
GPIO 4, 0, 2, 15, 13, 12, 14, 27, 33 and 32 on the common ESP32 boards.
Yes. Each touch pin can be its own switch.
The touch pad is only low-voltage logic, but keep it away from the mains side of the relay.
4 parts: ESP32-WROOM-32 38-Pin Development Board, 5V Relay Module with Optocoupler, Solderless Breadboard and Dupont Jumper Wires (40 pcs). On this page you can tick the parts you already have and add only the rest to your cart.
It is a beginner project and takes about 45 minutes. The build on this page has 6 steps.
No. This page has the complete, tested code (touch_lamp.ino).
About ₹530 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Using touchRead on ESP32 touch pins; Finding a good touch threshold; Toggling a relay once per touch.
Ideas that reuse this build: ESP32 clap switch. They are listed under Take it further on this page.
Lights switch on by themselves at dusk and off again in the morning.
A precise thermostat that keeps tropical fish at a steady 25–26 °C.
Starts the pump when the tank runs low and stops it when it is full.
Tap a hidden pad to switch a lamp on or off, even through a wooden desk.
Learning project. Check every circuit and the datasheet of each part before you power it up.