Playing Sounds and Using Buttons With Raspberry Pi
Playing Sounds and Using Buttons With Raspberry Pi
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
Reboot the Pi (% rebo o t) and when it comes back up, load Sound Drivers and Setup for
3.5mm Jack Output
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.
http://learn.adafruit.com/playing-sounds-and-using-buttons-with-
© Adafruit Industries Page 6 of 10
raspberry-pi
Bread Board Setup for Input Buttons
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
#!/usr/bin/env python
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