Learn

Detecting Claps and Sound with a KY-038 Sound Sensor on a Raspberry Pi 4B

Wire a KY-038 sound sensor to a Raspberry Pi 4B, count claps from its digital output (DO), and read the raw microphone level from its analog output (AO) through an MCP3008 ADC. In the simulator, you click the sensor to make a sound.

Note: the Sound Sensor Module (KY-038) is a VIP-tier component in the simulator — you’ll need a VIP plan to use it there.

Try this directly in the free Raspberry Pi 4B simulator — no hardware or signup required. New to the GPIO header? Start with the interactive pinout guide.

What you’ll need

  • Raspberry Pi 4B
  • Sound Sensor Module (KY-038)
  • Resistor (1kΩ)
  • Resistor (2kΩ)
  • MCP3008 ADC (for the analog part)

Step by step

  1. Drag Raspberry Pi 4B and the Sound Sensor Module (KY-038) onto the Canvas.
  2. Power the sensor:
KY-038 pinPi pin
VCC5V
GNDGND

The KY-038 has two outputs. DO is a quick trigger: it stays LOW and gives a short HIGH pulse when a sound is loud enough to cross the sensor's sensitivity threshold, which is how a "clap detected" trigger behaves. AO is the raw microphone level as a voltage. In the simulator there is no real microphone: you click the sensor on the Canvas to make one clap.

  1. Part 1, clap detection (DO). DO swings up to 5V, but the Pi's GPIO pins only tolerate 3.3V, so drop it through a voltage divider:
FromTo
KY-038 DOone end of the 1kΩ Resistor
other end of the 1kΩ ResistorPi physical pin 37 and one end of the 2kΩ Resistor (the divider's middle)
other end of the 2kΩ ResistorPi GND pin

1kΩ over 2kΩ turns 5V into about 3.3V at the middle point, which is safe for the Pi. If you wire DO straight to a Pi pin instead, the Console prints a warning about 5V on a 3.3V pin when the script sets the pin up.

  1. Go to the Code tab and write a script that counts claps:
import RPi.GPIO as GPIO
import asyncio
 
GPIO.setmode(GPIO.BOARD)
GPIO.setup(37, GPIO.IN)
 
claps = 0
last = 0
 
while True:
now = GPIO.input(37)
if now == 1 and last == 0: # rising edge = a new sound event
claps += 1
print(f"Clap detected! Total: {claps}")
last = now
await asyncio.sleep(0.02)
  1. Select the sensor and look at its settings panel. Ambient noise is the steady background level, Clap loudness is how much louder a clap is, and Sensitivity threshold is the level a sound must reach to trip DO (all in percent). Click Start, then click the sensor on the Canvas to clap.

Each click gives one short pulse (about a fifth of a second), so the script prints one line per clap. The levels can only be changed while the simulation is stopped, but you can click the sensor as often as you like while it runs. If the ambient noise plus the clap loudness is below the threshold, the click is too quiet and DO stays LOW: raise the clap loudness, or lower the threshold. The sensor's onboard LED lights during a detected clap.

  1. Part 2, the raw microphone level (AO). Drag the MCP3008 ADC onto the Canvas and wire it to the Pi's fixed SPI pins (physical/BOARD numbering):
MCP3008 pinPi pin
VDD3.3V
VREF3.3V
AGNDGND
DGNDGND
CLKPhysical pin 23 (SCLK)
DOUTPhysical pin 21 (MISO)
DINPhysical pin 19 (MOSI)
CS/SHDNPhysical pin 24 (CE0)
  1. Wire the sensor's analog output to the ADC:
FromTo
KY-038 AOMCP3008 CH0
  1. Replace the script with one that reads the analog level:
import spidev
import asyncio
 
spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 1350000
 
def read_channel(channel):
adc = spi.xfer2([1, (8 + channel) << 4, 0])
return ((adc[1] & 3) << 8) + adc[2] # returns 0-1023
 
while True:
value = read_channel(0)
print(f"Sound level: {value} / 1023")
await asyncio.sleep(0.05)

AO sits at the ambient level and jumps to ambient plus clap for the short moment after each click, scaled from 0V at 0% to 5V at 100%. The MCP3008 here measures against 3.3V, so with the default 5% ambient you should see about 79 at rest. A loud clap pushes AO above 3.3V, so the reading pins at 1023 and the Console warns that the channel is over range. That warning is real: on hardware you would put a divider on AO too. Because the jump only lasts a fraction of a second, poll every 0.05 seconds or faster to catch it.

What “working correctly” looks like

  • Part 1: the Console stays quiet until you click the sensor, then prints one "Clap detected!" line per click with a rising total.
  • The sensor's onboard LED lights briefly on each detected clap.
  • Lowering the Sensitivity threshold makes quieter claps count; raising it above ambient plus clap loudness makes the sensor ignore your clicks.
  • Part 2: the reading holds steady at the ambient level (about 79 by default), then jumps toward 1023 for a moment after each click.

If something’s wrong

  • Nothing is counted when you click → check the peak against the threshold: ambient noise plus clap loudness must reach the Sensitivity threshold, and the simulation must be running (clicks only make a sound while it runs).
  • The Console warns about 5V on a 3.3V pin → DO is wired straight to the Pi pin; put the 1kΩ/2kΩ divider between DO and the pin as shown.
  • One click prints several claps → make sure the script only counts the rising edge (last == 0 and now == 1), as in the example.
  • Claps are missed by the script → don't sleep too long between reads; each pulse lasts about 0.2 seconds, so poll every 0.05 seconds or faster.
  • The analog reading is always 0 → check the MCP3008's VDD, VREF, AGND and DGND, that CS goes to physical pin 24 (CE0), and that AO goes to CH0.
  • The analog reading pins at 1023 with an over-range warning → the peak level is above the ADC's 3.3V reference; lower the clap loudness, or add a divider on AO as you would on real hardware.