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
    sudo apt-get install git
  • Download the wiringPi project using Git
    git clone git://
  • Compile and install the wiringPi project
     cd wiringPi

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.

cd examples
make softTone
sudo ./softTone

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.

#include <stdio.h>
#include <errno.h>
#include <string.h>

#include <wiringPi.h>
#include <softTone.h>

#define	PIN	3

int scale [23] = { 659, 659, 0, 659, 0, 523, 659, 0, 784, 0,0,0, 392, 0,0,0, 523, 0,0, 392, 0,0,330 } ;

int main ()
  int i ;
  wiringPiSetup () ;
  softToneCreate (PIN) ;
  for (i = 0 ; i < 23 ; ++i)
      printf ("%3d\n", i) ;
      softToneWrite (PIN, scale [i]) ;
      delay (200) ;

Make and run the softTone program again:

make softTone
sudo ./softTone

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.

Related Posts
  • Computer Skills
    Controlling DC Motors Using Python With a Raspberry PiPi motor
    In this tutorial I will show you how to connect some motors to your Raspberry Pi. Doing so will allow your Raspberry Pi to interact in the real world, making it possible to build a robot, turn on a fan on a hot day or even drop a treat for your cat or dog while your away.Read More…
  • Computer Skills
    Building a Wireless Sensor Network in Your HomeSensors
    Your environment is full of all sorts of fun and useful data, but getting it all together can be a challenge. In this tutorial you will learn how to use Arduino and Raspberry Pi technology to create inexpensive, wireless sensor nodes that report their data back to the Internet of Things. Read More…
  • Computer Skills
    How to Control a Lamp With Voice Commands and a Raspberry PiPivoice400
    Voice control is not just for smart phones and B-movies anymore. Steve Hickson has created a system to bring automation and the intelligence of Wolfram Alpha to your beck and call. In this tutorial I'll show you how to install Voice Command, configure it, and teach it to turn a lamp on and off. The control of mains power is a tricky and dangerous subject for hobbyists so you'll be using a prebuilt solution for controlling the voltage to the lamp.Read More…
  • Computer Skills
    Learn How to Use Raspberry Pi GPIO Pins With ScratchScratchgpio400
    In this tutorial, I'll show you how to install the ScratchGPIO expansion package, how to assemble a breadboard with LEDs and buttons, and run a simple Scratch game that uses the LEDs and breadboard buttons to play.Read More…
  • Computer Skills
    Control Your Christmas Lights With a Raspberry PiXmaspi400
    In this tutorial I will show you how to control five sets of Christmas lights with a Raspberry Pi and the PiFace I/O expansion board. Since the board is pre-assembled this project doesn't require any soldering. You'll be able to control the lights with a simple Python program, which is easily modifiable for different patterns or triggers. You could also setup your Pi for remote control and be able to control your Christmas lights with an iPad or mobile phone.Read More…
  • Computer Skills
    How to Build a Wireless Pi Camera Pan and Tilt PlatformRapi400
    With the release of the Raspberry Pi camera module, taking high quality pictures, or video, with the Pi has become very simple. In this tutorial, I will show you how to attach the Pi camera module to your Pi and wirelessly control a pan tilt platform. In the process, I will cover topics that include attaching the Pi camera module, connecting and controlling servos with your Pi, command line control of the camera, and some low level video streaming commands.Read More…