How to Use Microphones for Sound Detection with an Arduino!

LeMaster Tech
183 followers
10 Video Views·Apr 20, 2025
The microphone from this tutorial is available here:
https://amzn.to/44wtbPC
The code from this project can be found here:
https://github.com/plemaster01/arduinoMicrophone
Detecting sound inputs is a common and extremely useful tool in Arduino projects! Using a one dollar microphone add-in we can make an adjustable threshold sound detector to use to trigger any outputs we like, in this example we just use LED lights!
Thank you for watching, let me know what you'd like to see next and don't forget to leave a like on the video and subscribe to the channel!
Consider becoming a super supporter of the channel at the Patreon link below!
https://www.patreon.com/LeMasterTech
welcome back to a Master Tech in today's
video I'm going to show you how to use
microphones as sound detection inputs
for your Arduino projects
[Music]
so in today's video I'm going to show
you how to use a three pin digital input
microphone to detect sound and control
outputs based on those with an Arduino
if you want the microphone that I show
in this project they're really
affordable I'll link to a five pack for
six dollars on Amazon below but let's
talk about the physical build of my
project here you can do everything I do
in this video with just one if all
you're really trying to do is proof of
concept how these work but if you'd like
to follow along exactly I use two
microphones one where I adjust the sound
detection sensitivity potentiometer
pretty low so it picks up even quiet
volume and then the other one I turn
pretty high so I have to talk very
loudly to get the second microphone to
trigger to register that it's on and so
for the physical build each microphone
needs one pin hooked up to five volt
power from the Arduino one pin hooked up
to ground from the Arduino and then the
third pin which will be our digital
input pin runs back to any digital pin
on the Arduino I use pins two and four
if you use different ones just make sure
you update your sketch accordingly when
we get there and then the only other
thing I do is I tie two LEDs to digital
output pins 12 and 13 and then through
ground going through a 220 Ohm resistor
so that we don't overpower the LEDs and
smoke them out then just make sure to
connect the 5 volt and ground power from
your Arduino back to one of the rails on
your breadboard and we are ready to look
at the sketch for this project okay so
the code is really straightforward just
make sure that you define a constant
integer for all four of the pins we're
using the loud and quiet input pins and
then also the red and green LED output
pins and then we just need an integer
variable to track the state of our quiet
pin and our loud pin so that we can use
those accordingly in our Loop section in
the setup section of the sketch just
make sure to set the sound pins as
inputs the LED pins as outputs and use
serial dot begin at a baud rate of 9600
if you want to be able to track statuses
of your project it's always a pretty
good idea to include this in your
project now the loop section of this
code is really straightforward but an
important thing to understand to make
this make sense is that the microphones
read a one when they're not picking up
volume above that certain threshold and
they go to a zero when they are above
the noise threshold so this is called a
normally closed device in the world of
controls and automation it means it's
ordinarily sending a one normally on and
when the condition that causes a change
of state happens then it goes to a zero
so normally closed normally one but
basically all we want to do is at the
start of every Loop read the state of
our quiet and Loud pins from their
digital pins into those variables we
made and then the rest of the loop
section is determining what to do to the
LEDs based on those two readings so to
start out if they are both zeros that
means that we are giving a volume that
has been a loud enough threshold to
break both microphone to trigger our
more sensitive and less sensitive one
I'm going to just trigger the green LED
and turn off the red LED if we're at
that point and then I'm also going to
print into my serial window loud and
then because this is less common I want
to hold its state for a little bit
longer so I'm going to delay 100
milliseconds or a tenth of a second and
then in the next one which is going to
be just the quiet pin activated which is
going to be much more common since
that's way more sensitive it's going to
look very similar only now we're going
to turn just the red pin on and we're
going to delay it for just 10
milliseconds so a hundredth of a second
this is because I want the low volume
one to be much more responsive and I
want the loud volume one to be more
clear when it gets triggered it's really
just a personal preference thing
obviously as a programmer as an Arduino
hobbyist whatever change it however you
want the next scenario I'm going to
handle is actually going to be that
we're getting the loud pin but not the
quiet pin this would be weird so so I'm
going to turn both pins on and print
something like wait what or error
because that's not supposed to happen in
normal conditions so it's good to catch
this Fringe scenario maybe we can do
some debugging with it and then the last
scenario is that it's basically silent
in your room neither pin is active we'll
turn both LEDs off and we'll just print
silent to the serial window and that's
really it for the sketch I'm just going
to upload it if you want that exact
sketch you'll be able to find out a
GitHub Link in the description below
this video as well and let's take a look
at this project and see what we get okay
so once you've uploaded hopefully what
you can see is that whatever LED you
linked to your more sensitive microphone
in my case it's the red LED is going on
and off constantly with the same Pace at
which you're talking but the green LED
is waiting for much more volume so if I
get in closer to the green LED and
forgive the noise if I talk loud then
you can see the green LED starts to go
on more frequently and again these
microphones have a small Phillips head
screw terminal that you can adjust the
sensitivity of so if you want one to
basically trigger if any noise goes on
even a whisper and the other one to
trigger at regular voice volume all you
have to do is adjust the potentiometer
with a little bit of guess and check on
your side as well okay so there you have
it short and sweet that is how you use a
sound input module with an Arduino
please let me know in the comments below
what you'd like to see next on the
channel or if you had any questions
about this project as always thank you
everyone for watching leaving likes on
the video subscribing to the channel
until next time good luck with your
projects thanks bye
