To this point we have controlled light, motion, and electrons. Let’s tackle sound next. But sound is an analog phenomena, how will our digital Arduino cope? We will once again rely on its incredible speed which will let it mimic analog behavior. To do this, we will attach a piezo element to one of the Arduino’s digital pins. Piezo buzzers are simple devices that can generate basic beeps and tones.  They work by using a piezo crystal, a special material that changes shape when voltage is applied to it.  If the crystal pushes against a diaphragm, like a tiny speaker cone, it can generate a pressure wave which the human ear picks up as sound.  Simple change the frequency of the voltage sent to the piezo and it will start generating sounds by changing shape very quickly! Let’s get to experimenting with it and get your Arduino playing “Twinkle Twinkle Little Star”.

Hardware Used:

  • 1 – Piezo Buzzer
  • 1 – Arduino UNO
  • 1 – Breadboard

You can buy all this Hardware at Createlabz.

Software Used:

Arduino IDE

Set up the Hardware:



Code Breakdown:

We assigned the pin of the speaker into the 9.

We assign how many number of notes we will use.

So this are the notes that we will use. There is a table about the notes for its frequency, period and timehigh.

We made a function playTone(), inside this function there is digitalWrite() code as this will initate the music if this function is called. HIGH means on, LOW means off.

assigning the Pin 9 which is the signal of the Buzzer as an output.


Twinkle Twinkle Little Star


Happy Birthday (first line)





Tags : arduinoarduino unoarduionbuzzermelodymusicpiezosound

Leave a Response