Find all your DIY electronics in the MakerShed. 3D Printing, Kits, Arduino, Raspberry Pi, Books & more!

raspberry-pi-week_banner-1

M33_RaspberryPi_Opener

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).

Downloads

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.

M33_RaspberryPi_Diagram

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.

M33-raspi-code-diagram

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.setmode(GPIO.BCM)
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:
try:
if (GPIO.input(23) == True):
soundChannelA.play(sndA)
if (GPIO.input(24) == True):
soundChannelB.play(sndB)
if (GPIO.input(25) == True):
soundChannelC.play(sndC)
sleep(.01)
except KeyboardInterrupt:
exit()

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.

M33_RaspberryPi_UseIt

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.


Comments

  1. Michael Margolis says:

    Matt, the links are missing

  2. Joe Misterovich says:

    Is there any chance we could get the links included in this article?

  3. Craig Couden says:

    That should fix it!

  4. Keith says:

    I just tried this out. I had forgotten to put the sound files in place and so there was nothing for the Python script to play when a button was pressed. What I heard instead was a buzzing sound as long as the button was pressed. It stopped once I released the button. I put the sound files in place and tried again and could still hear the buzzing just before the sound file was played. Any ideas on where the buzz is coming from and how to eliminate it?

    1. Joe Misterovich says:

      That sounds like a grounding issue… Maybe try a different power supply?

      1. Keith Rice says:

        Per your suggestion I tried a couple of different power supplies I have on hand but there wasn’t any difference. If I get a chance I’ll wire up a voltage regulator and try running the Pi off a battery pack and post the results.

      2. Keith Rice says:

        Interesting. I hooked everything up again today and ran it off the first power supply I used last night. No buzzes. However, if I moved or renamed a sound file so the script couldn’t find it I did get a buzz but only for the one button that looked for it. When I put the sound file back in place the buzz was gone.

        Anyway, I went ahead and wired up a 5v voltage regulator and ran the Pi off a 9V battery. No buzzes when the sound files were in place but there was a buzz when the sound files weren’t there.

        The only real difference between last night and this afternoon was the breadboard. I used my Elenco 9425 today. Last night I used my Radio Shack 278-003. Maybe the large metal plate on the bottom of the Elenco made the difference. Something else to look into.

        Thanks for the tip!

  5. Laura says:

    It looks like the buzzing you are hearing is the “play” command executing every 0.1 seconds while you hold the pin high. when you stop the play command finally gets to execute the sound completely. you can modify the python code to account for this. also, fart noises are a pretty good alternative choice for sound effects!

  6. […] simple circuit to control a Raspberry Pi soundboard for my kids’ train set (inspired by this Make Article) on a breadboard and debugging code to make it work how I wanted, I got a little too attached when […]

In the Maker Shed