The KY-038 is a microphone with a comparator — the module behind clap switches and “sound-activated” lights. It gives a raw audio signal and a digital “loud!” output.
On the Nano the analog output goes to A0 and the digital output to D2. A clap toggles the on-board LED, and once a second the sketch prints how loud the room is.
Wiring
| Arduino Nano | KY-038 sound sensor (microphone + LM393) | Wire | Notes |
|---|---|---|---|
| A0 | A0 | yellow | Microphone signal riding on about half the supply. |
| GND | G | black | |
| 5V | + | red | |
| D2 | D0 | green | Comparator output — HIGH on a loud sound. |
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
- Set the trimmer first: turn it until the module’s second LED just goes off in a quiet room. Too sensitive and every fan noise counts as a clap.
- The analog output is an AC sound wave around ~2.5 V, not a volume level. The sketch measures the peak-to-peak swing over 50 ms to get loudness.
- The KY-038 is not a sound-level meter: it can’t give dB values and it misses quiet speech. For voice, use a MAX9814 or INMP441 module.
- Don’t mix up the module’s pins with the Nano’s: the module’s A0 pin goes to the Nano’s A0, and its D0 to D2 — not to RX0.
Code
// KY-038 sound sensor on an Arduino Nano as a clap switch, plus a loudness reading.
// A0 → A0, G → GND, + → 5V, D0 → D2. Set the blue trimmer so its LED just goes off in silence.
const uint8_t PIN_A = A0;
const uint8_t PIN_D = 2;
bool lampOn = false;
void setup() {
Serial.begin(115200);
pinMode(PIN_D, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.println(F("Clap to toggle the on-board LED"));
}
int loudness() { // peak-to-peak over 50 ms
int lo = 1023, hi = 0;
unsigned long start = millis();
while (millis() - start < 50) {
int v = analogRead(PIN_A);
if (v < lo) lo = v;
if (v > hi) hi = v;
}
return hi - lo;
}
void loop() {
static unsigned long lastClap = 0;
if (digitalRead(PIN_D) == HIGH && millis() - lastClap > 400) { // D0 goes HIGH on a loud sound
lastClap = millis();
lampOn = !lampOn;
digitalWrite(LED_BUILTIN, lampOn ? HIGH : LOW);
Serial.println(lampOn ? F("Clap! LED on") : F("Clap! LED off"));
}
static unsigned long lastPrint = 0;
if (millis() - lastPrint > 1000) {
lastPrint = millis();
Serial.print(F("Loudness (p-p): "));
Serial.println(loudness());
}
}// KY-038 sound sensor on an Arduino Nano as a clap switch, plus a loudness reading.
// A0 → A0, G → GND, + → 5V, D0 → D2. Set the blue trimmer so its LED just goes off in silence.
const uint8_t PIN_A = A0;
const uint8_t PIN_D = 2;
bool lampOn = false;
void setup() {
Serial.begin(115200);
pinMode(PIN_D, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.println(F("Clap to toggle the on-board LED"));
}
int loudness() { // peak-to-peak over 50 ms
int lo = 1023, hi = 0;
unsigned long start = millis();
while (millis() - start < 50) {
int v = analogRead(PIN_A);
if (v < lo) lo = v;
if (v > hi) hi = v;
}
return hi - lo;
}
void loop() {
static unsigned long lastClap = 0;
if (digitalRead(PIN_D) == HIGH && millis() - lastClap > 400) { // D0 goes HIGH on a loud sound
lastClap = millis();
lampOn = !lampOn;
digitalWrite(LED_BUILTIN, lampOn ? HIGH : LOW);
Serial.println(lampOn ? F("Clap! LED on") : F("Clap! LED off"));
}
static unsigned long lastPrint = 0;
if (millis() - lastPrint > 1000) {
lastPrint = millis();
Serial.print(F("Loudness (p-p): "));
Serial.println(loudness());
}
}Expected serial output
Clap to toggle the on-board LED
Loudness (p-p): 6
Clap! LED on
Loudness (p-p): 214Clap to toggle the on-board LED Loudness (p-p): 6 Clap! LED on Loudness (p-p): 214
Common errors
Toggles by itself
Trimmer set too sensitive, or vibration through the table. Turn it back and mount the module on foam.
Loudness always 0
AO not on A0, or the trimmer turned so far the output sits at 0 V or 5 V.