A Clap Switch: the KY-038 Sound Sensor and a Double Clap
Two claps and the light comes on, two more and it goes off. We look at the KY-038 module with its LM393 comparator: the analog and digital outputs, setting the threshold, how a single clap chatters, and telling a double clap from a slammed door.
About the project
A retro-gadget classic: a lamp that switches on when you clap. The KY-038 module is an electret microphone, an amplifier and an LM393 comparator. When a sound is louder than the threshold set by a trimmer, a signal appears on the DO output. We teach the ESP32 to recognise a double clap, so the lamp does not click on at a cough or a slammed door.
What you need
- ESP32 DevKit
- KY-038 sound sensor module (or the KY-037 with a bigger microphone)
- An LED and a 220 Ω resistor (or a relay module for a real lamp)
Wiring
| KY-038 | ESP32 |
|---|---|
| AO (analog) | optional (GPIO34, if you want to see the level) |
| G | GND |
| + | 3V3 |
| DO (digital) | GPIO27 |
"Lamp": GPIO26 → 220 Ω → LED → GND. For a 230 V lamp, use a relay module as in the motion-sensor article, with all its safety warnings.
The module's two outputs
- AO is the amplified audio signal around mid-supply (~1.65 V).
analogRead()sees random points of the wave, so loudness is measured as the swing: the difference between the maximum and the minimum over 20–50 ms. - DO is the comparator output: HIGH when the sound is louder than the threshold (on most modules a second LED lights up too). The blue trimmer sets the threshold. Adjust it so that in silence the DO LED is just barely off.
One clap, several pulses
Sound is an oscillation, so during the 5–10 ms of a clap the comparator manages to switch several times. Without protection, one clap counts as three. So after the first pulse we ignore DO for 150 ms: the same debouncing as for a button.
Double-clap logic
- Remember the first clap and its time.
- If the second one arrives within 700 ms, toggle the lamp.
- If not, forget the first. A single sound does nothing.
Code
// Clap switch: KY-038 (DO output) + a "lamp". A double clap toggles the light
const int MIC_DO = 27;
const int LAMP = 26;
const unsigned long DEBOUNCE_MS = 150; // one clap chatters for a few milliseconds
const unsigned long WINDOW_MS = 700; // the second clap must come within 0.7 s
bool lampOn = false;
int claps = 0;
unsigned long lastClap = 0, firstClap = 0;
void setup() {
Serial.begin(115200);
pinMode(MIC_DO, INPUT);
pinMode(LAMP, OUTPUT);
Serial.println("Clap twice to switch the light on or off");
}
void loop() {
unsigned long now = millis();
if (digitalRead(MIC_DO) == HIGH && now - lastClap > DEBOUNCE_MS) {
lastClap = now;
if (claps == 1 && now - firstClap <= WINDOW_MS) { // second clap in time
claps = 0;
lampOn = !lampOn;
digitalWrite(LAMP, lampOn);
Serial.println(lampOn ? "Light ON" : "Light OFF");
} else { // first clap
claps = 1;
firstClap = now;
Serial.println("One clap…");
}
}
if (claps == 1 && now - firstClap > WINDOW_MS) claps = 0; // ignore a single sound
}
Good to know
- Threshold. If DO is lit all the time (noise, music), the lamp reacts to everything. If it never lights, clap louder or closer: a KY-038 hears claps at 1–2 metres.
- More reliable via AO. Instead of DO, measure the AO swing over 30 ms and compare it with a threshold in code. Then the threshold can be changed from a phone rather than with a screwdriver.
- Rhythm. The same logic can tell 2 claps from 3 (two different actions), or check for a "secret rhythm".
- In the simulator, click the sensor to clap. The "Background noise" slider imitates a noisy room.