1. Computer Skills
  2. Electronics

Creating a Speaker for Your Raspberry Pi Using a Piezo Element


Adding sound to a Raspberry Pi project is a simple matter of hooking up an HDMI monitor with built in speakers or attaching amplified speakers to the audio port. However, neither solution is very light or portable. Projects that require a small, energy efficient footprint don't have room for big amplified speakers. However for simple sounds, the Pi can generate frequencies on its GPIO pins that drive a piezo element to produce sounds.

This tutorial will show you how to connect an inexpensive piezo electric element to your Pi to produce simple tones.

How it Works

Piezo electric elements work on the principle that some materials such as crystals will produce electricity when put under stress, bent, or vibrated. Some uses of a piezo element include detecting vibrations such as knocks or sounds.

Piezo electric components also work in reverse, if a current is applied to the element it will bend or flex. Using this method and applying an oscillating signal will cause the element to vibrate at that same frequency as the signal.

Gather the Components and Tools

Assemble and Hook Up the Piezo Element

To hook up the piezo element you will need to solder the stranded wires to something that can connect to the Pi pins. If you are willing to commit jumper wires to the project, cut and strip one end off of the wire and solder like below. The instructions below use a bit of hookup wire to avoid snipping apart jumper wires.

  • Secure the red lead from the piezo element in your Third Hand clip and tin the exposed wire by heating the wire and coating it with solder
  • Cut two inches of red hookup wire
  • Strip both ends of the wire using your wire stripper
  • Secure the red hookup wire in the other clamp of the Third Hand so it is overlapping the lead from the piezo element
  • Solder the piezo element red lead to the red hookup wire. Optionally, you could cover this joint with heat shrink wire
  • Insert the other end of the red hookup wire into one end of the red jumper wire
  • Repeat the steps above for the black lead, black hookup wire, and black jumper wire
  • Cut a large circle from the plastic to make a vibrating membrane for the element.
  • Put a drop of super glue in the center of the plastic membrane and push the element copper side down into the glue, holding it until it has set. The plastic membrane will help make the sound louder and more consistent.
  • assembled-element600
    Assembled Sound Element
  • Connect the red jumper to your Pi pin GPIO 22 or the 8th pin down on the left when the SD Card is on the top. Use this pinout diagram for help
  • Connect the black jumper to a ground pin on your Pi. Third pin down on the right should do.
  • pipinout600
    Jumpers wired to the Pi Pins

After these steps you should have an assembled piezo element on a membrane that will amplify the buzzing of the element attached to your Pi. It should look something like the image below.

Assembled Pi and Element

Setup the Raspberry Pi

There are several guides to purchasing and installing an OS on your Raspberry Pi. Follow your choice of purchase and installation. Once your Raspberry Pi is booted up and configured move on to the next step.

Install Wiring Pi

Now that the electronics are hooked up the software to run the GPIO ports is needed. The wiringPi project provides fine control over the GPIO pins from the command line and C. There are python and other wrappers for wiringPi as well. In this example, you will install the wiringPi and we will modify a C example program to produce sound.

  • Make sure you have git installed on your Pi with the following command
  • Download the wiringPi project using Git
  • Compile and install the wiringPi project

Making Music With Your Pi

In the examples directory of the wiringPi repository is a file named softTone.c. This file demonstrates the use of the function softToneWrite which oscillates the selected pin at a given frequency. The example program causes the piezo element to play a scale. To test that you have build and wired everything correctly compile the example and run it as root.

You should now hear a scale being played on the piezo element repeatedly. Use CTRL C to stop the program. Next, you'll get a little more creative with the music. Follow the instruction below to modify the softTone.c program to play something a little more interesting. Because the array scale is just list of frequencies, you can substitute the numbers and size of the array with whatever you want. Replace the file softTone.c with the contents below. There are actually only a few small changes to the original. The result will play a single melody line and stop.

Make and run the softTone program again:

If you listen closely you might recognize this little tune. By simply modifying the scale array and the size of it on declaration and the for loop you can make the tune as long or short as as you like.


For projects that require just a simple sounds and not a full amplified audio subsystem a small piezo element can be the ideal solution. In this tutorial you learned how to attach a piezo element to your Raspberry Pi, install wiringPi, and use an examples from the wiringPi package to play music on the element.

Modifying C programs may not be for everyone so there are wiringPi wrappers for python and other languages. See the wiringPi project website for a language that may be more familiar. Now you should be able to use whatever language you are comfortable with to create your own sounds and melodies.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.