Preview build — page text is a draft awaiting human review. Code badges come from real compiles. The flasher is real but not yet hardware-tested; the Playground is still a preview.

KY-038 sound sensor with Arduino Uno — clap switch that works

Wire a KY-038 microphone module to an Arduino Uno R3 (A0 and D2), measure sound as peak-to-peak level instead of raw readings, and build a double-clap switch.

Arduino C++Verified — core avr 1.8.8, 3 Nov 2026Compile-only

Community draft — report a mistake

The KY-038 is a small electret microphone with an amplifier and a comparator. It is the usual part for clap switches, knock detectors and “is the room noisy?” projects, and costs about ₹50.

On the Uno, power it from 5V so both outputs fit the 0–5 V inputs. Wire A0 (the analog signal) to the Uno’s A0 and D0 (the threshold output) to D2. The labels are easy to confuse: on this module A0 and D0 are its own outputs, not Uno pins.

Most KY-038 tutorials print analogRead() and wonder why nothing changes. A microphone gives a wave, not a level. The sketch samples for 50 ms and takes the difference between the highest and lowest reading. That number rises cleanly with loudness, and two claps within 0.7 s toggle the on-board LED.

Wiring

Arduino Uno R3Only the pins used are shown · 5 V logicKY-038 sound sensor (microphone + LM393)5VGNDA0D2A0G+D0
Every connection
Arduino Uno R3KY-038 sound sensor (microphone + LM393)WireNotes
A0A0yellowRaw microphone signal on a DC level of about half the supply.
GNDGblack
5V+red
D2D0greenComparator output; set its threshold with the blue pot.

3.3–5 V part, 5 V board. Both outputs follow the supply. Power it from 3.3 V on ESP32 and Pico and from 5 V on Uno, and both A0 and D0 stay within your board’s limits.

Board quirks

  • A single analogRead() of a microphone is just one point on a sound wave — it shows about 512 in silence and during a loud clap. The sketch measures peak-to-peak over 50 ms, which tracks loudness.
  • The KY-038 is not a decibel meter. It tells quiet from loud and catches claps and knocks; it can’t measure speech volume accurately or record audio.
  • D0’s polarity differs between batches, so the sketch learns its quiet level at start-up. Turn the pot until the D0 LED just goes out in a quiet room.
  • Ceiling fans and AC compressors make a steady hum. If it triggers on its own, raise CLAP_LEVEL while the fan is running.

Code

ky038-uno.inoArduino C++Open in PlaygroundOpen in PlaygroundFlash
// KY-038 sound sensor on an Arduino Uno R3 - clap switch.
// A0 → A0, G → GND, + → 5V, D0 → D2.
// Measures the sound level (peak-to-peak on A0 over 50 ms) and toggles the
// on-board "L" LED on a double clap.
const uint8_t MIC_PIN = A0, D0_PIN = 2;
const int CLAP_LEVEL = 60;  // peak-to-peak counts that count as a clap - tune for your room

bool ledOn = false;
bool d0Rest;
unsigned long lastClap = 0;

int soundLevel() {
  int lo = 1023, hi = 0;
  unsigned long start = millis();
  while (millis() - start < 50) {  // 50 ms window = 2.5 cycles of a 50 Hz hum
    int v = analogRead(MIC_PIN);
    if (v < lo) lo = v;
    if (v > hi) hi = v;
  }
  return hi - lo;
}

void setup() {
  pinMode(D0_PIN, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  d0Rest = digitalRead(D0_PIN);  // D0's quiet level - its polarity differs between boards
  Serial.println(F("Clap twice to toggle the L LED"));
}

void loop() {
  int level = soundLevel();
  bool d0 = digitalRead(D0_PIN) != d0Rest;
  if (level > CLAP_LEVEL) {
    unsigned long now = millis();
    Serial.print(F("Sound level "));
    Serial.print(level);
    Serial.println(d0 ? F("  (D0 triggered)") : F(""));
    if (now - lastClap > 150 && now - lastClap < 700) {  // second clap within 0.7 s
      ledOn = !ledOn;
      digitalWrite(LED_BUILTIN, ledOn);
      Serial.println(ledOn ? F("Double clap - LED ON") : F("Double clap - LED OFF"));
      lastClap = 0;
    } else {
      lastClap = now;
    }
    delay(100);  // skip the echo of the same clap
  }
}

Expected serial output

Serial Monitor · 9600Output
Clap twice to toggle the L LED
Sound level 214  (D0 triggered)
Sound level 198  (D0 triggered)
Double clap - LED ON

Common errors

A0 shows about 512 whatever I do

That is the DC level. Look at the peak-to-peak value the sketch prints instead — single readings don’t show loudness.

Never detects a clap

Lower CLAP_LEVEL (try 30) and clap within a metre. The KY-038’s microphone is not very sensitive; the KY-037 (big mic) hears further.

D0 LED always on

The pot is set too sensitive. Turn it until the LED goes out in silence.

↑↓ to move · Enter to open