PyRaspBeta

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.

Sound sensor

Pins

PinWhat it isType
AOAnalog out (audio around VCC/2)io
GNDGroundground
+Supply 3.3–5 Vpower
DODigital out: HIGH while louder than the thresholdio
SettingTypeRangeDefault
thresholdDbnumber30–11065

In circuit.py:

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:

circuit.py
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.GPIO27

From 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

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. AO rests 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.
  • DO compares 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 AO swing (up to VCC) and a higher DO. 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 AO moves like audio when sampled from an ADC, and DO flickers 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. soundDb is 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 DO stays low.

Going deeper

Further reading