In this video we cover how to use a piezo electric buzzer/speaker in combination with potentiometers and a pushbutton to build a fun music box project with our Arduino microcontroller! We take a detailed look at how to build the circuit and the code for this project and also discuss in detail what makes a material piezoelectric!
Hope you like the project, let me know in the comments what you thought of this video and what you'd like to see more of on the channel in the future!! Thanks for watching, goodluck with your code and don't forget to like and subscribe to the channel!!
Timestamps: 00:00 - Intro 01:02 - Circuit Build 04:04 - Quick Science-y Lesson Thing: How Piezo Speakers Work! 05:45 - Code/Sketch Construction 10:57 Outro and Showcase!
welcome to the Master Tech and in
today's video I'm going to show you how
to build this music box project with an
Arduino microcontroller
so I call this project the music box
mostly because it makes music you know
seems pretty straightforward I gave mine
a volume control potentiometer and three
modes that you can cycle through using a
push button first mode is manual where
you can use this additional
potentiometer to change the actual note
that plays through your buzzer the
second mode is a quick cycle through a
simple scale and then the third mode
plays a pre-recorded song which really
showcases how cool you can make this
build with some meticulous programming
there are brilliant people in the
Arduino Community who have already made
tons of your favorite songs and they
leave GitHub repositories of all these
songs as we'll talk about a little bit
later that you can download for
completely free now I did solder my
final project mainly because I haven't
done that in years and I wanted to get a
little better at it but you absolutely
do not have to this whole project can be
built on a breadboard and this tutorial
stands completely on its own but if you
do want to get caught up follow along
with all the Arduino tutorials on the
channel I will leave a link to that
playlist in the description of this
video as well as a card to it somewhere
in the top of the screen right about now
the kit I've used to build everything in
my Arduino Series so far is just the
official Arduino starter kit I got it
from Amazon I'll leave a link to that in
the description of the video as well and
for this project in particular in
addition to an Arduino board and a
breadboard to build everything on all
you'll need is a few jumper wires a 10
kilo Ohm resistor a push button two
potentiometers and your piezoelectric
buzzer again you don't need a soldering
kit but potentiometers do have kind of a
bad success rate in breadboards so if
your project seems a little buggy but
you're confident you have it wired up
right you might want to consider it but
huge disclaimer right here never solder
anything unless you are comfortable with
the concepts and if you're on the
younger side make sure you have adult
supervision the soldering iron gets very
hot and it can be very dangerous so
don't put yourself at risk you can build
this on a breadboard now let's dive
right into the circuit build as always
make sure to do the wiring without the
Arduino powered on this circuit and most
circuits basically boil down to power
ground and your signals back to the
Arduino start by providing power to one
leg of your manual note control
potentiometer and one leg of your push
button next connect ground to one leg of
each potentiometer and one leg of the
Piezo and then also connect ground to
the other leg of the push button but
make sure to put your 10 kilo Ohm
resistor between that leg and ground
this will provide a load for when we
push the push button down so that we
don't generate a short and now we just
have to look at incorporating our
Arduino signals start by jumpering an
analog input wire from the Third Leg of
our manual note control potentiometer
back to an analog pin I'll use a0 next
jumper a digital input wire from the
ground side of your push button but
before the 10 kilo ohm resistor back to
a digital pin on your Arduino board I'll
use two for my build now for the last
Arduino signal Loop jumper a wire from a
digital pin on your Arduino to the
unused leg of your volume control
potentiometer and for me I'll use pin 8
for this then the last Connection in
this circuit is to make the Third Leg of
that potentiometer tied to the unused
leg of the piezoelectric speaker this
completes the circuit and to summarize
what's going on here we have a push
button that will give us a digital zero
or one reading a manual note control
potentiometer which will give us a
reading between 0 and 5 volts as we
adjust it and then a digital output that
actually tells our note what to play and
passes through an additional
potentiometer where we can limit how
much total voltage gets to the Piezo now
you may not have heard of a Piezo
speaker before so to get a real
understanding of what's going on with
this little buzzer let's take a look at
them
so Piezo buzzers are like tiny speakers
but don't require an amplifier or any
other circuitry to drive them which also
means they don't get as loud when we
apply voltage to the Piezo there is a
ceramic or Crystal component that
actually changes in size and by rapidly
changing the voltage we are actually
changing its shape fast enough to make
it vibrate and this creates a pressure
wave that our ears can interpret as
sound the property of changing in size
due to alternating voltage applied to it
is known as piezoelectricity and it does
actually work in Reverse as well meaning
if we apply mechanical stress to these
components they actually generate
electricity this is the basic principle
of how original clap on clap off lights
work now specifically for our
application as a speaker the frequency
at which we apply electricity to the
element determines the frequency or note
that it's going to play however the
volume of the note or amplitude is
actually determined by how much voltage
we apply to it and this is the whole
purpose of the volume control
potentiometer because we actually need
to mechanically adjust how much
electricity gets to it some of you
attentive viewers and fans of the
channel in the Arduino series may be
saying why are we able to control the
frequency and therefore note of the
Piezo using any digital output pin on
the Arduino rather than needing a pwm or
pulse width modulation pin like we had
to use in past videos to control analog
elements like our Servo motor and that's
because Arduino boards use a method
called interrupt rather than pwm to
control piezos I'm not going to get too
far down the rabbit hole of pwm versus
interrupt today because that would take
too long and this is just a big sciency
lesson thing now let's check out the
programming or sketch for this project
we want want to start by creating an
array of integers that we'll use as the
notes for our project now while the
Piezo can be commanded to a wide range
of frequencies basically any value
between zero and five thousand we're
going to use the middle a b c d e f and
g with respect to values of 220 247 262
294 330 349 and 392. I'm not going to
get too into why those numbers represent
those notes today that's a little more
music theory and a little less science
lessony next Define an integer value to
store the state of our switch an integer
value to track what state is currently
active another integer to store our
manual note controls potentiometer's key
value as well as an integer to check
what the last state recorded of our push
button was that's really important to
make sure that we only change modes once
every time we push the push button now
the code for the setup Loop which runs
once per project initialization is very
simple start by commanding serial dot
begin and then pass in a 9600 as an
argument this is known as the body rate
or bits per second of data transfer that
our computer communicates back and forth
with the Arduino board then on the next
line Define pin mode 2 or whatever you
have your push button hooked up to as an
input now interestingly we don't need to
Define pin a or whatever pin you're
using to control your Piezo as an output
here because as we'll see we actually
use the tone command which doesn't need
to be initialized and that's it for the
setup code now let's take a look at the
Loop code this is code that runs over
and over forever as long as our project
is booted up we'll start by setting our
key Val variable equal to the reading
from pin a0 every Loop this is going to
give us whatever value our manual note
control potentiometer is currently
adjusted to next let's set our switch
State equal to a digital read command
from pin 2. now if you're ever curious
to monitor what value your potentiometer
is Reading in real time or you need to
do any troubleshooting as to why maybe
your board isn't working appropriately
the serial dot print or serial.println
if you want a new line every value is a
really good command to insert here and
read the actual state of your switch and
the value of your potentiometer so you
can boot it up and see the values
changing on your console in your program
now let's handle what to do when the
button is pressed and we just want to
check if the button is actively pressed
and if the previous state of the button
was not already pressed if we do have
these conditions we want to increment
our mode which means if we're less than
the maximum total modes in our build
which for me will be a total of two to
start then we just want to add one to
the mode but if we're already at our
maximum mode and we want to reset it
back to zero so basically create a
continuous cycle of modes then
regardless of the state of our push
button set previous switch State equal
to switch State and this is how we
handle when we push the button only
incrementing mode once because we'll
only see that the previous switch state
was zero and the new Switch state is one
once then however long we hold the
button down previous switch state will
still see that the last scan was a one
as well and so it'll know not to
increment it again then when we release
it it'll go back to zero and it'll wait
there until the next time we push the
button now let's take a look at what to
do in mode one which is going to be our
manual note control now with this simple
for loop we're going to check the value
of key Val and play one of the notes in
our array accordingly since the raw
reading we'll get is between 0 and 1 0 2
3 and we have seven notes total that we
can play in this range each note comes
out to getting about a value of 147
where they should be the note that we
play on that potentiometer now this for
Loop needs to read that for an index
value of less than seven where every
Loop iteration increments up one value
if the key value reading is between 147
times our index up to 147 times our
index plus one then send a tone to pin 8
at a frequency of our index values place
in our notes list this is a really cool
little code optimization to avoid seven
if else statements and this is all we
have to do to get the program to play a
note based on our manual potentiometer
when we enter mode one now for mode 2
we're going to create a simple little
for Loop but it's actually even more
basic because we no longer care about
the key vowel right it's going to be an
automated sequence of notes so first
let's make another for Loop that says
for index less than seven which is every
note we have in our current notes list
scale all the way up the notes list with
a little delay of 100 milliseconds so
that we have some time to actually hear
each note then make a second for Loop of
index less than five that's going to go
back down our notes list now if you're
asking why 5 and not 7 for the second
loop it's just because I want my scale
to only play the first and last notes
once and if we had a 7 here it would
play those notes twice as it shifted
between four Loops in mode two and the
last thing we need to do for this
initial build here is add an else
statement so if it's not in mode 1 or
mode 2 and we just tell the Piezo in
this mode don't play anything by writing
no tone to pin 8. this is an important
addition because otherwise the Piezo is
going to keep playing whatever note we
told it to play last now let's verify
this and load this project into our
board and play around with it a bit
before taking a look at how to add in
pre-programmed songs so immediately on
upload it shouldn't be making any noise
because we have it set to load into mode
0 by default now if I push the push
button I should enter mode 1 and you
should start to hear a buzzing make sure
your volume control knob is working just
by adjusting that one up and down and
the note shouldn't change but the volume
at which it plays should now make sure
your manual control potentiometer is
working by turning it up and down the
scale and you should notice about every
one seventh of your range the note
changes this is a super fun way to
manually interact with the music we're
generating with our build now if I push
the push button again I switch into mode
2 which is our scales and you should
start hearing it go all the way up and
down our list of notes that we
programmed it with and now if I push the
push button again and we set everything
up appropriately it should go back to
mode 0 where we get some peace and quiet
again now obviously in the intro and
I've talked about it a few times we've
heard some full programmed songs with
these piezos and I haven't explained how
that works yet the reason I didn't show
how to do that while we were doing our
look at the programming sketch is
because it's the same concept as we have
in mode one where we play the scales and
we tell it to play a specific note for a
specific amount of time but to do an
entire song sequence is a massive amount
of work and it would take a ton of time
to program in each note and duration of
a note in these popular songs
fortunately the Arduino Community is
filled with incredibly talented and
generous people who leave everything
open source online I used some of their
songs to showcase the capabilities of
this project but they are massively
talented programmers who have put all
the time into building these really cool
songs for you so huge shout out to Rob
Singh kauto who I used for the Game of
Thrones and Super Mario Brothers songs
that you probably heard in this project
there's an incredible public GitHub
repository of songs you can download and
there will be a link to that in the
description of this video as well there
you can find all the code which includes
setting up a library of basically every
note the Piezo is capable of playing and
then the full Melody that you need to
play to play these songs and all you
have to do is figure out how to use our
framework that we just built for modes
one and two to add a mode 3 and copy
that code from their githubs into your
program so you can get this full song
but if you do have any questions about
how to incorporate it into your build
just let me know in the comments below
and I can offer some guidance if you're
struggling to get that added in if you
have any questions about anything you
saw here today or anything you'd like to
see in the future on the channel be sure
to let me know about in the comments
below I hope everyone enjoys these
projects I appreciate every one of you
that watches to the end of my videos if
you want to help me do bigger and more
complex projects in the future consider
becoming a channel supporter at the
patreon link in the description of the
video as well also hit the like button
subscribe button ring the notification
button hit any other buttons you can
find and until next time good luck with
your projects and thanks for watching
bye
[Music]