Playing sounds and using buttons with Raspberry Pi
Created by Mikey Sklar
Guide Contents
Guide Contents 2
Overview 3
Install Audio 4
Install Python Module RPi.GPIO 6
Bread Board Setup for Input Buttons 7
Code 9
http://learn.adafruit.com/playing-sounds-and-using-buttons-with-
© Adafruit Industries Page 2 of 10
raspberry-pi
Overview
One of the great things about the Raspberry Pi is how everyone starts with same piece of gear.
Since the audio hardware is identical on every unit it is trivial to load the drivers and play mp3
files.
This guide describes how to connect input buttons and play audio files using a Raspberry Pi. We
make use of the Adafruit's Pi Cobbler Breakout Kit and the python module RPi.GPIO. If you have
not already used the raspberry pi as a input device this guide will show you how to wire the pull-
down resistors, GPIO pins and buttons.
http://learn.adafruit.com/playing-sounds-and-using-buttons-with-
© Adafruit Industries Page 3 of 10
raspberry-pi
Install Audio
With the Pi connected to the Internet and SSH'ed in (see our previous
tutorial (http://adafru.it/aJ5)) install the alsa audio drivers and MP3 Player
$ sudo apt-get install alsa-utils
$ sudo apt-get install mpg321
Reboot the Pi (% rebo o t) and when it comes back up, load Sound Drivers and Setup for
3.5mm Jack Output
$ sudo modprobe snd_bcm2835
$ sudo amixer cset numid=3 1
http://learn.adafruit.com/playing-sounds-and-using-buttons-with-
© Adafruit Industries Page 4 of 10
raspberry-pi
http://learn.adafruit.com/playing-sounds-and-using-buttons-with-
© Adafruit Industries Page 5 of 10
raspberry-pi
Install Python Module RPi.GPIO
The RPi.GPIO python module offers easy access to the general purpose IO pins on the
Raspberry Pi.
To get the latest version of this, take a little diversion to follow the instructions in this Adafruit
Lesson: http://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-
setup (http://adafru.it/aTH)
If you are an advanced user, you can probably skip the lesson above and issue the following
commands in a Terminal window.
$sudo apt-get update
$sudo apt-get install python-dev
$sudo apt-get install python-rpi.gpio
http://learn.adafruit.com/playing-sounds-and-using-buttons-with-
© Adafruit Industries Page 6 of 10
raspberry-pi
Bread Board Setup for Input Buttons
Important things to note:
The Adafruit Pi Cobbler Breakout Kit (http://adafru.it/914) is being used
The Adafruit Clear Full sized breadboard (http://adafru.it/239) is being used
(3) 10k pull-up resistors
(3) Momentary push-button switches (http://adafru.it/367)
GPIO pins 23, 24 and 25
Cobbler 3.3v rail is tied to positive breadboard
Cobbler gnd rail is tied to negative breadboard
Each button connection looks like:
3.3v --> 10k Pull-up Resistor --> GPIO --> Button --> GND
http://learn.adafruit.com/playing-sounds-and-using-buttons-with-
© Adafruit Industries Page 7 of 10
raspberry-pi
http://learn.adafruit.com/playing-sounds-and-using-buttons-with-
© Adafruit Industries Page 8 of 10
raspberry-pi
Code
Insert this code into a file named raspi-audio -butto n.py
Download or copy 3 mp3 files to the Pi and place them into the same directory as the
raspi-audio -butto n.py script. Replace the file names with the ones in the code
Make the file executable with chmo d
#!/usr/bin/env python
from time import sleep
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
GPIO.setup(25, GPIO.IN)
while True:
if ( GPIO.input(23) == False ):
os.system('mpg321 binary-language-moisture-evaporators.mp3 &')
if ( GPIO.input(24) == False ):
os.system('mpg321 power-converters.mp3 &')
if ( GPIO.input(25)== False ):
os.system('mpg321 vader.mp3 &')
sleep(0.1);
$ chmod +x raspi-audio-button.py
Run the python program as an administrator (with sudo ). Press the button keys to hear the
mp3 files play. Make sure you have speakers or headphones hooked up to the 3.5mm jack.
http://learn.adafruit.com/playing-sounds-and-using-buttons-with-
© Adafruit Industries Page 9 of 10
raspberry-pi
$ sudo python raspi-audio-button.py
© Adafruit Industries Last Updated: 2013-03-30 12:20:44 AM EDT Page 10 of 10