Double-clap light switch
Clap twice and the lights come on. Clap twice again to switch them off.
Shop by category
Shop all componentsEstimate the pitch of a sound by counting how often the waveform crosses its midpoint, and show which band it falls in. A proper spectrum needs an FFT, which is more than we will attempt here. Zero-crossing counting is the honest approximation: count how many times the signal passes its midpoint in a fixed window, and half that count per second is roughly the dominant frequency. It works well for a whistle or a single tone and poorly for a chord, which is a genuinely useful thing for students to discover for themselves.
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_055_sound_frequency_visualizer.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 bandLeds[5] = {12, 14, 27, 26, 25};
int midpoint = 512;
int bandLimits[5] = {200, 500, 1000, 2000, 5000};
const char* bandNames[5] = {"BASS", "LOW MID", "MID", "HIGH MID", "TREBLE"};
int measureFrequency()
{
int crossings = 0;
bool wasAbove = false;
int highest = 0;
int lowest = 1023;
unsigned long start = micros();
unsigned long window = 100000; // 100 ms
while (micros() - start < window)
{
int sample = analogRead(micPin);
if (sample > highest) highest = sample;
if (sample < lowest) lowest = sample;
bool isAbove = (sample > midpoint);
if (isAbove && !wasAbove)
{
crossings = crossings + 1;
}
wasAbove = isAbove;
}
if (highest - lowest < 40)
{
return 0; // Too quiet to judge
}
return crossings * 10; // Crossings in 100 ms -> per second
}
void setup()
{
analogReadResolution(10); // ESP32 is 12-bit; use 0-1023 like the examples
for (int i = 0; i < 5; i++)
{
pinMode(bandLeds[i], OUTPUT);
}
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
Serial.println("OLED not found");
while (true) { }
}
display.setTextColor(SSD1306_WHITE);
// Learn the resting midpoint of this module
long total = 0;
for (int i = 0; i < 200; i++)
{
total = total + analogRead(micPin);
delay(2);
}
midpoint = total / 200;
Serial.print("Midpoint: ");
Serial.println(midpoint);
}
void loop()
{
int frequency = measureFrequency();
int band = -1;
for (int i = 0; i < 5; i++)
{
if (frequency > 0 && frequency <= bandLimits[i])
{
band = i;
break;
}
}
if (frequency > bandLimits[4]) band = 4;
for (int i = 0; i < 5; i++)
{
digitalWrite(bandLeds[i], i == band);
}
Serial.print("Frequency approx ");
Serial.println(frequency);
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.print("FREQUENCY BANDS");
display.setTextSize(2);
display.setCursor(0, 14);
if (frequency > 0)
{
display.print(frequency);
display.setTextSize(1);
display.print(" Hz");
}
else
{
display.print("--");
}
display.setTextSize(1);
display.setCursor(0, 36);
display.print("Band: ");
display.print(band >= 0 ? bandNames[band] : "silent");
for (int i = 0; i < 5; i++)
{
int x = i * 25 + 3;
display.drawRect(x, 46, 20, 14, SSD1306_WHITE);
if (i == band)
{
display.fillRect(x + 2, 48, 16, 10, SSD1306_WHITE);
}
}
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.
Use the sensor's AO pin on GPIO34 and turn the blue trim pot: the module's LED should flicker when you clap.
Check SDA on GPIO21 and SCL on GPIO22, power from 3V3, and that the sketch uses address 0x3C (some boards are 0x3D).
A proper spectrum needs an FFT, which is more than we will attempt here. Zero-crossing counting is the honest approximation: count how many times the signal passes its midpoint in a fixed window, and half that count per second is roughly the dominant frequency. It works well for a whistle or a single tone and poorly for a chord, which is a genuinely useful thing for students to discover for themselves.
Silence shows -- and no LED. Hum a low note: around 150 Hz with the BASS LED lit. Whistle: 1500 Hz or more with a higher band lit.
Yes. It is project 55 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.
9 parts: ESP32-WROOM-32 38-Pin Development Board, USB Cable, Solderless Breadboard, Dupont Jumper Wires (40 pcs), 5 × Through-Hole LED Pack (Pack of 10), Carbon Film Resistor 1/4W 5% (Pack of 10), 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 6 steps.
No. This page has the complete, tested code (titan_kit_055_sound_frequency_visualizer.ino).
About ₹958 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Basic spectrum analysis by zero crossing; Finding a signal's resting midpoint; Why an FFT would be needed for real spectra; The limits of a simple approximation.
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.