Clap Switch Circuit Using a Sound Sensor Module and Arduino
Build a double-clap switch with a sound sensor module and Arduino Uno. Wiring table, full commented code, calibration steps and fixes.
On this page
Clap twice and a light turns on. Clap twice again and it turns off. A clap switch is a classic beginner project, and with a cheap sound sensor module and an Arduino you can build one in about 20 minutes. Unlike many online versions that react to any noise, this one waits for two claps in a short time window, so a door slam or a single cough will not toggle your light.
Safety note
This project switches a 5 V LED only. If you later control a lamp, use only low-voltage DC (12 V or less) through a proper relay module, and never open or modify mains wiring yourself.
What you need
Scroll sideways to see all columns.
| Item | Qty | Notes |
|---|---|---|
| Arduino Uno (or clone) + USB cable | 1 | CH340 clones need the CH340 driver installed on your PC |
| Sound sensor module (electret microphone + LM393) | 1 | Often sold as KY-038 style; 3 or 4 pins: VCC, GND, DO (and AO) |
| LED 5 mm, any colour | 1 | Long leg = anode (+) |
| Resistor 220 Ω | 1 | LED resistor (see our Ohm's law guide) |
| Jumper wires | 4 | Male-to-female if module has header pins |
| Small screwdriver | 1 | To adjust the blue potentiometer on the module |
| Digital multimeter | 1 | For the checks below |
How the sound sensor module works
The little silver cylinder is an electret microphone. It turns sound into a very small changing voltage. The LM393 chip on the board is a comparator. It compares the microphone signal with a threshold you set using the blue potentiometer and then switches the DO (digital output) pin.
- Quiet room: the module output is in its idle state.
- A loud sound (a clap): the output switches for a short moment.
- Turn the potentiometer clockwise or anticlockwise to change how loud the sound must be. A small LED on the board tells you when it triggers.
The idle level of DO is not the same on every board. On many boards it is LOW at idle and goes HIGH on sound, while others do the opposite. That is why the code below has one setting you can flip.
Wiring table
Scroll sideways to see all columns.
| Arduino Uno pin | Connects to |
|---|---|
| 5V | Sound module VCC |
| GND | Sound module GND |
| D2 | Sound module DO |
| D8 | 220 Ω resistor → LED anode |
| GND | LED cathode (short leg) |
Signal path in one line:
Clap → microphone → LM393 comparator → DO → Arduino D2 → code decides → D8 → 220 Ω → LED
How the code decides
A clap is a short spike, and the room echoes it, which could look like several claps. The code uses three timing rules:
- Lock-out (150 ms): count only transitions into the active state, and ignore any within 150 ms of an accepted clap.
- Double-clap window (up to 800 ms): the second clap must arrive no later than 800 ms after the first.
- Timeout: if no second clap arrives in the window, forget the first one.
When two claps fit the window, the LED toggles (ON becomes OFF, OFF becomes ON). The code uses millis() rather than delay(), so the Arduino is never "blind" while waiting.
The code
// ElectroDai - Double-clap switch with a sound sensor module
// Sound module DO -> D2, LED (with 220 ohm resistor) -> D8
const int SOUND_PIN = 2; // digital output of the sound module
const int LED_PIN = 8; // LED
// Level on DO when a clap is heard. Many modules give HIGH on sound.
// If your LED toggles at random or never, try changing HIGH to LOW.
const int ACTIVE_LEVEL = HIGH;
const unsigned long LOCKOUT_MS = 150; // ignore echoes after a clap
const unsigned long MAX_GAP_MS = 800; // second clap must come within this time
bool wasActive = false;
bool ledOn = false; // current LED state
bool waitingSecondClap = false; // true after the first clap
unsigned long firstClapTime = 0; // when the first clap happened
unsigned long lastClapTime = 0; // when the last clap was accepted
void setup() {
pinMode(SOUND_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
wasActive = (digitalRead(SOUND_PIN) == ACTIVE_LEVEL);
Serial.begin(9600); // for debugging in Serial Monitor
}
void loop() {
unsigned long now = millis();
if (waitingSecondClap && now - firstClapTime > MAX_GAP_MS) {
waitingSecondClap = false;
Serial.println("Timeout");
}
bool active = (digitalRead(SOUND_PIN) == ACTIVE_LEVEL);
bool newClap = active && !wasActive;
wasActive = active;
if (newClap && now - lastClapTime >= LOCKOUT_MS) {
lastClapTime = now;
if (!waitingSecondClap) {
// First clap: start the timer
waitingSecondClap = true;
firstClapTime = now;
Serial.println("Clap 1");
} else {
// Second clap inside the window: toggle the LED
ledOn = !ledOn;
digitalWrite(LED_PIN, ledOn ? HIGH : LOW);
waitingSecondClap = false;
Serial.println(ledOn ? "Clap 2 -> LED ON" : "Clap 2 -> LED OFF");
}
}
}
Because the lock-out is 150 ms, two claps closer than that will count as one. A normal double clap is about 200-500 ms apart.
Calibration (do this once)
- Upload the sketch and open Serial Monitor at 9600 baud.
- Stay quiet. If "Clap 1" prints by itself, the module is too sensitive: turn the blue potentiometer so the module's LED goes off in a quiet room.
- Clap once from about 30 cm away. The module LED should blink and "Clap 1" should print.
- If nothing happens, turn the potentiometer the other way in small steps (quarter-turns) and clap again.
- Clap twice. The LED should toggle. Adjust the gap between claps until it works reliably.
Testing with a multimeter
- Supply: 5 V between module VCC and GND (Arduino powered).
- DO idle: measure DO to GND in a quiet room. You should read close to 0 V or close to 5 V. A value like 2.5 V suggests a poor connection.
- DO on sound: a clap is too short to see on a meter. Instead, turn the potentiometer to make the module output stay triggered, and confirm DO flips to the opposite level. This also shows you which level (HIGH or LOW) your module uses as active.
- LED branch: with the LED lit, the voltage across the 220 Ω resistor should be about 3 V (5 V minus the LED drop of roughly 2 V), which is about 14 mA.
Troubleshooting
Scroll sideways to see all columns.
| Symptom | Likely cause | Fix |
|---|---|---|
| LED toggles by itself | Too sensitive, or ACTIVE_LEVEL wrong | Turn the pot down; try changing HIGH to LOW |
| LED never changes | Too insensitive or DO wire on the wrong pin | Turn the pot up; check DO goes to D2 |
| Works with a loud clap only | Sensitivity low | Increase sensitivity slowly |
| Single clap toggles the LED | Echoes, or lock-out too short | Increase LOCKOUT_MS to 250 |
| Double clap not recognised | Claps too far apart | Increase MAX_GAP_MS to 1000 |
| Nothing prints in Serial Monitor | Wrong port or driver | Select the right port; install CH340 driver for clones |
| Module LED always on | Pot turned fully one way | Turn the pot back until the LED goes off |
FAQ
Can I use the AO pin instead?
Yes, you can read AO with analogRead and set the threshold in code, but the module's AO output is noisy and tends to give wrong readings from a single microphone. DO with the on-board comparator is simpler and more reliable for claps.
Can I switch a 12 V light?
Yes, with an active-HIGH 5 V relay module: connect IN to D8, VCC to 5 V and GND to Arduino GND, then switch the load through COM and NO, and a supply of 12 V DC or less. See our transistor switch tutorial for how relay coils are driven. Keep wiring neat and insulated.
Why does my module not respond to a clap across the room?
The electret microphone is not very sensitive, and the clap's sound drops quickly with distance. Aim the microphone at the clapper and keep it within 2-3 m.
Why not just one clap?
A single loud sound is common in a home. Requiring two claps cuts false triggers significantly.