A ₹690 switch you can toggle with two claps. A sound sensor module reports loud sounds, and the sketch listens for two claps with the right gap between them. The page explains the timing windows, so that a single bang or an echo does not switch the light and so that you can tune it for your room.
Scope: beginner. This is a design worksheet with illustrative costs. It has not been bench-tested by this site. The windows below are design values to tune, not measured ones. Sound sensors cannot tell a clap from a door slam or a dropped pan, so false triggers are normal and the page says how to reduce them. Switch only a low-voltage lamp or a rated relay module through a proper enclosure; mains wiring needs a qualified electrician.
How it works
The sound module has a small microphone and a comparator with a trimmer. Its digital output goes high when sound exceeds the trimmed level. A clap produces a short burst, so the sketch looks for a rising edge, then waits for a quiet gap, then a second rising edge.
Timing rules decide what counts: ignore anything within 150 ms of the first clap, because that is usually echo or the tail of the same clap; accept a second clap between 150 ms and 700 ms later; if none arrives, forget the first clap. Sound crosses a 3 m room in about 9 ms (343 m/s), so room echoes arrive far inside the 150 ms window and are ignored.
Bill of materials
| Part | Qty | Unit | Total |
|---|---|---|---|
| Arduino Nano (or compatible) | 1 | ₹400 | ₹400 |
| Sound sensor module (KY-038 or similar) | 1 | ₹60 | ₹60 |
| 5 V relay module | 1 | ₹70 | ₹70 |
| LED lamp or low-voltage lamp for testing | 1 | ₹50 | ₹50 |
| Breadboard | 1 | ₹60 | ₹60 |
| Jumper wires | 1 | ₹50 | ₹50 |
| Parts total | ₹690 | ||
Scroll the table sideways to see every column.
Prices checked Nov 2026. Shipping and tools are not included.
Prices are illustrative figures for Indian sellers in November 2026, before shipping, and have not been checked against current listings.
The numbers
| Gap after the first clap | What the sketch does |
|---|---|
| 0 to 150 ms | Ignored: echo or the tail of the same clap |
| 150 to 700 ms | Second clap: toggle the relay |
| Over 700 ms | Forget the first clap and treat the next as a new first clap |
Sound at 343 m/s covers 3 m in about 8.7 ms, so even a big room’s echoes sit well inside the ignore window. A lockout of 500 ms after each toggle also stops a further clap from toggling straight back.
Power and wiring
| Part | Connects to | Note |
|---|---|---|
| Sound module VCC and GND | 5V and GND | |
| Sound module DO | D2 | Digital output |
| Relay IN | D7 | Many modules are active-low |
| Relay supply | 5V and GND | Switch only low voltage here |
The sketch
// Double-clap switch: Nano + sound sensor module + relay
// Design worksheet: tune the trimmer and the timing windows in your room.
const int SOUND = 2, RELAY = 7;
const bool ON_LEVEL = LOW; // many relay modules are active-low
const unsigned long IGNORE_MS = 150, WINDOW_MS = 700, LOCKOUT_MS = 500;
bool lampOn = false;
bool waiting = false; // first clap seen
unsigned long firstAt = 0, lockUntil = 0;
bool lastSound = false;
void setLamp(bool on) {
lampOn = on;
digitalWrite(RELAY, on ? ON_LEVEL : !ON_LEVEL);
}
void setup() {
pinMode(SOUND, INPUT);
pinMode(RELAY, OUTPUT);
setLamp(false);
}
void loop() {
unsigned long now = millis();
bool s = digitalRead(SOUND) == HIGH;
bool edge = s && !lastSound;
lastSound = s;
if (waiting && now - firstAt > WINDOW_MS) waiting = false; // too slow: reset
if (edge && now > lockUntil) {
if (!waiting) {
waiting = true; firstAt = now; // first clap
} else if (now - firstAt >= IGNORE_MS) {
setLamp(!lampOn); // second clap
waiting = false;
lockUntil = now + LOCKOUT_MS;
} // else: echo, ignore
}
}The sensor output may stay high for a short time on its own, so the sketch reacts to the rising edge rather than the level. If the module’s output is active-low in your version, invert the comparison. Speech and music can trigger it; a longer IGNORE_MS or a stricter trimmer setting reduces that. The sketch has been proofread but not compiled on this site.
Test plan
- Trimmer: adjust until the module LED lights on a clap but not on speech.
- Timing: clap twice at different speeds and note which toggle.
- Echo test: clap in a tiled room and check there is no double toggling.
- False triggers: play music or talk loudly and count wrong toggles over ten minutes.
- Distance: test at 1, 3 and 5 metres.
- Lockout: clap four times fast and see that it toggles once or twice only.
What usually goes wrong
| Symptom | Likely cause | Fix |
|---|---|---|
| Toggles on any loud noise | Trimmer too sensitive | Turn it down |
| Never triggers | Trimmer too insensitive or wrong pin | Turn it up and check D2 |
| Toggles twice per double clap | Lockout too short | Raise LOCKOUT_MS |
| Relay chatters | Sound level hovering around the threshold | Use the edge logic and a lockout |
| Lamp inverted | Relay is active-low or high | Swap ON_LEVEL |
Upgrades
- Three-clap mode: add a second function for three claps.
- Analog level: read the analog output to separate claps from speech by peak.
- Timer: auto-off after a set time.
- Wi-Fi: move to an ESP32 and add a phone switch too.
Cost and parts notes
The ₹690 total is an illustration and does not include shipping. Use the cost calculator to compare sellers.
Common questions
Why two claps rather than one?
Single loud sounds are common, but two with the right gap are rarer.
Will it work in a noisy room?
Not reliably; background noise raises the trigger level and false triggers rise.
Is it safe for mains lights?
Not as a hobby build. Use a rated relay in a proper enclosure and have mains wiring done by a qualified electrician.
Why a lockout?
It prevents extra claps from toggling the lamp straight back.
Last reviewed: November 2026. This is a design worksheet with illustrative costs. It has not been bench-tested by this site; library names, pins and limits must be checked against your board and the current library versions, and measurements from your build are welcome.