Double-clap light switch with a sound sensor on Arduino: design worksheet and code

A ₹690 design worksheet for a double-clap switch using a sound sensor module and a relay, with the timing windows worked out and notes on false triggers from echoes and speech.

About the author · Sources, testing and AI policy

  • Parts₹690
  • Bench time2–3 h
  • LevelBeginner
  • ReviewedNov 2026

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

PartQtyUnitTotal
Arduino Nano (or compatible)1₹400₹400
Sound sensor module (KY-038 or similar)1₹60₹60
5 V relay module1₹70₹70
LED lamp or low-voltage lamp for testing1₹50₹50
Breadboard1₹60₹60
Jumper wires1₹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 clapWhat the sketch does
0 to 150 msIgnored: echo or the tail of the same clap
150 to 700 msSecond clap: toggle the relay
Over 700 msForget the first clap and treat the next as a new first clap
Design values; tune to your room and your clapping rhythm.

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

PartConnects toNote
Sound module VCC and GND5V and GND
Sound module DOD2Digital output
Relay IND7Many modules are active-low
Relay supply5V and GNDSwitch only low voltage here
Turn the module’s trimmer until its indicator LED lights only on a clap held a metre away.

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

  1. Trimmer: adjust until the module LED lights on a clap but not on speech.
  2. Timing: clap twice at different speeds and note which toggle.
  3. Echo test: clap in a tiled room and check there is no double toggling.
  4. False triggers: play music or talk loudly and count wrong toggles over ten minutes.
  5. Distance: test at 1, 3 and 5 metres.
  6. Lockout: clap four times fast and see that it toggles once or twice only.

What usually goes wrong

SymptomLikely causeFix
Toggles on any loud noiseTrimmer too sensitiveTurn it down
Never triggersTrimmer too insensitive or wrong pinTurn it up and check D2
Toggles twice per double clapLockout too shortRaise LOCKOUT_MS
Relay chattersSound level hovering around the thresholdUse the edge logic and a lockout
Lamp invertedRelay is active-low or highSwap 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.

More builds

Next builds to try

Free: the final-year project pack

A shortlist with BOMs, a report outline that matches the usual chapter scheme, a costing sheet and 40 viva questions. Built for the February–April submission season.

The Build Note

One short email when something is worth your bench time.

A new build sheet, a part price that moved, or a mistake worth avoiding. No daily drip, no recycled news.

One short email when a build or a price is worth your time. Unsubscribe in one click.