DEV Community

Shilleh
Shilleh

Posted on • Originally published at shillehtek.com

Arduino Nano KY-037: Double-Clap Relay Switch

Project Overview

Arduino Nano + KY-037 sound sensor clap switch: A KY-037 sound sensor detects sharp claps, and the Arduino Nano measures the time between claps to toggle a 5V relay, turning a lamp on or off with a double clap.

Clap once for nothing, then clap twice within about one second to flip the relay state. The same setup can control a fan, coffee machine, or other mains-powered device through the relay.

  • Time: ~30 minutes
  • Skill level: Beginner
  • What you will build: A double-clap-controlled relay that switches a lamp on and off.

KY-037 + relay + Arduino = the classic Clapper.

Parts List

From ShillehTek

External

  • Mains lamp + plug (routed through the relay)

Safety: Relays here switch mains AC. If you are not comfortable with mains wiring, use a smart plug controlled via Wi-Fi instead for a similar effect with less risk.

Step-by-Step Guide

Step 1 - Inspect the KY-037

Goal: Understand what the KY-037 provides so you can set a reliable clap threshold.

What to do: Identify the microphone, the trim-pot (threshold adjust), and the output pins. This build uses the digital output (DOUT), which goes HIGH when the sound level crosses the threshold.

Electret mic + op-amp + comparator + trim-pot threshold. Both analog and digital outputs.

Expected result: You know which pin is DOUT and where the sensitivity/threshold trim-pot is.

Step 2 - Wire it

Goal: Connect the KY-037 and relay module to the Arduino Nano with a shared ground.

What to do: Make the following connections:

KY-037 DOUT → D2; relay IN → D3; common GND.

  • KY-037 VCC → 5V, GND → GND, DOUT → D2
  • Relay VCC → 5V, GND → GND, IN → D3
  • Lamp routed through relay COM and NO

Full circuit - adjust the KY-037 trim-pot until a normal clap reads HIGH on DOUT.

Expected result: The Arduino Nano, KY-037, and relay module are powered from 5V and share ground, with DOUT on D2 and relay IN on D3.

Step 3 - Upload the sketch (double-clap detection)

Goal: Detect two claps close together and toggle the relay output.

What to do: Upload the code below. It debounces the sensor and counts claps within a one-second window to toggle the relay state.

Code:

const int MIC = 2;
const int RELAY = 3;
bool relayOn = false;
unsigned long lastClap = 0;
int clapCount = 0;

void setup() {
  pinMode(MIC, INPUT);
  pinMode(RELAY, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if (digitalRead(MIC) == HIGH) {
    unsigned long now = millis();
    if (now - lastClap < 80) return;   // debounce
    if (now - lastClap < 1000) {
      clapCount++;
    } else {
      clapCount = 1;
    }
    lastClap = now;
    if (clapCount == 2) {
      relayOn = !relayOn;
      digitalWrite(RELAY, relayOn ? HIGH : LOW);
      Serial.println(relayOn ? "ON" : "OFF");
      clapCount = 0;
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Expected result: Opening Serial Monitor at 9600 baud prints ON and OFF each time you successfully double-clap.

Step 4 - Test and adjust the threshold

Goal: Make the clap detection reliable without false triggers.

What to do: Power the circuit and try two claps. If voices or room noise trigger the sensor, turn the KY-037 trim-pot to make it less sensitive, then retest.

Clap, clap → lamp toggles. Adjust the KY-037 trim-pot if false triggers happen from voices.

Two claps and the lamp comes on.

Expected result: A double clap toggles the lamp reliably, and normal background sound does not.

Step 5 - Where to take it next

Goal: Explore safe, incremental extensions of the same concept.

What to do: Try one of these ideas:

  • Two-mode switch: single clap → fan, double clap → light
  • Multi-channel relay → control 4 separate appliances by clap count
  • Pair with HC-SR501 PIR so claps only count when you are in the room
  • Swap KY-037 for an INMP441 I2S mic + ESP32 + ML to detect specific gestures / words

Expected result: You have a clear next modification that still builds on clap detection and relay control.

Conclusion

You built an Arduino Nano KY-037 clap switch that detects a double clap and toggles a 5V relay to control a lamp. Along the way, you used threshold detection, basic debouncing, and simple timing logic with millis().

Want the exact parts used in this build? Grab them from ShillehTek.com. If you want help customizing this project or building something for your product, check out our IoT consulting services.

Top comments (0)