Sensors & Signal Processing
How to detect one sound and ignore louder ones
To make an Arduino react to one sound, like a doorbell or a clap, and ignore louder ones, filter before you measure. A band-pass filter, a high-pass then a low-pass, keeps the frequencies around your sound and shrinks the rest. Then take the loudness of what's left with an envelope follower, and compare it with a line.
Loudness alone can't do it
A washing machine rumbles low, a kettle hisses high, and a doorbell is one note in between, here 400 Hz. A loudness meter adds them all up, so the loudest wins. Unfiltered, the rumble reads louder than the bell, and any line high enough to ignore the machine misses the bell too.
The chain
At 4000 samples a second:
- High-pass:
slowfollowsxby 0.25, andhp = x - slow. - Low-pass:
bandfollowshpby 0.5. - Loudness:
envfollowsfabs(band)by 0.01. - Decide:
ringing = env > 50.
Through the band-pass, the bell's loudness is about 62, the hiss 39 and the rumble 35, so a line at 50 has room on both sides. With the high-pass alone, the hiss reads 99 and the bell 85, and no line can light for the bell without the hiss.
Live experiment
A clap switch at a party
Bass thumps twice a second, louder than any clap, and two claps come at 1.1 s and 2.6 s. Let the envelope listen to everything and every thump flips the lamp; raise the line to 600 and the claps are missed too; listen only to the highs and just the claps switch it.
“Pick out one sound” is lesson 6 of 6 in Alias Atoll, Sensors & Signal Processing World 3. Free with an account. Lessons unlock in order, so you’ll do the 18 before it first.
Where the corners sit
With alphas this large the simple cutoff formula reads low; working it out properly, the high-pass corner sits near 180 Hz and the low-pass corner near 440 Hz. That brackets the bell's 400 Hz, sits far above the 50 Hz rumble, and is below most of the hiss.
Filter, then measure
High-pass then low-pass, or the other way round: both cut the same frequencies. What matters is that the loudness is measured last, on what the filters left. The envelope is already a slow number, and filtering it can't tell a rumble from a bell.
Sharper bands
Simple filters are gentle slopes, so a very loud sound just outside the band still leaks through a little. For a narrow, sharp band, put more stages in a row, use a biquad, or use the Goertzel algorithm, which measures the strength of one frequency directly and is how touch-tone phone digits are decoded in software. Smoke-alarm listeners for deaf people and bat detectors pick out one band in the same way.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Pick out one sound” is lesson 6 of 6 in Alias Atoll, Sensors & Signal Processing World 3. Free with an account. Lessons unlock in order, so you’ll do the 18 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: A high-pass filter. A high-pass keeps the highs, where the clap lives, and shrinks the low bass.
- Why not “A higher line”? The bass is louder than the claps, so a line high enough to miss it misses the claps too.
- Why not “A low-pass filter”? A low-pass keeps the lows, which is exactly where the bass thumps live.
- Why not “More samples”? More samples describe the sound more finely, but they don't separate bass from claps.
Show the answer
Answer: A band-pass: it keeps the frequencies in between. The high-pass removes what's below the band and the low-pass what's above. What survives both is the band.
- Why not “Nothing gets through”? Frequencies between the two corners pass both filters. Only the lows and the highs are shrunk.
- Why not “Everything gets through”? Each filter removes something: the lows go at the high-pass, the highs at the low-pass.
- Why not “A louder signal”? Simple filters only shrink signals. What comes out is never louder than what went in.
Show the answer
Answer: Missed: it's quieter than the line. That's why you can't fix it with the line alone: you must filter before you measure.
- Why not “Caught”? The doorbell is quieter than the washing machine, so a line above the machine is above the bell too.
- Why not “Doubled”? Nothing in a line doubles a sound. It only decides whether the loudness crosses it.
- Why not “Louder”? A line doesn't change the sound. The bell is as loud as it was.
“Pick out one sound” is lesson 6 of 6 in Alias Atoll, Sensors & Signal Processing World 3. Free with an account. Lessons unlock in order, so you’ll do the 18 before it first.
Questions people ask
What is a band-pass filter?
A filter that passes a range of frequencies and shrinks those above and below it. One way to make it is a high-pass filter, which removes the lows, followed by a low-pass filter, which removes the highs.
Can an Arduino detect a specific frequency?
Yes. Sample the microphone fast enough, at least twice the frequency and preferably much more, then use a band-pass filter and an envelope, or the Goertzel algorithm, which measures one frequency's strength with a few multiplies per sample.
Why does my clap switch trigger on music?
Bass is loud, and a detector that only measures loudness can't tell it from a clap. Add a high-pass filter before the envelope so it listens only to the higher frequencies where claps live.
Related topics
- How to remove a drifting baseline from a sensor signalSubtract a slow running average from the reading, and only the quick changes are left.
- First-order vs second-order low-pass filters, in codeOne filter passes a tenth of a wave ten times past its cutoff. Two in a row pass a hundredth.
- How to measure loudness with an envelope followerThe average of a sound wave is zero. The average of its size is how loud it is.
- High-pass filter explainedA capacitor in series that blocks DC and the bass, passes the treble, and couples audio between stages.