Integrating a sound sensor—like the KY‑038 (or KY‑037)—with the Raspberry Pi Pico unlocks possibilities like clap-activated lighting, sound detection alarms, or even ambient noise monitors. tutorial provides a solid foundation for wiring the sensor and reading its output via MicroPython
These modules typically use an onboard MIC, LM393 comparator, and potentiometer, producing both digital (DO) and sometimes analog (AO) outputs
2. Sound Sensor Types & How They Work
a) Digital-Only Sound SensorsModules like the KY‑038 provide a simple on/off digital output:
DO goes LOW when the ambient sound exceeds your set threshold.
- DO goes LOW when the ambient sound exceeds your set threshold.
A potentiometer lets you adjust sensitivity.
- A potentiometer lets you adjust sensitivity.
An onboard LED indicates when sound is detected
An onboard LED indicates when sound is detected
These act as basic threshold detectors.
b) Dual Analog + Digital SensorsSome versions include:
AO: a raw voltage proportional to sound amplitude (read via ADC).
- AO: a raw voltage proportional to sound amplitude (read via ADC).
DO: a threshold output similar to digital-only modules
- DO: a threshold output similar to digital-only modules
With AO, you can measure sound levels continuously and trigger events when thresholds are exceeded.
3. Hardware You’ll NeedRaspberry Pi Pico or Pico W (MicroPython firmware)
- Raspberry Pi Pico or Pico W (MicroPython firmware)
Sound sensor module (e.g., KY‑038, KY‑037)
- Sound sensor module (e.g., KY‑038, KY‑037)
Breadboard, jumper wires
- Breadboard, jumper wires
Optional: LED + 220 Ω resistor, relays, buzzers (to demonstrate outputs)
- Optional: LED + 220 Ω resistor, relays, buzzers (to demonstrate outputs)
(Optional) tiny screwdriver for adjusting the potentiometer
- (Optional) tiny screwdriver for adjusting the potentiometer
Connect the sensor and LED like this:
Sensor Pin
Pico Pin
Notes
VCC
3V3
Provides power to the module
GND
GND
Common ground
DO
GP0
Reads sound vector as HIGH/LOW (1/0)
LED+resistor
GP16
Optional: indicates sound detection
LED‑leg to GND or 3V3
—
LED wiring as per your logic
A popular Instructables design shows the LED lighting whenever sound is detected .
B) Dual AO + DO WiringUsing both outputs, your wiring would include:
AO → GP26–28 to read analog signals.
- AO → GP26–28 to read analog signals.
DO → some GPIO, used for threshold-triggered reactions.
- DO → some GPIO, used for threshold-triggered reactions.
VCC/GND → 3.3 V & GND, respectively
VCC/GND → 3.3 V & GND, respectively
5. MicroPython Code Examples
A) Digital-Only Detectionpython
CopyEdit
from machine import Pin
import time
sensor = Pin(0, Pin.IN)
led = Pin(16, Pin.OUT)
while True:
sound_detected = sensor.value() == 0 # DO goes LOW upon detection
led.value(sound_detected)
if sound_detected:
print("🔊 Sound detected!")
time.sleep(0.1)
python
CopyEdit
from machine import Pin
import time
sensor = Pin(0, Pin.IN)
led = Pin(16, Pin.OUT)
while True:
sound_detected = sensor.value() == 0 # DO goes LOW upon detection
led.value(sound_detected)
if sound_detected:
print("🔊 Sound detected!")
time.sleep(0.1)
This loops, reading the digital pin: when sound is loud, LED lights and a message prints. It echoes the Techatronic example
B) Edge Detection Using IRQpython
CopyEdit
from machine import Pin
import utime
def sound_handler(pin):
utime.sleep_ms(50)
if pin.value() == 0:
print("🔊 Sound event!")
sensor = Pin(0, Pin.IN)
sensor.irq(trigger=Pin.IRQ_FALLING, handler=sound_handler)
python
CopyEdit
from machine import Pin
import utime
def sound_handler(pin):
utime.sleep_ms(50)
if pin.value() == 0:
print("🔊 Sound event!")
sensor = Pin(0, Pin.IN)
sensor.irq(trigger=Pin.IRQ_FALLING, handler=sound_handler)
This interrupts on falling edge, firing the handler only when new sounds are detected. A similar approach is discussed in “Digital sound sensor and the Raspberry Pico”
C) Analog Level Monitoringpython
CopyEdit
from machine import ADC
import time
adc = ADC(26)
threshold = 20000 # Tune based on readings
while True:
level = adc.read_u16()
print("Level:", level)
if level > threshold:
print("🔊 LOUD SOUND!")
time.sleep(0.2)
python
CopyEdit
from machine import ADC
import time
adc = ADC(26)
threshold = 20000 # Tune based on readings
while True:
level = adc.read_u16()
print("Level:", level)
if level > threshold:
print("🔊 LOUD SOUND!")
time.sleep(0.2)
Great for logging or plotting ambient noise levels; AO often operates in a limited range.
6. Calibrating & TipsPotentiometer tuning: Adjust sensitivity using a screwdriver until detection is reliable
- Potentiometer tuning: Adjust sensitivity using a screwdriver until detection is reliable
Debouncing: Brief delays (~50 ms) help avoid false triggers from echoes or noise.
- Debouncing: Brief delays (~50 ms) help avoid false triggers from echoes or noise.
Record AO readings: Helps determine threshold values more accurately than guessing.
- Record AO readings: Helps determine threshold values more accurately than guessing.
Use interrupts for efficiency: IRQ-based detection frees CPU cycles compared to polling.
- Use interrupts for efficiency: IRQ-based detection frees CPU cycles compared to polling.
Power supply stability: For cleaner AO readings, use a stable 3.3 V source and ensure solid grounding.
- Power supply stability: For cleaner AO readings, use a stable 3.3 V source and ensure solid grounding.
Clap-activated lighting: Trigger LEDs, relays, or smart circuits based on clap detection—classic DIY project
Clap-activated lighting: Trigger LEDs, relays, or smart circuits based on clap detection—classic DIY project
Noise monitoring: Ideal for baby rooms, integrated with Pico W to send alerts or graphs to phones or the cloud
- Noise monitoring: Ideal for baby rooms, integrated with Pico W to send alerts or graphs to phones or the cloud
Security alarms: Audio threshold detection can activate alarms or notifications on loud intrusions.
- Security alarms: Audio threshold detection can activate alarms or notifications on loud intrusions.
Sound-visual effects: Drive NeoPixel LED strips or NeoMatrix displays based on microphone volume level.
- Sound-visual effects: Drive NeoPixel LED strips or NeoMatrix displays based on microphone volume level.
Robotics & toys: Trigger motors or servos based on sounds—command robots with snaps or voice.
- Robotics & toys: Trigger motors or servos based on sounds—command robots with snaps or voice.
IoT dashboards: Pico W can send sound data wirelessly to dashboards like Initial State or MQTT servers
- IoT dashboards: Pico W can send sound data wirelessly to dashboards like Initial State or MQTT servers
Spectrum analysis: While basic modules aren’t suited for this, coupling with high-quality mics and FFT libraries enable spectrum visualization.
- Spectrum analysis: While basic modules aren’t suited for this, coupling with high-quality mics and FFT libraries enable spectrum visualization.
Issue
Possible Fix
Constant LED on/off
Adjust sensitivity pot. Ensure DO is wired to input pin.
No sensor readings
Check wiring, ensure DO/AO pins are correct, verify 3.3 V supply.
AO values fluctuate
Add smoothing factor or capacitor, ensure stable power, ground correctly.
False sound triggers
Increase debounce delay or threshold; isolate from vibrations.
Interrupt not firing
Enable internal pull-up/pull-down, ensure correct interrupt type.
9. Enhancing & ExpandingAdd multiple sensors: Use multiple units to detect sound directionality or pattern recognition.
- Add multiple sensors: Use multiple units to detect sound directionality or pattern recognition.
Combine sensors: Fuse sound with motion detectors (PIR), light sensors (LDR), or temperature sensors (DHT11).
- Combine sensors: Fuse sound with motion detectors (PIR), light sensors (LDR), or temperature sensors (DHT11).
Build interactive menus: Use buttons and the LCD to adjust thresholds or switch modes.
- Build interactive menus: Use buttons and the LCD to adjust thresholds or switch modes.
Voice command detection: Advance to simple voice detection by syncing clicks or frequency band filters.
- Voice command detection: Advance to simple voice detection by syncing clicks or frequency band filters.
Noise-level service: Create a baby-monitor style service with alerts and responsive lights using Pico WNoise-level service: Create a baby-monitor style service with alerts and responsive lights using Pico W
Comments