Source: https://mokxi.com/learn/arduino-sound-sensor
Updated: 2026-10-05

Learn

# An Arduino clap switch with a KY-038 sound sensor

Beginner

Written by the Mokxi team, updated October 5, 2026

Start building free Open the editor

Or see every lesson.

- 261 parts on the bench
- 31 boards running now
- 1.00x real time, on every board

Click to open it in the editor

The Clap switch example on a simulated ATmega328P. Press clap: the lamp toggles once, not dozens of times.

The KY-038 is the small sound sensor in most Arduino kits: a round microphone, a blue knob and four pins. It is also the module people give up on most often, because it seems not to work. It does work. It just does less than you would expect, and this page shows you exactly what it can do.

The circuit above is an Arduino Uno with a KY-038 and a yellow lamp. Clap once and the lamp comes on. Clap again and it goes off. Press the clap button on the sensor to try it, or drag its slider to make the room louder. It runs in your browser on the sketch below.

## What you need

- An Arduino Uno
- A KY-038 sound sensor module
- An LED and a 220 ohm resistor
- A half-size breadboard and jumper wires

## Wiring

Part and pin | Goes to | Note

KY-038 D0 | Pin 2 | The comparator output: HIGH while the sound is loud enough

KY-038 A0 | A0 | The raw microphone signal

KY-038 + | 5 V | Works from 3.3 to 5 volts

KY-038 G | GND | Shared ground

LED | Pin 9 | Through a 220 ohm resistor to GND

## Why it reads 512 all the time

There is no amplifier on a KY-038. The microphone’s signal comes out on A0 as a few thousandths of a volt riding on top of half the supply. An Uno’s analogRead() counts in steps of about 5 thousandths of a volt. So in a quiet room, A0 reads about 512 and barely moves.

You can see this in the circuit above. The sketch prints the lowest and highest A0 it saw each second. In a quiet room it prints "512 to 512". Push the slider all the way to 110 dB, very loud, and it only spreads to about 497 to 526. A single clap reached 473 to 551 in our run.

So the KY-038 is really a loud noise detector. It is good for a clap, a knock or a door slam. It will not hear normal talking, on this module or on a real one.

## D0: one clap is many pulses

The digital pin is the better one to use. The board compares the microphone signal with the level set by the blue knob. D0 goes HIGH every instant the sound is above that level.

Here is the catch. A clap is not one clean HIGH. It is a burst of dozens of short pulses over about a tenth of a second. A sketch that toggles the lamp on every pulse toggles it dozens of times, and ends up in a random state.

The fix is the same one a bouncing button needs. Take the first rising edge, then ignore D0 for a while. The sketch waits 300 milliseconds. That is longer than a clap rings and shorter than two claps in a row.

Latch the first edge, then stop listening, from the Clap switch example

## Setting the sensitivity

On a real module, turn the blue knob slowly until the D0 light on the board just goes out in a quiet room. Then a clap will trip it, and background noise will not. Turn it too far one way and nothing trips it. Too far the other way and it stays on.

In Mokxi, the knob is the threshold property. Click the sensor to change it in the properties panel.

## Try it in the editor

Change the sketch to need two claps within a second before the lamp changes. Count the claps, and reset the count when a second passes with no clap.

Drag the slider up to 110 dB and leave it there. The lamp flickers on and off by itself, because a steady loud noise keeps tripping D0. That is a good reason to set the knob with care.

## Common mistakes

Reading D0 as a steady level. One clap is dozens of pulses. Latch the first one and wait.

Expecting speech or music to show up on A0. The signal is far too small for the Uno to see without an amplifier.

Mixing up the pins. The board is printed A0, G, + and D0. On the module, A0 is the name of its analog output. It happens to go to the Uno’s pin A0 here, but it could go to any analog pin.

## Questions

Why does my Arduino sound sensor always read about 512?

The KY-038 has no amplifier, so the microphone’s tiny signal sits on half the supply and moves by only a few counts. It only reacts to loud sounds like claps and knocks.

Should I use A0 or D0 on the KY-038?

D0, for a clap or knock switch. It goes HIGH when the sound passes the level set by the knob. Latch the first pulse and ignore it for a few hundred milliseconds.

Can the KY-038 measure how loud a room is?

Not well. Without an amplifier, normal sounds are smaller than one step of the Uno’s ADC. A module with a built-in amplifier works much better for sound levels.

Does Mokxi use my computer’s microphone?

No. The slider sets how loud the room is, and the clap button is one sharp clap. The small size of the signal and the burst of pulses on D0 are modeled.

Related

## Keep going

Arduino Button Debounce: See the Bounce, Then Fix It Arduino Blink Without Delay: Two LEDs, Two Rates Analog vs Digital Pins, and ADC Resolution PIR Motion Sensor: How It Works, With an Arduino How the KY-038 is modeled The Arduino Uno simulator The project page, with the full sketch Beginner

## Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.

Start building free Open the editor
