Lights
Touch hexagon lamp
Three hexagons of light on the wall. Tap a hexagon to step it through its colours; touch all three to switch everything off.
- Hard
- about 80 min
- ESP32-S3
- 7 parts
- 12 wires
- 68 lines of code
ESP32 packs are built on real parts: the simulator runs Arduino Uno circuits only.
Idea
One strip of LEDs is treated as three groups of six. The sketch never lights “a hexagon”: it lights LEDs 0 to 5, 6 to 11 and 12 to 17, and the case does the rest.
Before you start
Eighteen LEDs at full white can draw more than a computer's USB port gives. The sketch limits brightness; if you raise it, power the lamp from a 5 V 2 A charger.
What you learn
Splitting one LED strip into zones, and handling several inputs at once.
How it works
input
Touch pad 1
Touch pad 2
Touch pad 3
decides
ESP32-S3
output
LED strip
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 12
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
hex-lamp.ino
// Touch hexagon lamp
// Three touch pads, three hexagons of light. Tap a pad to step that hexagon
// through its colours; hold all three to switch everything off.
#include <Adafruit_NeoPixel.h>
const int PIN_PAD1_IO = 4;
const int PIN_PAD2_IO = 5;
const int PIN_PAD3_IO = 6;
const int PIN_STRIP_DIN = 16;
const int LEDS_PER_HEX = 6;
const int HEX_COUNT = 3;
Adafruit_NeoPixel strip(LEDS_PER_HEX * HEX_COUNT, PIN_STRIP_DIN, NEO_GRB + NEO_KHZ800);
const int PADS[HEX_COUNT] = {PIN_PAD1_IO, PIN_PAD2_IO, PIN_PAD3_IO};
// 0 is off; the rest are hues round the colour wheel.
const uint16_t HUES[] = {0, 5461, 16384, 27306, 38229, 49151, 60074};
const int STEP_COUNT = 8; // off, six colours, white
int step[HEX_COUNT] = {1, 3, 5};
bool wasTouched[HEX_COUNT] = {false, false, false};
uint32_t colourOf(int s) {
if (s == 0) return 0;
if (s == STEP_COUNT - 1) return strip.Color(255, 220, 180);
return strip.gamma32(strip.ColorHSV(HUES[s]));
}
void show() {
for (int h = 0; h < HEX_COUNT; h++) {
for (int i = 0; i < LEDS_PER_HEX; i++) strip.setPixelColor(h * LEDS_PER_HEX + i, colourOf(step[h]));
}
strip.show();
}
void setup() {
for (int h = 0; h < HEX_COUNT; h++) pinMode(PADS[h], INPUT);
strip.begin();
strip.setBrightness(90);
show();
}
void loop() {
bool touched[HEX_COUNT];
int count = 0;
for (int h = 0; h < HEX_COUNT; h++) {
touched[h] = digitalRead(PADS[h]) == HIGH;
if (touched[h]) count++;
}
if (count == HEX_COUNT) {
for (int h = 0; h < HEX_COUNT; h++) step[h] = 0;
show();
while (digitalRead(PADS[0]) == HIGH || digitalRead(PADS[1]) == HIGH || digitalRead(PADS[2]) == HIGH) delay(20);
for (int h = 0; h < HEX_COUNT; h++) wasTouched[h] = false;
return;
}
for (int h = 0; h < HEX_COUNT; h++) {
// act on release, so a three-pad hold is not also three taps
if (!touched[h] && wasTouched[h]) {
step[h] = (step[h] + 1) % STEP_COUNT;
show();
}
wasTouched[h] = touched[h];
}
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 > ESP32S3 Dev Module, then Tools > Port and choose the board's port.
- Press Upload (the arrow). Wait for “Done uploading”.