Use PyGame Library to Play Game Sounds with Raspberry Pi

In this session we are going to use Raspberry Pi and the PYGAME functions to make a sound board. In simple terms, we are going to connect few buttons to the Raspberry Pi GPIO pins and when these buttons are pressed Raspberry Pi plays audio files stored in its memory. These audio files can be played one by one or they can all be played together. In other words you can press one or multiple buttons at the same time, Raspberry Pi will play one or multiple audio files accordingly at the same time. Check the Demo Video at the end of this article. Also check our Raspberry Pi Tutorial Series along with some good IoT Projects.

use pygame library to play game sounds with raspberry piWe have 26 GPIO pins in Raspberry Pi which can be programmed, out of which some are used to perform some special functions and then we have 17 GPIO remaining. Each GPIO pin can deliver or draw a maximum of 15mA. And the sum of currents from all GPIO cannot exceed 50mA. So we can draw a maximum of 3mA in average from each of these GPIO pins. We will use resistors to limit the current flow. Learn more about GPIO Pins and interfacing button with Raspberry Pi here.

Components Required:

Here we are using Raspberry Pi 2 Model B with Raspbian Jessie OS. All the basic Hardware and Software requirements are previously discussed, you can look it up in the Raspberry Pi Introduction and Raspberry PI LED Blinking for getting started, other than that we need:

  • Raspberry Pi with pre-installed OS
  • Power supply
  • Speaker
  • 1KΩ resistor (6 pieces)
  • Push Buttons (6 pieces)
  • 1000uF capacitor

Working Explanation:

Here we are Playing Sound using Buttons with Raspberry Pi. We have used 6 push buttons to play 6 audio files. We can add more buttons and audio files to extend this board to create more beautiful pattern by pressing these buttons. Before explaining any further, complete the steps below.

1. First of all download the 6 Audio files from the link given below or you can use your audio files, but then you need to change the file names in Code.

Download Audio files from here

2. Create a new folder on Raspberry Pi desktop screen and name it as “PI SOUND BOARD”.

3. Unzip the downloaded audio files into the folder which we have created on DESKTOP in previous step.

4. Open the terminal window in Raspberry Pi and enter below command:

sudo amixer cset numid=3 1  <press enter>

This command tells PI to provide audio output through 3.5mm audio jack on board.

If you want audio output from HDMI Port then you can use below command:

$ sudo amixer cset numid=3 2 <press enter>

5. Connect speakers to the 3.5mm audio output jack on the Raspberry Pi board.

6. Create a PYTHON file (*.py extension) and save it in the same folder. Check this tutorial for creating and running the Python Program in Raspberry Pi.

7. Pygame mixer will be installed by default in the OS. If the program, after execution, does not recall PYMIXER, then update the OS of Raspberry Pi by entering below command in the terminal window. Make sure that Pi is connected to internet.

sudo apt-get update <press enter>

Wait for few minutes for the OS to update.

Now connect every component as per the circuit diagram given below, Copy the PYHTON program into the PYHTON file created on the desktop and finally hit run to play the audio files through the buttons. Python Program is given at the end with the Demo Video.

Circuit Diagram:schematic use pygame library to play game sounds with raspberry pi

For more detail: Use PyGame Library to Play Game Sounds with Raspberry Pi

Scroll to Top
Scroll to Top
Read previous post:
Raspberry Pi Alarm Clock using RTC Module DS1307

In this session we are going to Interface Real Time Clock Module DS1307 with Raspberry PI to make an Alarm...