Sound – Piezo Beeper/Sounder/Speaker – whatever…

Micro-controllers such as Arduino’s etc. are embedded computers, they are designed to run code and control/read their input/output pins. So we have to add components to them to get them to do anything useful. Whether this is a temperature sensor (see here, here and here !) or a display (see here).  Sound is no exception, so we need to add some sort of sound output device, which in the simplest case can be a piezo beeper. These have terrible sound quality and generally aren’t very loud, so why use them? Well they are extremely cheap ($1,GBP1,1Euro for about 10 of them pre-wired) and take very little current to operate, meaning they can be directly connected to Arduino pins with no issues. Proper speakers draw too much current so cannot be directly connected to the Arduino (or just about any other processor either).


An example of a Piezo speaker is shown below, they often come with black and red wires but they can be connected to any polarity – it makes no difference.

Piezo Sounder or Piezo Buzzer – what’s the difference, which do I buy?
It’s essential you buy a “passive” piezo sounder/speaker. If it’s item title includes the words “buzzer” then often it includes some electronic components to enable it to beep when power is supplied. These types are handy when all you want is a simple beep and no change in tone etc. With the simpler types if you just add power you will hear nothing, when power is supplied the disk flexes (you wouldn’t notice it though as it’s too small an amount). If we disconnect power the disk goes back to normal, repeat this fast enough, on off on off etc., and the movements back and force of the disk (vibrations) cause a sound, the faster we switch the disk on and off the faster the vibrations and the higher the pitch. We need to control the pitch and so we need this “passive” type of piezo sounder. Read seller descriptions carefully if the disk is encased in a plastic housing just to make sure it is of the passive type. Certainly buying ones as shown above will be fine, you can clearly see their is no additional circuitry involved.

Wiring up a Piezo
Use the connection diagram below and connect your piezo speaker to pin 8 and Gnd.

The Software
Load up the Arduino IDE and select “File->Examples->Digital->toneMelody“.  Compile and upload to your device.  You should here a simple tune, or maybe not! If you have purchased a piezo in a plastic housing then there’s a good chance that you can hear your device quite well, if you have a bare piezo speaker then you may hear nothing. You need to attach it to something to act as a sounding board to amplify the sounds from the device. Attaching it to a project case or piece of plastic will usually suffice. I tried sellotaping one to the back of a plastic cup and it gave good results, see below:

I then further cut down the size of a the cup to around 1cm (0.5″) height with little detriment to the sound. However as mentioned if you are using a project box then hot gluing the speaker to the box with some holes drilled to let some audio come out clearly should be all that is required.

So how does it work?
Look a the animation below. The Arduino code using a command called “tone“. This uses some of the Arduino’s timers (another article) to accurately pulse pin 8 high then low again at a frequency that you have passed in. The higher the frequency the higher the pitch of the note. Each time pin 8 goes high the piezo speaker flexes in one direction, then pin 8 is brought low again and the speaker flexes back to it’s normal state. This happens typically hundreds or thousands of  times per second depending on the pitch of note required.

An improved tone command!
A programmer by the name of Tim Eckel developed a new version of the tone command called toneAC.  This has some advantage over the standard tone command but it deserves an article to itself. This will be coming soon, but for now the above should get you started.