Sound sensor
The KY-038 sound sensor module costs $3 and does one job: it notices noise. An electret capsule feeds an LM393 comparator, the blue trimmer sets the threshold, and DO goes high while the sound is louder than that. AO exposes the microphone's raw signal — a waveform around half the supply, not a loudness reading — which you can sample through an ADC if you want to measure how loud instead of just how loud enough. It is the classic clap switch, and the honest answer to "why does my sound sensor not hear speech?" is that this module detects sharp, loud events, not voices.
Pins
| Pin | What it is | Type |
|---|---|---|
| AO | Analog out (audio around VCC/2) | io |
| GND | Ground | ground |
| + | Supply 3.3–5 V | power |
| DO | Digital out: HIGH while louder than the threshold | io |
| Setting | Type | Range | Default |
|---|---|---|---|
thresholdDb | number | 30–110 | 65 |
In circuit.py:
SoundSensor()— Sound sensor — $3- Pins: 4
thresholdDb is the trimmer in decibels (default 65 dB, range 30–110). DO is active high while the level is above it; AO is the audio itself.
Wiring it in circuit.py
Power it from 3.3 V so DO swings to a GPIO-safe level:
from wiring import *
pi = Pi()
mic = SoundSensor()
pi.V3_3 >> mic.plus # 3.3 V keeps DO in GPIO range
mic.gnd >> pi.GND
mic.do >> pi.GPIO27From 5 V the module works too, but then DO (and AO) swing to 5 V and need a divider before the Pi — the same trap as the MQ-2. AO should go to an ADC, never straight to a GPIO.
Driving it in main.py
from gpiozero import DigitalInputDevice
from time import sleep
clap = DigitalInputDevice(27) # DO: high while loud
while True:
print("loud" if clap.value else "quiet")
sleep(0.1)DO chatters at the threshold — a clap arrives as a burst of pulses, not one clean level — so a real switch latches the first edge and ignores DO for a moment afterwards. gpiozero's when_activated on the input device gives you that first edge for free.
Electrical notes
- Supply is 3.3–5.5 V at about 4 mA; the output impedance is about 1 kΩ.
- Sensitivity is 0.3 V at 94 dB SPL.
AOrests at half the supply and swings around it with the waveform, so its peak-to-peak amplitude grows with loudness — measure it by sampling fast and taking min/max. DOcompares the instantaneous signal with the trimmer; there is no hysteresis, so it flickers near the threshold. Treat it as "a loud event happened", latch it, and hold off briefly.- Higher supply means a wider
AOswing (up toVCC) and a higherDO. The trimmer sets the fraction of the swing at which the comparator trips. - The capsule responds to the whole audio band, so a door slam and a dropped book are indistinguishable.
Simulator vs real hardware
- The sound level comes from the feed: its zone's ambient noise, or this computer's microphone in the Lab. A slider, a wave or recorded audio can drive it.
- Every 5 ms the model draws a point on a waveform whose amplitude follows the level, so
AOmoves like audio when sampled from an ADC, andDOflickers the way the real comparator does. - There is no absolute calibration and no frequency response: the module has no A-weighting filter and neither does the model.
soundDbis a level, not a physical SPL meter reading. - The amplitude comes from a 30 dB floor: with no feed it behaves as a quiet room, and
DOstays low.
Going deeper
- Primer: analog inputs and ADCs — sampling
AOfor a loudness estimate. - Its twin from the same family of comparator boards: MQ-2 gas sensor.
- Sensors that react to the world: PIR, LDR, HC-SR04.
- Python side: gpiozero notes.
Further reading
- Joy-IT: KY-038 microphone sound sensor — pinout, threshold behaviour and Raspberry Pi example.
- gpiozero: DigitalInputDevice —
valueand edge callbacks. - Adafruit: Electret microphone amplifier — what is inside a microphone module and how to read its analog signal.