Double-clap light switch
Clap twice and the lights come on. Clap twice again to switch them off.
Shop by category
Shop all componentsMake an RGB LED pulse with the music: brightness follows the volume and the colour shifts as the beat changes. Two competing needs meet here. The lights must jump instantly on a beat, but they must not flicker between beats, so the sketch rises fast and falls slowly. When the new level is higher than the current one it is adopted immediately; when it is lower, the current level decays gradually towards it. That asymmetric smoothing is exactly how a studio compressor behaves, and it is what makes reactive lighting look musical rather than twitchy.
The ESP32 is a 3.3V board: never connect a 5V signal straight to a GPIO pin.
Every part for this build comes in the Geekobot Titan Kit box, a Geekobot exclusive, together with the parts for all 120 projects, from a first blinking LED to a smart fan and a reaction game. The wiring below is the kit's own wiring.
Tick anything you already have and the total updates.
ESP32 DevKit V1 (38-pin): the brain of every Titan Kit project, with Wi-Fi and Bluetooth
Male-to-female and female-to-female leads for the modules
220 ohm resistors: one in series with every LED
Sound sensor module: AO gives the loudness, DO switches at the knob setting
ESP32 expansion board from the kit: every GPIO has its own S, V, G header
Follow the colours: red is power, dark grey is ground, blue is a signal.
titan_kit_054_sound_reactive_rgb_lightin.inoVerify & upload in Electrolarkint micPin = 34;
int redPin = 32;
int greenPin = 5;
int bluePin = 23;
float smoothLevel = 0;
float hue = 0;
int quietLevel = 20;
int loudLevel = 350;
int amplitude()
{
unsigned long start = millis();
int highest = 0;
int lowest = 1023;
while (millis() - start < 20)
{
int sample = analogRead(micPin);
if (sample > highest) highest = sample;
if (sample < lowest) lowest = sample;
}
return highest - lowest;
}
void hueToRGB(float h, float brightness, int &r, int &g, int &b)
{
float section = h / 60.0;
if (section < 1) { r = 255; g = section * 255; b = 0; }
else if (section < 2) { r = (2 - section) * 255; g = 255; b = 0; }
else if (section < 3) { r = 0; g = 255; b = (section - 2) * 255; }
else if (section < 4) { r = 0; g = (4 - section) * 255; b = 255; }
else if (section < 5) { r = (section - 4) * 255; g = 0; b = 255; }
else { r = 255; g = 0; b = (6 - section) * 255; }
r = r * brightness;
g = g * brightness;
b = b * brightness;
}
void setup()
{
analogReadResolution(10); // ESP32 is 12-bit; use 0-1023 like the examples
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
int level = constrain(map(amplitude(), quietLevel, loudLevel, 0, 100), 0, 100);
// Rise instantly, fall slowly
if (level > smoothLevel)
{
smoothLevel = level;
}
else
{
smoothLevel = smoothLevel * 0.90 + level * 0.10;
}
// Loud sounds push the colour further round the wheel
hue = hue + 0.2 + (smoothLevel / 25.0);
if (hue >= 360) hue = hue - 360;
float brightness = smoothLevel / 100.0;
int r, g, b;
hueToRGB(hue, brightness, r, g, b);
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
Serial.print("Level ");
Serial.print(smoothLevel);
Serial.print(" Hue ");
Serial.println(hue);
}
Use a micro-USB data cable (some cables only charge), install the CP210x or CH340 driver if no port appears, pick Tools > Board > ESP32 Dev Module and hold the BOOT button on the board until the upload starts.
Check that it is the common-cathode type with the longest leg to GND, and that red, green and blue go to the GPIOs in the table.
Use the sensor's AO pin on GPIO34 and turn the blue trim pot: the module's LED should flicker when you clap.
Two competing needs meet here. The lights must jump instantly on a beat, but they must not flicker between beats, so the sketch rises fast and falls slowly. When the new level is higher than the current one it is adopted immediately; when it is lower, the current level decays gradually towards it. That asymmetric smoothing is exactly how a studio compressor behaves, and it is what makes reactive lighting look musical rather than twitchy.
Silence leaves the LED almost dark. Clap and it flashes brightly at once. Play music and it pulses in time, brightening on each beat.
Yes. It is project 54 of 120 in the Geekobot Titan Kit, in level 10 (Sound analysis). Every part on the list comes in the kit box, and you can also buy the parts one by one from the list above.
No. Every Titan Kit project is wired on the ESP32 expansion board and the breadboard with jumper wires.
8 parts: ESP32-WROOM-32 38-Pin Development Board, USB Cable, Solderless Breadboard, Dupont Jumper Wires (40 pcs), 5mm RGB LED (Pack of 10), Carbon Film Resistor 1/4W 5% (Pack of 10), Generic LM393 Sound Detection Sensor Module – Digital Output and ESP32 38-Pin Expansion Board. On this page you can tick the parts you already have and add only the rest to your cart.
It is a intermediate project and takes about 30 minutes. The build on this page has 5 steps.
No. This page has the complete, tested code (titan_kit_054_sound_reactive_rgb_lightin.ino).
About ₹774 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Fast analog reads and PWM scaling; Asymmetric smoothing, fast attack, slow decay; Converting a hue angle to RGB channels; Passing values back through reference parameters.
The ESP32 is a 3.3V board: never connect a 5V signal straight to a GPIO pin.
Clap twice and the lights come on. Clap twice again to switch them off.
Plays a friendly ding-dong whenever someone walks into the shop or room.
Beeps and flashes any message in Morse, or key your own by hand.
Turn a membrane keypad into a two-octave piano.
Lessons are free to read. Maker (₹499/month) and Pro add quizzes, worked problems and certificates, paid with your Geekobot account. See plans
Learning project. Check every circuit and the datasheet of each part before you power it up.