Double-clap light switch
Clap twice and the lights come on. Clap twice again to switch them off.
Shop by category
Shop all componentsCount sharp sounds like claps or drum beats, ignoring the echoes that follow each one. A clap is a transient, a sudden rise followed by a rapid fall, and its echoes look like several smaller claps arriving a few tens of milliseconds later. Detecting the rise is easy; the real work is debouncing. After each accepted event the sketch refuses to count anything for 250 ms, which is longer than the echo but shorter than a human can deliberately clap twice. It also measures the interval between beats and converts it to beats per minute.
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
LEDs from the kit's red, green, yellow and blue packs
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_059_sound_triggered_counter.inoVerify & upload in Electrolark#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
int micPin = 34;
int led = 25;
int resetButton = 12;
int triggerLevel = 55;
unsigned long deadTime = 250; // Ignore echoes for this long
int count = 0;
unsigned long lastTrigger = 0;
unsigned long lastInterval = 0;
int bpm = 0;
int level()
{
unsigned long start = millis();
int highest = 0;
int lowest = 1023;
while (millis() - start < 12)
{
int sample = analogRead(micPin);
if (sample > highest) highest = sample;
if (sample < lowest) lowest = sample;
}
return constrain(map(highest - lowest, 20, 400, 0, 100), 0, 100);
}
void setup()
{
analogReadResolution(10); // ESP32 is 12-bit; use 0-1023 like the examples
pinMode(led, OUTPUT);
pinMode(resetButton, INPUT_PULLUP);
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
Serial.println("OLED not found");
while (true) { }
}
display.setTextColor(SSD1306_WHITE);
}
void loop()
{
int now = level();
if (digitalRead(resetButton) == LOW)
{
count = 0;
bpm = 0;
lastInterval = 0;
Serial.println("Counter reset");
delay(300);
}
if (now > triggerLevel && millis() - lastTrigger > deadTime)
{
if (lastTrigger > 0)
{
lastInterval = millis() - lastTrigger;
if (lastInterval > 200 && lastInterval < 3000)
{
bpm = 60000 / lastInterval;
}
}
lastTrigger = millis();
count = count + 1;
digitalWrite(led, HIGH);
Serial.print("Event ");
Serial.print(count);
Serial.print(" gap ");
Serial.print(lastInterval);
Serial.println(" ms");
}
if (millis() - lastTrigger > 100)
{
digitalWrite(led, LOW);
}
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.print("SOUND COUNTER");
display.setTextSize(3);
display.setCursor(0, 14);
display.print(count);
display.setTextSize(1);
display.setCursor(64, 20);
display.print("events");
display.setCursor(0, 42);
if (lastInterval > 0)
{
display.print("Gap ");
display.print(lastInterval);
display.print("ms");
}
else
{
display.print("Clap to start");
}
display.setCursor(0, 52);
if (bpm > 0)
{
display.print("Tempo ");
display.print(bpm);
display.print(" BPM");
}
else
{
display.print("Level ");
display.print(now);
display.print("%");
}
display.display();
}
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.
Turn it round: the long leg (anode) goes to the GPIO side through the 220 ohm resistor, the short leg to GND.
The sketch uses INPUT_PULLUP, so one leg goes to the GPIO and the other to GND. Use two legs on opposite sides of the button.
Use the sensor's AO pin on GPIO34 and turn the blue trim pot: the module's LED should flicker when you clap.
A clap is a transient, a sudden rise followed by a rapid fall, and its echoes look like several smaller claps arriving a few tens of milliseconds later. Detecting the rise is easy; the real work is debouncing. After each accepted event the sketch refuses to count anything for 250 ms, which is longer than the echo but shorter than a human can deliberately clap twice. It also measures the interval between beats and converts it to beats per minute.
Clap once and the counter reads 1 with a brief LED flash. Echoes in a hard-walled room do not add extra counts. Clap steadily and the tempo in BPM appears.
Yes. It is project 59 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.
10 parts: ESP32-WROOM-32 38-Pin Development Board, USB Cable, Solderless Breadboard, Dupont Jumper Wires (40 pcs), Through-Hole LED Pack (Pack of 10), Carbon Film Resistor 1/4W 5% (Pack of 10), Tactile Push Button Switch, Generic LM393 Sound Detection Sensor Module – Digital Output, 0.96" OLED Display 128x64 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 45 minutes. The build on this page has 7 steps.
No. This page has the complete, tested code (titan_kit_059_sound_triggered_counter.ino).
About ₹905 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Transient peak counting and debouncing; Dead time as an echo filter; Converting an interval to beats per minute; Rejecting intervals that are implausible.
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.