Reacting to Sound
| Difficulty | Intermediate |
|---|---|
| Category | Sensors |
| Supported boards | Arduino Uno R3 and compatible ATmega328P boards |
| Time needed | 50 minutes |
| Prerequisites | Digital and Analog, and When Each Applies |
| Tested board | Not yet verified |
| Tested on | Not yet verified |
| Arduino IDE version | Not yet verified |
| Library | Not yet verified |
What it does
An electret microphone, an amplifier and a comparator on one small board. It gives you a digital pin that goes active when a sound is loud enough, and an analog pin carrying the amplified microphone signal.
This is the sensor most likely to disappoint, and it is worth saying why before you wire it up. It does not measure loudness, it cannot tell you what a sound was, and the obvious way to use the analog pin gives you a number that bounces around meaninglessly. All of those are fixable or avoidable, and this page is mostly about how.
Before you begin
Required parts and tools
- An Arduino Uno or compatible board.
- One microphone sound sensor module. This is Siratek product 2892, supplied as the KY-038 or the similar KY-030.
- Four jumper wires.
Check the pin order on your own board
This matters more here than on any other module in this batch. The Siratek board’s four-pin header is labelled, in order:
AO G + DO
That is not the AO DO GND VCC order used by the gas and soil modules. Power and ground sit in the middle, with the two outputs on the outside.
If you wire this from memory after doing the MQ-2 tutorial, you will put 5 V onto the digital output pin. Read the silkscreen, every time, on every module. The same four signals appear in at least three different orders across cheap sensor boards.
What is on the board
- The silver cylinder is an electret microphone.
- The blue multi-turn trimmer sets the comparator threshold. Multi-turn means it takes many turns to cross its range, so a quarter turn does almost nothing. Keep going.
- An LM393 comparator.
- Two LEDs: one for power, one that follows
DO.
The analog pin does not give you loudness
This is the misunderstanding worth clearing up first.
Sound is a pressure wave that swings above and below the resting air pressure, thousands of times a second. The microphone turns that into a voltage that swings above and below a midpoint, and that is what reaches AO.
So a single analogRead() catches the waveform at one arbitrary instant. In silence it returns the midpoint, somewhere around 512. In a loud room it returns a number that could be anywhere between the extremes, including the midpoint. Print those readings and you get noise in both senses.
Loudness is the size of the swing, not the height of one sample. To get it you must sample over a window of time and measure the spread:
loudness ~ (highest sample in the window) - (lowest sample in the window)
A window of about 50 milliseconds is a reasonable compromise: long enough to catch a full cycle of anything audible, short enough to feel responsive. That is what the second example does, and it is the difference between a sensor that seems broken and one that works.
What it cannot do
- It is not a sound level meter. The number is uncalibrated and depends on the trimmer, the module and the distance. It cannot be converted into decibels in any meaningful way.
- It cannot recognise sounds. There is no frequency analysis here. A clap, a door and a shout are all just a swing.
- It is not very sensitive. These modules hear a clap across a room and a conversation only up close. If you want to detect speech at a distance, this is not the part.
What it is good for: clap switches, knock detection, a noise-activated light, a visual level display. Those are all about something happened, which is exactly what it is good at.
Pin reference
| Pin or label | Connect to | Purpose | Notes |
|---|---|---|---|
AO |
Uno A0 |
Amplified microphone signal | Swings around a midpoint |
G |
Uno GND |
Ground | Second from the AO end |
+ |
Uno 5V |
Power | Third from the AO end |
DO |
Uno D2 |
Threshold output | Set by the trimmer |
Wiring steps
- Unplug the USB cable.
- Read the four labels on your board and confirm the order before connecting anything.
GtoGND.+to5V.AOtoA0.DOtoD2.- Plug the USB cable back in. The power LED should light.
Software and libraries
No library is required.
Minimal working example
Start by seeing the problem, so the fix makes sense.
// Siratek: the naive reading, which is deliberately unhelpful.
const int soundPin = A0;
void setup() {
Serial.begin(9600);
Serial.println("Siratek: watch this jump around even in a quiet room.");
}
void loop() {
Serial.println(analogRead(soundPin));
delay(50);
}
Run it in silence and the numbers wander. Clap and nothing clearly changes. This is the sketch most people write first, and the reason they conclude the sensor is faulty.
Measuring the swing instead
// Siratek: loudness as the peak-to-peak swing over a 50 ms window.
const int soundPin = A0;
const unsigned long windowMs = 50;
int measureLoudness() {
unsigned long start = millis();
int high = 0;
int low = 1023;
while (millis() - start < windowMs) {
int sample = analogRead(soundPin);
if (sample > high) {
high = sample;
}
if (sample < low) {
low = sample;
}
}
return high - low;
}
void setup() {
Serial.begin(9600);
Serial.println("Siratek: be quiet, then clap, and write both numbers down.");
}
void loop() {
int loudness = measureLoudness();
Serial.print("loudness = ");
Serial.println(loudness);
}
Now the quiet reading is small and steady, and a clap produces an obvious spike. Those two numbers are your floor and your ceiling, and every threshold should sit between them, measured in the room where the project will live.
Note there is no delay(). The window itself takes 50 milliseconds, which is the pacing.
A clap switch
// Siratek: toggling an output on a loud sound, ignoring echoes.
const int soundPin = A0;
const int lampPin = 8;
// Measure your own with the sketch above.
const int clapAbove = 120;
// A clap echoes. Ignore anything for a moment afterwards.
const unsigned long ignoreFor = 400;
unsigned long lastClap = 0;
bool lampOn = false;
int measureLoudness() {
unsigned long start = millis();
int high = 0;
int low = 1023;
while (millis() - start < 50) {
int sample = analogRead(soundPin);
if (sample > high) {
high = sample;
}
if (sample < low) {
low = sample;
}
}
return high - low;
}
void setup() {
pinMode(lampPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int loudness = measureLoudness();
if (loudness > clapAbove && millis() - lastClap > ignoreFor) {
lastClap = millis();
lampOn = !lampOn;
digitalWrite(lampPin, lampOn);
Serial.println(lampOn ? "on" : "off");
}
}
The 400 millisecond blanking period is what makes this usable. A clap in a hard-walled room produces echoes loud enough to cross the threshold again, and without it one clap toggles the lamp twice and leaves it where it started. It is the same thought as debouncing a button, for the same reason.
Using the digital pin instead
If A0 is needed elsewhere, DO will do the job, with the trimmer doing what your threshold did:
- In a quiet room, turn the trimmer until the
DOLED just goes out. - Back it off slightly.
- Clap, and check it lights.
It is a multi-turn trimmer, so give it several full turns before deciding nothing is happening.
Expected result
With the window sketch, a quiet room gives a small steady number and a clap gives a clear spike, several times larger.
The absolute values depend on the trimmer and the module. What matters is the ratio: if a clap does not produce a number several times the quiet reading, turn the sensitivity up and try again.
Troubleshooting
| Symptom | Likely cause | Check or fix |
|---|---|---|
| Analog reading jumps about in silence | Reading the waveform, not the level | Measure peak-to-peak over a window |
| No difference between quiet and loud | Single sample again | Same fix |
| Nothing works, module warm | Pin order assumed from another board | This one is AO G + DO |
| Quiet reading is not near the middle | Normal; the midpoint varies | Use the swing, not the absolute value |
| Trimmer seems to do nothing | It is multi-turn | Keep turning, several full turns |
DO always active |
Threshold too sensitive | Adjust in a quiet room until the LED goes out |
DO never active |
Threshold backed off too far | Turn back, then test with a clap |
| One clap toggles twice | Echoes crossing the threshold again | Add a blanking period |
| Cannot hear speech across a room | These modules are not sensitive | Use it for loud, close sounds |
| Readings change when a motor runs | Electrical noise on the supply | Separate supplies, grounds joined |
Next steps
- Easier first: Digital and Analog, and When Each Applies, for why one sample is not a level.
- Related products: the passive buzzer, to make sound as well as hear it.
- Next project: a bar display of the current level on an OLED, updated several times a second.
Sources and attribution
All text, wiring guidance and code in this tutorial are original Siratek work.
- The
AO G + DOheader order, the electret microphone, the multi-turn trimmer, the LM393 and the two indicator LEDs were read from the Siratek product photographs for item 2892.
The peak-to-peak window technique described here follows from how a microphone signal behaves rather than from any particular source. Threshold values are presented as something to measure in the room where the project will be used. This module is explicitly described as uncalibrated and unsuitable for sound level measurement. No third-party tutorial text, diagrams, photographs or code has been reproduced here.

