Get ready to make your very own music box! In this project, we’ll learn how to hook up some buttons to the GPIO pins of your Raspberry Pi and then use them with a Python application in order to create your own music player.
Here at Vilros we all love jamming out to music. So, we’re excited about this challenge to create your own project which ultimately can play all sorts of different sounds. Get your disco ball ready!
For this project, you’re going to want to use a bunch of sample sounds. Even though there are many sound files on Raspbian, they can be tricky to play when you’re using Python.
So, the first thing you want to do is convert the sound files to a file format that’s more compatible with Python. Just follow these steps:
If you’ve done it correctly, you should now see multiple “.flac” files in your sample directory.
Python works better with .wav files, so your next step is to convert all your .flac files to .wav files. Ideally, you should be able to do this in one big batch step.
In order, to batch process all your files, you’ll want to use the “libav-tools” software.
For each file with a .flac ending, you can convert it using “avconv”. Just keep the original name of the file, but change .flac to .wav.
The next step of this project is to write the code for Python. Though you can technically use any text editor, we recommend using Python’s Integrated Development Environment (IDLE) – just make sure you use the version for Python 3!
For this music box project, you’re going to want to start by using 4 different sounds. To do this, first you will need to import and initialize the module called “pygame”. Next, you’ll create a “sound” object and connect it with the path to your new .wav files.
(Note: You can save your Python file to your gpio-music-box directory by clicking “save” under “file”).
Your next step will be to connect your 4 buttons to your Raspberry Pi.
First, you’ll want to place each of the 4 buttons into the breadboard. Then, using the jumper leads, connect each button to a different GPIO pin on your Raspberry Pi.
(Note: so that you don’t connect too many jumper leads to GND pins, try wiring the negative rail on your breadboard to a single GND pin, which should then make it so that all of your buttons will use the same ground rail.)
Now, you want to use each button to call a function that takes no arguments. You want to create a trigger that calls the function, so that each time you press a button, you get the desired sound effect.
Because you set up functions in the pygame module, you should now be able to use them with the “when_pressed” method.
Congratulations, you should now have a working music box that plays sounds you can rock out to with your friends!
If you want to take this project to the next level, consider working on making your code a bit cleaner. And/or, you might want to add more buttons for more sound options, or LED lights to create a fun disco effect!
To find out more about this project, including code and help tools, just check out this step-by-step guide.
Our team here at Vilros hopes you love dancing along with your new music box. If you liked this project, be sure to check out more project ideas on our Vilros Projects blog!
Comments will be approved before showing up.
GET IN TOUCH