# Playing sounds and using buttons with Raspberry Pi

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/000/877/medium800/raspberry_pi_IMG_4926.jpg?1396766100)

This guide describes how to use CircuitPython on a Raspberry Pi to trigger audio file playback using tactile button presses. This tutorial works will all versions of Raspberry Pi hardware to date (v1, v2, v3, Zero, etc.) If you have not already used the Raspberry Pi as a input device this guide will show you how to wire up the buttons to the GPIO pins and access their state from a python script.&nbsp;

## To Follow This Tutorial You Will Need
- [A Raspberry Pi](https://www.adafruit.com/?q=raspberry%20pi)&nbsp;(compatible with all 26pin and 40pin Pi releases to date)
- [Pi T-Cobbler Plus,](https://www.adafruit.com/product/2028)&nbsp;[Pi Cobbler Plus for Model B+ / Pi 2](https://www.adafruit.com/products/2029)&nbsp;or the original&nbsp;[Pi Cobbler](https://www.adafruit.com/product/914)
- [(1) Half size breadboard](https://www.adafruit.com/products/64)
- [Hook-up Wire](https://www.adafruit.com/index.php?main_page=adasearch&q=hook-up+wire+spool "Link: https://www.adafruit.com/index.php?main\_page=adasearch&q=hook-up+wire+spool")
- [(3) Tactile Button Switches](https://www.adafruit.com/product/367)
- [(1) 5v 2.5A Switching Power Supply](https://www.adafruit.com/product/1995)

# Playing sounds and using buttons with Raspberry Pi

## Pi Prep

# Update Your Pi to the Latest Raspbian
Your Pi will need to be running the latest version of Raspbian. This tutorial was written using Raspbian Stretch (Nov. 2018). Checkout our guide for&nbsp;[Preparing an SD Card for your Raspberry Pi](https://learn.adafruit.com/adafruit-raspberry-pi-lesson-1-preparing-and-sd-card-for-your-raspberry-pi)&nbsp;if you have not done so already. After the installation is complete be sure and run the following commands to make sure your installation packages are up to date.&nbsp;

```
$ sudo apt-get update -y
$ sudo apt-get upgrade -y
```

# Install pip3
pip3 is already installed with a full Raspbian installation, but the Raspbian Lite does not include pip3 so it needs to be installed as shown below.

```
$ sudo apt-get install python3-pip

```

# Install adafruit-blinka
```
$ sudo pip3 install adafruit-blinka
```

# Playing sounds and using buttons with Raspberry Pi

## Install CircuitPython for Raspberry Pi

# Install adafruit\_blinka
```
$ sudo pip3 install adafruit-blinka
```

If you have installed adafruit-blinka in the past this would be a good time to see if any upgraded version is available.&nbsp;

```
$ pip3 install --upgrade adafruit_blinka
```

# Playing sounds and using buttons with Raspberry Pi

## Circuit Diagram

All Raspberry Pi versions released to date can use the CircuitPython wiring below. We have used the same GPIO pins 23, 24 and 25 as they are available on Raspberry Pi v1, v2, v3 and Zeros. It does not matter if your cobbler is a 40-pin version or the earlier 26-pin version.

# CircuitPython T-Cobbler
![](https://cdn-learn.adafruit.com/assets/assets/000/067/349/medium800/components_circuitpython-cobbler_bb.jpg?1544302943)

Note that we are not using pull up resistors and do not need to connect to the 3.3v rail. We can do direct pin to button and button to GND connections with CircuitPython configuration.

# Playing sounds and using buttons with Raspberry Pi

## CircuitPython Code

Now for a bit of Python. There are two example files you can download to your Pi and execute with python3. The first is an script that plays a different audio bell when when each button is pressed. The second code example is a jukebox which allows one to navigate through the files, play a selected sample and stop playback using the same three buttons.

Be sure to download the following MP3 files so that you can hear the samples playing.&nbsp;

1. [temple-bell.mp3](https://github.com/adafruit/Adafruit_Learning_System_Guides/raw/master/Playing_Sounds_and_Using_Buttons_with_Raspberry_Pi/temple-bell.mp3)
2. [temple-bell-bigger.mp3](https://github.com/adafruit/Adafruit_Learning_System_Guides/raw/master/Playing_Sounds_and_Using_Buttons_with_Raspberry_Pi/temple-bell-bigger.mp3)
3. [temple-bell-huge.mp3](https://github.com/adafruit/Adafruit_Learning_System_Guides/raw/master/Playing_Sounds_and_Using_Buttons_with_Raspberry_Pi/temple-bell-huge.mp3)

## Pulling Down the Files on the Pi
We can use wget to pull down the audio files and code directly to the Pi we are working on.

```
$ wget https://github.com/adafruit/Adafruit_Learning_System_Guides/raw/master/Playing_Sounds_and_Using_Buttons_with_Raspberry_Pi/temple-bell.mp3
$ wget https://github.com/adafruit/Adafruit_Learning_System_Guides/raw/master/Playing_Sounds_and_Using_Buttons_with_Raspberry_Pi/temple-bell-bigger.mp3
$ wget https://github.com/adafruit/Adafruit_Learning_System_Guides/raw/master/Playing_Sounds_and_Using_Buttons_with_Raspberry_Pi/temple-bell-huge.mp3
$ wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Playing_Sounds_and_Using_Buttons_with_Raspberry_Pi/audio-button.py
$ wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Playing_Sounds_and_Using_Buttons_with_Raspberry_Pi/simple-jukebox.py
```

## Audio Button
https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Playing_Sounds_and_Using_Buttons_with_Raspberry_Pi/audio-button.py

Running the **audio-button.py** script as indicated below should result in each button playing a different bell tone.

```
sudo python3 ./audio-button.py
```

## Simple Jukebox
```
    https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/master/Playing_Sounds_and_Using_Buttons_with_Raspberry_Pi/simple-jukebox.py
  
```

Running the **simple-jukebox.py** &nbsp;as indicated below should result in each button having a different function. Audio selection, playing of the selected file and stop playing the audio file.

```
$ sudo python3 ./simple-jukebox.py
```

Here, we get a list of mp3 files in the current directory, and then set up the following control scheme:

- **button #1** (GPIO 23) clicks through the list of available mp3s
- **button #2** (GPIO 24) plays the currently selected mp3
- **button #3** (GPIO 25) runs a command that kills any existing mp3 player processes, stopping whatever mp3s are playing right now


## Featured Products

### Raspberry Pi 3 - Model B+ - 1.4GHz Cortex-A53 with 1GB RAM

[Raspberry Pi 3 - Model B+ - 1.4GHz Cortex-A53 with 1GB RAM](https://www.adafruit.com/product/3775)
The Raspberry Pi 3 Model B is the most popular Raspberry Pi computer made, and the Pi Foundation knows you can always make a good thing _better_! And what could make the Pi 3 better? How about a&nbsp;_faster_ processor, 5 GHz WiFi, and updated Ethernet chip with PoE capability?...

In Stock
[Buy Now](https://www.adafruit.com/product/3775)
[Related Guides to the Product](https://learn.adafruit.com/products/3775/guides)
### Half Sized Premium Breadboard - 400 Tie Points

[Half Sized Premium Breadboard - 400 Tie Points](https://www.adafruit.com/product/64)
This is a cute, half-size breadboard with&nbsp;400 tie points, good for small projects. It's 3.25" x 2.2" / 8.3cm&nbsp;x 5.5cm&nbsp;with a standard double-strip in the middle and two power rails on both sides.&nbsp;You can pull the power rails off easily to make the breadboard as...

In Stock
[Buy Now](https://www.adafruit.com/product/64)
[Related Guides to the Product](https://learn.adafruit.com/products/64/guides)
### Tactile Button switch (6mm) x 20 pack

[Tactile Button switch (6mm) x 20 pack](https://www.adafruit.com/product/367)
Little clicky switches are standard input "buttons" on electronic projects. These work best in a PCB but [can be used on a solderless breadboard as shown in this tutorial](https://learn.adafruit.com/adafruit-arduino-lesson-6-digital-inputs?view=all). The pins are normally...

In Stock
[Buy Now](https://www.adafruit.com/product/367)
[Related Guides to the Product](https://learn.adafruit.com/products/367/guides)
### Adafruit Raspberry Pi B+ / Pi 2 / Pi 3 Case - Smoke Base

[Adafruit Raspberry Pi B+ / Pi 2 / Pi 3 Case - Smoke Base](https://www.adafruit.com/product/2258)
It took awhile to perfect&nbsp;-&nbsp;but that's okay&nbsp;since we can now safely say that the Adafruit case for Raspberry Pi Model B+ / Pi 2 / Pi 3&nbsp;is The Single&nbsp;Greatest Raspberry Pi Model B+ Case Ever.

This enclosure&nbsp;was designed by Mike Doell - just like our...

In Stock
[Buy Now](https://www.adafruit.com/product/2258)
[Related Guides to the Product](https://learn.adafruit.com/products/2258/guides)
### 5V 2.5A Switching Power Supply with 20AWG MicroUSB Cable

[5V 2.5A Switching Power Supply with 20AWG MicroUSB Cable](https://www.adafruit.com/product/1995)
Our all-in-one 5V 2.5 Amp + MicroUSB cable power adapter is the perfect choice for powering single-board computers like Raspberry Pi, BeagleBone, or anything else that's power-hungry!

This adapter was specifically designed to provide 5.25V, not 5V, but we still call it a 5V USB...

In Stock
[Buy Now](https://www.adafruit.com/product/1995)
[Related Guides to the Product](https://learn.adafruit.com/products/1995/guides)
### Assembled Pi T-Cobbler Plus - GPIO Breakout

[Assembled Pi T-Cobbler Plus - GPIO Breakout](https://www.adafruit.com/product/2028)
 **This is the assembled version of the Pi T-Cobbler Plus. &nbsp;It only works with the Raspberry Pi Model Zero, A+, B+, Pi 2, Pi 3 & Pi 4!** (Any Pi with 2x20 connector)  
  
The Raspberry Pi has landed on the Maker World like a 40-GPIO pinned, quad-USB ported, credit...

In Stock
[Buy Now](https://www.adafruit.com/product/2028)
[Related Guides to the Product](https://learn.adafruit.com/products/2028/guides)
### Assembled Pi Cobbler Plus - Breakout Cable

[Assembled Pi Cobbler Plus - Breakout Cable](https://www.adafruit.com/product/2029)
The Raspberry Pi B+ / Pi 2 / Pi 3 / Pi 4&nbsp;has landed on the Maker World like a 40-GPIO pinned, quad-USB ported, credit card sized bomb of DIY joy. And while you can use most of our great Model B accessories by hooking up our [downgrade...](https://www.adafruit.com/product/1986)

In Stock
[Buy Now](https://www.adafruit.com/product/2029)
[Related Guides to the Product](https://learn.adafruit.com/products/2029/guides)
### Adafruit Assembled Pi Cobbler Breakout + Cable for Raspberry Pi

[Adafruit Assembled Pi Cobbler Breakout + Cable for Raspberry Pi](https://www.adafruit.com/product/914)
Now that you've finally got your hands on a [Raspberry Pi® Model B](http://www.raspberrypi.org/), you're probably itching to make some fun embedded computer projects with it. What you need is an add on prototyping Pi Cobbler from Adafruit, which can break out all those...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/914)
[Related Guides to the Product](https://learn.adafruit.com/products/914/guides)

## Related Guides

- [CircuitPython Libraries on Linux and Google Coral](https://learn.adafruit.com/circuitpython-on-google-coral-linux-blinka.md)
- [Single Channel LoRaWAN Gateway for Raspberry Pi](https://learn.adafruit.com/raspberry-pi-single-channel-lorawan-gateway.md)
- [Windows IoT Core Application Development: Headed Blinky](https://learn.adafruit.com/windows-iot-application-development-headed-blinky.md)
- [Adafruit's Raspberry Pi Lesson 11. DS18B20 Temperature Sensing](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing.md)
- [LoRa and LoRaWAN Radio for Raspberry Pi](https://learn.adafruit.com/lora-and-lorawan-radio-for-raspberry-pi.md)
- [Adafruit's Raspberry Pi Lesson 12. Sensing Movement](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-12-sensing-movement.md)
- [Setting up a Raspberry Pi as a WiFi Access Point](https://learn.adafruit.com/setting-up-a-raspberry-pi-as-a-wifi-access-point.md)
- [Analog Inputs for Raspberry Pi Using the MCP3008](https://learn.adafruit.com/reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi.md)
- [Pi Hole Ad Detection Display with PiTFT](https://learn.adafruit.com/pi-hole-ad-pitft-tft-detection-display.md)
- [Moonshine Voice Control on Raspberry Pi](https://learn.adafruit.com/moonshine-voice-control-on-raspberry-pi.md)
- [Capacitive Touch Sensors on the Raspberry Pi](https://learn.adafruit.com/capacitive-touch-sensors-on-the-raspberry-pi.md)
- [Monitor Your Home With the Raspberry Pi B+](https://learn.adafruit.com/monitor-your-home-with-the-raspberry-pi-b-plus.md)
- [Drive a 16x2 LCD with the Raspberry Pi](https://learn.adafruit.com/drive-a-16x2-lcd-directly-with-a-raspberry-pi.md)
- [Circadian Pi Desk Light](https://learn.adafruit.com/circadian-pi-desk-light.md)
- [Cloud Cam: Internet-Connected Security Camera](https://learn.adafruit.com/cloud-cam-connected-raspberry-pi-security-camera.md)
