The KY-038 is a microphone with a comparator: D0 flips when the sound is louder than the pot setting, and A0 gives the raw level. On a Pico W, D0 goes to GP16 with a pin interrupt so no clap is missed, and A0 to GP26 (ADC0).
Power it from 3V3. Two claps within a second toggle the on-board LED — the classic clap switch — and the script prints the sound level once a second.
Wiring
| Raspberry Pi Pico W | KY-038 sound sensor (microphone + LM393) | Wire | Notes |
|---|---|---|---|
| GP26 | A0 | green | GP26 = ADC0, physical pin 31. |
| GND | G | black | |
| 3V3 | + | red | 3V3 is pin 36. Outputs then stay at 3.3 V or below. |
| GP16 | D0 | yellow | GP16 is physical pin 21. |
3.3–5 V part, 3.3 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
- Adjust the blue pot first: turn it until the D0 LED is just off in a quiet room and flashes on a clap. Everything depends on this setting.
- The KY-038 is a loudness detector, not a microphone for recording — A0 gives a rough level, nowhere near audio quality.
- One clap rings for a moment and triggers several edges; the interrupt handler ignores edges within 120 ms so each clap counts once.
- A ceiling fan or pressure cooker whistle can trigger it too. Requiring two claps within a second cuts most false switching.
Code
# KY-038 sound sensor as a clap switch on a Raspberry Pi Pico W (MicroPython).
# Wiring: + -> 3V3 (pin 36), G -> GND, D0 -> GP16 (pin 21), A0 -> GP26 / ADC0 (pin 31).
# Two claps within a second toggle the on-board LED. Set the threshold with the blue pot.
from machine import ADC, Pin
import time
mic = ADC(26)
d0 = Pin(16, Pin.IN)
led = Pin("LED", Pin.OUT)
claps = []
last_edge = 0
def on_sound(pin):
global last_edge
now = time.ticks_ms()
if time.ticks_diff(now, last_edge) > 120: # one clap rings for a while - count it once
claps.append(now)
last_edge = now
d0.irq(trigger=Pin.IRQ_RISING, handler=on_sound)
print("Clap twice to toggle the LED. Turn the pot until D0's LED only flashes on a clap.")
t_level = time.ticks_ms()
while True:
now = time.ticks_ms()
while claps and time.ticks_diff(now, claps[0]) > 1000:
claps.pop(0)
if len(claps) >= 2:
claps.clear()
led.toggle()
print("Double clap -> LED", "ON" if led.value() else "OFF")
if time.ticks_diff(now, t_level) > 1000: # once a second, show the analog sound level
t_level = now
samples = [mic.read_u16() for _ in range(200)]
print("Sound level (peak-to-peak): {}".format(max(samples) - min(samples)))
time.sleep_ms(10)# KY-038 sound sensor as a clap switch on a Raspberry Pi Pico W (MicroPython).
# Wiring: + -> 3V3 (pin 36), G -> GND, D0 -> GP16 (pin 21), A0 -> GP26 / ADC0 (pin 31).
# Two claps within a second toggle the on-board LED. Set the threshold with the blue pot.
from machine import ADC, Pin
import time
mic = ADC(26)
d0 = Pin(16, Pin.IN)
led = Pin("LED", Pin.OUT)
claps = []
last_edge = 0
def on_sound(pin):
global last_edge
now = time.ticks_ms()
if time.ticks_diff(now, last_edge) > 120: # one clap rings for a while - count it once
claps.append(now)
last_edge = now
d0.irq(trigger=Pin.IRQ_RISING, handler=on_sound)
print("Clap twice to toggle the LED. Turn the pot until D0's LED only flashes on a clap.")
t_level = time.ticks_ms()
while True:
now = time.ticks_ms()
while claps and time.ticks_diff(now, claps[0]) > 1000:
claps.pop(0)
if len(claps) >= 2:
claps.clear()
led.toggle()
print("Double clap -> LED", "ON" if led.value() else "OFF")
if time.ticks_diff(now, t_level) > 1000: # once a second, show the analog sound level
t_level = now
samples = [mic.read_u16() for _ in range(200)]
print("Sound level (peak-to-peak): {}".format(max(samples) - min(samples)))
time.sleep_ms(10)Expected serial output
Clap twice to toggle the LED. Turn the pot until D0's LED only flashes on a clap.
Sound level (peak-to-peak): 412
Double clap -> LED ON
Sound level (peak-to-peak): 9874Clap twice to toggle the LED. Turn the pot until D0's LED only flashes on a clap. Sound level (peak-to-peak): 412 Double clap -> LED ON Sound level (peak-to-peak): 9874
Common errors
LED toggles by itself
Sensitivity too high — the D0 LED is flickering in normal room noise. Turn the pot back.
Claps are never detected
Sensitivity too low, or D0 is not on GP16. Clap close to the module and turn the pot until its D0 LED flashes.