Build a clap switch

Clap twice to switch a light on and off, using the small sound sensor from the Sensor Kit.
  • Level:Beginner
  • Duration: 35 min
  • Ages 10+
  • Platform: Arduino

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Build a clap switch – EEMENTOR STEM™ lesson

Clap, clap, and the light switches on. Clap twice again and it goes off. The small sound sensor turns loud sounds into a digital signal, and the UNO listens for two claps close together, so a single noise won't switch it by accident.

What you'll learn

  • How a sound sensor's digital output works
  • How to set a threshold with the module's knob
  • How to detect a pattern (two claps) using millis()

What you need

  • UNO R3 board and USB cable (UNO R3 Starter Kit)
  • Small sound sensor module (45-in-1 Sensor Kit)
  • 1 LED and 1 resistor, 220 Ω (UNO R3 Starter Kit)
  • Breadboard and jumper wires (UNO R3 Starter Kit)

Wiring

Breadboard wiring diagram: Build a clap switch
Breadboard view. Check each connection against the wiring table below.
From To
Sound module AO Not used
Sound module G GND
Sound module + 5V
Sound module DO UNO pin 2
UNO pin 8 220 Ω resistor, then the LED's long leg
LED short leg GND

Sensor modules from different makers can label and order their pins differently. Always follow the labels printed on your module.

Set the sensitivity

The module has two small LEDs. Turn the blue knob slowly until the signal LED is just off in a quiet room, but flashes when you clap. That point is the threshold.

The sketch

Some versions of this module switch their output to HIGH when they trigger, others to LOW. The sketch reads the output when it starts (keep things quiet and still for a moment) and treats any change from that resting state as a trigger, so it works with both.

const int SOUND_PIN = 2;
const int LED_PIN = 8;

int quietState;              // the output when the room is quiet
bool lightOn = false;
int claps = 0;
unsigned long firstClap = 0;
unsigned long lastSound = 0;

void setup() {
  Serial.begin(9600);
  pinMode(SOUND_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
  delay(500);
  quietState = digitalRead(SOUND_PIN);
  Serial.println("Clap twice to switch the light");
}

void loop() {
  bool sound = digitalRead(SOUND_PIN) != quietState;

  if (sound && millis() - lastSound > 150) {  // one clap, ignoring its echo
    lastSound = millis();
    if (claps == 0) {
      claps = 1;
      firstClap = lastSound;
    } else if (lastSound - firstClap < 800) { // second clap in time
      lightOn = !lightOn;
      digitalWrite(LED_PIN, lightOn);
      Serial.println(lightOn ? "Light on" : "Light off");
      claps = 0;
    }
  }

  if (claps == 1 && millis() - firstClap > 800) claps = 0; // too slow, start again
}

How it works

The microphone's signal goes to a comparator chip on the module. When the sound is louder than the level set by the knob, DO changes state. A clap lasts a few milliseconds and can bounce, so the sketch ignores anything within 150 ms of the last sound. Two claps less than 0.8 s apart toggle the LED.

Try this next

  • Make three claps do something different, such as blinking the LED.
  • Use the AO pin with analogRead() to print how loud each sound is.
  • Switch a low-voltage circuit with the relay lesson instead of the LED.

Troubleshooting

  • It switches on its own: turn the knob a little so the module is less sensitive.
  • Claps aren't detected: turn the knob the other way, and clap closer to the microphone.
  • It reacts to every clap: clap a bit slower; the two claps must be at least 0.15 s apart.

Build it yourself

Everything used in this lesson comes in one kit, with parts tested by our engineers.

UNO R3 Starter Kit | Arduino IDE Compatible | Free Tutorials $56.00

Shop the kit