Lights
Touch egg lamp
A night light shaped like an egg. A ring of LEDs glows under a frosted shade; tap the base to change colour.
- Easy
- about 40 min
- ESP32-C3
- 7 parts
- 6 wires
- 51 lines of code
ESP32 packs are built on real parts: the simulator runs Arduino Uno circuits only.
Idea
Two wires of signal and you have a lamp with no moving switch to wear out. A short tap and a long hold do different things, which is how most one-button gadgets work.
What you learn
Driving addressable LEDs, and telling a tap from a hold by timing it.
How it works
input
Touch sensor (base)
decides
ESP32-C3
output
LED ring
Powered with 5 V through the board's USB-C port.
A reference design. The sketch is compiled with the real ESP32 toolchain, and its pins are checked against the wiring table automatically. It has not yet been built and tested on real parts by us, so wire it on the bench and run the checks before you build it into a case. Real modules vary: follow the labels printed on yours, not their position in the picture.
Parts
0 of 7 ready
No prices here: they change by shop and by week. Buy each part by its name and the note beside it.
You also need
A computer with Arduino IDE 2 installed (free)
3D wiring
One jumper per step, with the USB cable unplugged. Several wires may share 3V3, 5V or GND: the expansion board or breadboard gives each its own pin.
Step 0 of 6
Lay out the parts
Seat the board on its expansion board or breadboard and lay the modules out round it. Nothing is connected yet.
All connections
egg-lamp.ino
// Touch egg lamp
// Tap the base to change colour. Hold it to turn the light off or on.
#include <Adafruit_NeoPixel.h>
const int PIN_TOUCH_IO = 3;
const int PIN_RING_DIN = 4;
const int LED_COUNT = 12;
const int BRIGHTNESS = 60; // of 255: soft enough for a night light
Adafruit_NeoPixel ring(LED_COUNT, PIN_RING_DIN, NEO_GRB + NEO_KHZ800);
const uint32_t COLOURS[] = {0xFFB060, 0xFF4020, 0xFF40A0, 0x6040FF, 0x20A0FF, 0x20FF80};
const int COLOUR_COUNT = sizeof(COLOURS) / sizeof(COLOURS[0]);
int colour = 0;
bool lampOn = true;
bool wasTouched = false;
bool holdHandled = false;
unsigned long touchStart = 0;
void show() {
ring.fill(lampOn ? COLOURS[colour] : 0);
ring.show();
}
void setup() {
pinMode(PIN_TOUCH_IO, INPUT);
ring.begin();
ring.setBrightness(BRIGHTNESS);
show();
}
void loop() {
bool touched = digitalRead(PIN_TOUCH_IO) == HIGH;
if (touched && !wasTouched) {
touchStart = millis();
holdHandled = false;
}
if (touched && !holdHandled && millis() - touchStart > 800) {
lampOn = !lampOn; // a long hold switches the lamp
holdHandled = true;
show();
}
if (!touched && wasTouched && !holdHandled && lampOn) {
colour = (colour + 1) % COLOUR_COUNT; // a short tap changes colour
show();
}
wasTouched = touched;
delay(20);
}
Libraries
- Adafruit NeoPixel: install it from Sketch > Include Library > Manage Libraries.
Uploading
First time with an ESP32: in Arduino IDE open Boards Manager, search “esp32” and install “esp32 by Espressif Systems”.
- Open the sketch in Arduino IDE.
- Plug the board in with the USB-C cable.
- Tools > Board > ESP32C3 Dev Module (USB CDC On Boot: Enabled), then Tools > Port and choose the board's port.
- Press Upload (the arrow). Wait for “Done uploading”.