KY-038 Sound Sensor with an ESP32 DevKit V1
A microphone module with a raw analog output and a digital output that trips on loud sounds such as claps.

On the ESP32 DevKit V1 the KY-038 is powered from 3V3, so neither AO on GPIO33 nor DO on GPIO13 can swing above the ESP32's 3.3 V inputs. The sketch takes loudness as AO's peak-to-peak swing over 50 ms and starts its running minimum at 4095, the top of the ESP32's 12-bit analogRead() scale.
Wiring the KY-038 Sound Sensor to the ESP32 DevKit V1
Pin by pin.
| KY-038 Sound Sensor | ESP32 DevKit V1 |
|---|---|
| VCC | 3V3 |
| GND | GND |
| AO | GPIO33 (D33) |
| DO | GPIO13 (D13) |
What to know on the ESP32 DevKit V1
- AO sits on GPIO33, channel 5 of ADC1. A clap detector on an ADC2 pin would stop hearing anything as soon as the Wi-Fi driver takes that converter.Espressif ESP32 Series Datasheet v5.3, p. 14; ESP-IDF Programming Guide, ADC Oneshot Mode Driver
- arduino-esp32 2.0.17 samples every ADC pin at 11 dB unless told otherwise, and Espressif warns that above a count of about 3000 (roughly 2450 mV) accuracy falls off. The level is a difference of counts, so it tolerates that better than a voltage reading, but compare levels only from the same board.arduino-esp32 2.0.17 source, cores/esp32/esp32-hal-adc.c; Espressif ESP32 Series Datasheet v5.3, p. 43
- DO comes from the LM393's open-collector output and the module's own pull-up to +. With + on 3V3 it idles at 3.3 V, which GPIO13 reads with plain INPUT; on 5 V it would exceed the VDD + 0.3 V an ESP32 input accepts.TI LM393 datasheet (SLCS005AH), p. 17; Espressif ESP32 Series Datasheet v5.3, p. 52; Joy-IT SensorKit, KY-038
- GPIO13 is not one of the ESP32's strapping pins (GPIO0, 2, 5, 12 and 15), so whatever level DO holds while the board resets cannot change how it boots.Espressif ESP32 Series Datasheet v5.3, section 3, p. 22
KY-038 Sound Sensor code for the ESP32 DevKit V1
Compiled for esp32:esp32:esp32 on 2026-10-30.
// KY-038 Sound Sensor on ESP32 DevKit V1: wiring and a free simulation at https://www.tinkered.ai/components/sound-sensor-ky-038/esp32
const int AO_PIN = 33; // KY-038 AO: the capsule's signal on a DC level
const int DO_PIN = 13; // KY-038 DO: HIGH while the sound passes the trimmer's threshold
const unsigned long WINDOW_MS = 50; // one loudness reading per 50 ms
const unsigned long CLAP_GAP_MS = 300;
int claps = 0;
unsigned long lastClap = 0;
unsigned long lastPrint = 0;
void setup() {
Serial.begin(9600);
pinMode(DO_PIN, INPUT); // the module has its own pull-up
Serial.println("KY-038 ready");
}
void loop() {
// the swing of AO over a window: its peak-to-peak is the loudness
unsigned long start = millis();
int lo = 4095, hi = 0; // the ESP32 reads 12 bits: 0 to 4095
bool loud = false;
while (millis() - start < WINDOW_MS) {
int v = analogRead(AO_PIN);
if (v < lo) lo = v;
if (v > hi) hi = v;
if (digitalRead(DO_PIN) == HIGH) loud = true;
}
int level = hi - lo;
// one clap per CLAP_GAP_MS, however long DO stays HIGH
if (loud && millis() - lastClap > CLAP_GAP_MS) {
lastClap = millis();
claps++;
Serial.print("Clap ");
Serial.print(claps);
Serial.print(" (level ");
Serial.print(level);
Serial.println(")");
}
if (millis() - lastPrint >= 1000) {
lastPrint = millis();
Serial.print("AO ");
Serial.print((lo + hi) / 2);
Serial.print(" level ");
Serial.println(level);
}
}
KY-038 Sound Sensor on the ESP32 DevKit V1: questions
Should the KY-038 use 3V3 or VIN on an ESP32?
3V3. Joy-IT wires it at 3.3 V on 3.3 V boards (Joy-IT SensorKit, KY-038), and from VIN both AO and DO could rise toward 5 V, above what the ESP32's pins accept.
What does the level the sketch prints mean?
It is the highest minus the lowest AO count in a 50 ms window, on the ESP32's 0 to 4095 scale. It rises with loudness but is not in decibels, so pick a clap threshold from your own readings.
Why does one clap only count once on the ESP32?
DO stays HIGH for as long as the sound lasts, so the sketch ignores DO for 300 ms (CLAP_GAP_MS) after each clap it counts.
Keep going
This guide as plain Markdown. /components/sound-sensor-ky-038/esp32.md