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 R3 | KY-038 sound sensor (microphone + LM393) | Wire | Notes |
|---|---|---|---|
| A0 | A0 | yellow | Raw microphone signal on a DC level of about half the supply. |
| GND | G | black | |
| 5V | + | red | |
| D2 | D0 | green | Comparator 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_LEVELwhile the fan is running.
Code
// 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
}
}// 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
Clap twice to toggle the L LED
Sound level 214 (D0 triggered)
Sound level 198 (D0 triggered)
Double clap - LED ONClap 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.