Getting Started With Raspberry PiThis project is from Getting Started with Raspberry Pi by Matt Richardson and Shawn Wallace. It assumes that you already know how to set up and operate the Raspberry Pi platform, and that you know the basics of the Python programming language. If you want to get up to speed quickly, you can find all of the above (and much more) in Richardson and Wallace’s book, available in the Maker Shed (makershed.com).


In this simple breadboard build, we’ll use a Raspberry Pi and the sound functions of the Pygame module in the Python programming language to make a soundboard. A soundboard lets you trigger the playback of sounds when you push its buttons.

You’ll also need a few uncompressed sound files, in WAV format. You can record or download your own, and there are a few sound files preloaded onto the Raspberry Pi in /usr/share/sounds/alsa/ that you can use for testing. We even collected a few public domain sound effects from The Internet Archive that you can download here.These will give you some fun files to play with right away.


1. Breadboard the circuit.

Using a female-to-male jumper wire, connect the Raspberry Pi’s ground pin to the negative rail on your breadboard.

With another female-to-male jumper wire, connect the Raspberry Pi’s 3.3V pin to the positive rail on your breadboard. Insert the 3 pushbutton switches into the breadboard, all straddling the center trench.

Now with standard jumper wires or small pieces of hookup wire, connect the positive rail of the breadboard to the top pin of each button. Next, add the pulldown resistors. Connect the bottom pin of each button to ground with a 10K resistor.

Connect each button’s bottom pin (the one with the 10K resistor) to the Raspberry Pi’s GPIO pins using female-to-male jumper wires. For this project, we use pins 23, 24, and 25.


2. Work on the code.

Create a new directory in your home directory called soundboard. Open that folder and create a file there called soundboard.py (or download it here). Open soundboard.py and type in the following code:

import pygame.mixer
from time import sleep
import RPi.GPIO as GPIO
from sys import exit
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
GPIO.setup(25, GPIO.IN)
pygame.mixer.init(48000, -16, 1, 1024)
sndA = pygame.mixer.Sound("buzzer.wav")
sndB = pygame.mixer.Sound("clap.wav")
sndC = pygame.mixer.Sound("laugh.wav")
soundChannelA = pygame.mixer.Channel(1)
soundChannelB = pygame.mixer.Channel(2)
soundChannelC = pygame.mixer.Channel(3)
print "Soundboard Ready."
while True:
if (GPIO.input(23) == True):
if (GPIO.input(24) == True):
if (GPIO.input(25) == True):
except KeyboardInterrupt:

Go to the command line and navigate to the folder where you’ve saved soundboard.py and execute the script with Python:
$ sudo python soundboard.py

After you see “Soundboard Ready,” start pushing buttons to play the sound samples.Depending on how your Raspberry Pi is set up, your sound might be sent via HDMI to your display, or it may be sent to the 3.5mm analog audio output jack on the board.

To change that, exit the script by typing CTRL+C and executing the following command to use the analog audio output:
$ sudo amixer cset numid=3 1

To send the audio through HDMI to the monitor, use:
$ sudo amixer cset numid=3 2

Of course, you aren’t limited to just 3 sounds; you can add quite a few more. Just add the files to the soundboard directory and update the code accordingly.


About Python

  • Python (python.org) is a great first programming language; it’s clear and easy to get up and running. More importantly, there are a lot of other users that you can share code with and hit up for questions.
  • Guido Van Rossum created Python, and very early on recognized its use as an accessible first language for computing. In 1999 Van Rossum put together a widely read proposal called “Computer Programming for Everybody” that laid out a vision for an ambitious program to teach programming in the elementary and secondary grade schools using Python. More than a decade later, this is actually happening with the coming of the Raspberry Pi.
  • Python is an interpreted language, which means that you can write a program or script and execute it directly rather than compiling it into machine code. Interpreted languages are a bit quicker to program and have a few side benefits. For example, in Python you don’t have to explicitly tell the computer whether a variable is a number, a list, or a string; the interpreter figures out the data types when you execute the script.
  • The Python interpreter can be run in 2 ways: as an interactive shell to execute individual commands, or as a command-line program to execute stand-alone scripts. The integrated development environment (IDE) bundled with Python and the Rasp-berry Pi is called IDLE.

MAKE Volume 33 features our special Software for Makers section covering apps for circuit board design, 3D design and printing, microcontrollers, and programming for kids. Also, meet our new Arduino-powered Rovera robot and get started with Raspberry Pi. As usual, you’ll also find fascinating makers inside, like the maniacs on our cover, the hackers behind the popular Power Racing Series events at Maker Faire.

Try your hand at 22 great DIY projects, like the Optical Tremolo guitar effects box, "Panjolele" cake-pan ukelele, Wii Nunchuk Mouse, CNC joinery tricks, treat-dispensing cat scratching post, laser-cut flexing wooden books, sake brewing, growing incredibly hot “ghost chili” peppers, and much more.

On newsstands now, by subscription, or available in the Maker Shed

Buy now!

Shawn Wallace

Shawn Wallace

Shawn Wallace is a MAKE contributor, artist, programmer, and editor living in Providence, R.I. He designs open hardware kits at Modern Device and organized the Fab Academy at the Providence Fab Lab. He makes iPhone synthesizers with the Fluxama collective and is a member of the SMT Computing Society.

  • I’ve also coded PiPlayer, an audio sample player for the RPi based on GStreamer. It has some more feature like stop playing, rewind, etc.. Hope this can help ;)

  • Mahou Baka

    Hi i’m sorry to bother you, but I had a question: i’m inputting the code here just as written, but I keep getting an indentation issue. is anyone else getting that?

    • jasontlane

      Long time, I got that too until I started editing in Notepad++ or something similar.
      Then it’s visualized, not like in Notepad.

  • Jonny Cordes

    I’m using your code for my own soundboard and it works great. The only thing is, I’m using it to play a song and for some reason the song is sped up slightly. The song plays great through VLC on the Pi but when I run your soundboard code it speeds up the song. Any ideas why?

    • Jonny Cordes

      I figured it out! For some reason you have edited the number in the pygame.mixer.init field. The defaults for the pygame mixer, found here (http://www.pygame.org/docs/ref/mixer.html) are init(frequency=22050, size=-16, channels=2, buffer=4096)

  • Lemmy

    Hello! I would like to make version with 1 button playing random sound file? Can you help me how to modify current code?

    I understand I need to remove part of code for button B and C and change somehow line:
    sndA = pygame.mixer.Sound(“buzzer.wav”)
    sndA = pygame.mixer.Sound(“PLAY RANDOM FUNCTION”)

    Thank you in advance!

  • Robbert van der Gaag

    Nice one! I made this for the office. Now we have soundeffects for certain colleagues comming in, failures etc. I want to change the input to a USB keyboard. How can I change the code so it will play a sound when a key is pressed e.g. ‘1’, ‘2’ etc. (in the same way as it is now (multiple channels for simultanious playing).

    I hope to hear from one of you! Thanks in advance!