In this tutorial I will show how to create your very own Raspberry Pi based speaker. No fancy thing, just a Raspberry Pi extended with an amplifier and a small speaker.
The first thing I dealt with was to get the soldering done. The amplifier does not come pre soldered and there for I had to handle the soldering myself.
If you are unsure how to solder take a look at this instruction video. The video shows how to solder a Raspberry Pi Zero which is quite a lot more complicated but it is the same concept.
Now when the soldering was done it was time to fit all the pieces together. There were however a few questions still remained that needed to have answers. How do I to connect the amplifier to the Raspberry Pi and what goes where? To know that I looked at the Raspberry Pi pin layout, it looks like this.
I also looked at the pin layout for the amplifier and came up with following mapping between the amplifier and the Raspberry Pi. (Please notice that you might have different mappings due to having a different version of the Raspberry Pi)
- 5V Raspberry -> Vin
- GND Raspberry -> GND
- PIN18 Raspberry -> BCLK
- PIN19 Raspberry -> LRC
- PIN21 Raspberry -> DIN
When everything was connected I powered the Raspberry Pi up and signed in to the Raspberry Pi using the SSH command tool.
The next chapter requires to have a Raspberry Pi setup with Raspbian and if do not have done this yet, look at this tutorial.
There are quite a few things that needs to be setup for the speaker and the amplifier. Luckily I am not the first one doing this, there are scripts that one can use to make this step a lot quicker.
Are you interested in learning exactly what the script does please look at this link otherwise you can just run the following command and not worry about the exact commands used. (The script is taked from https://learn.adafruit.com/adafruit-max98357-i2s-class-d-mono-amp/raspberry-pi-usage)
# curl -sS https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/i2samp.sh | bash
Test speaker connections
It was time to check that my soldering and the wires were connected properly. An easy way to check this is using utility tool “spearker-test” and if everything is ok you should hear a voice “front”, “center”.
# speaker-test -c2 --test=wav -w /usr/share/sounds/alsa/Front_Center.wav
I looked at different options for how to play music on a Raspberry Pi and found that Omxplayer was the best option. The reason for this is that it has a clear command line interface which is easy to understand and well used. You can find more information about Omxplayer here.
The reason I do not think Spotify is a valid option for this solution is that Spotify Connect requires one to have the phone or computer nearby. I want the Raspberry Pi to play music even though it is offline and neither my phone or my computer was nearby. My solution is therefor to download music to the Raspberry Pi and play it locally using Omxplayer.
I installed Omxplayer on the raspberry Pi using the “apt-get install” utility.
# sudo apt-get install omxplayer
I programmed a simple Python program to make the Raspberry Pi play music on repeat in all eternal. It looks like this. If you want to use this script make sure you have the music files “song1.mp3”, “song2.mp3” and “song3.mp3” locally on the Raspberry Pi and in the same folder as the script.
from time import sleep import random import subprocess import os class NextSong : songs = ["song1", "song2", "song3"] index = 0 def __init__(self) : self.index = random.randint(0, len(self.songs) - 1) def getNextSong(self): nextSong = self.songs[self.index] self.index = self.index + 1 if self.index >= len(self.songs) : self.index = 0 return nextSong nextSong = NextSong() while True: song = "omxplayer -o alsa --vol -2000 " + nextSong.getNextSong() + ".mp3" os.system(song)
With as little as a speaker and an amplifier you can extend a Raspberry Pi to play anything you want. As always it is your imagination which creates your limits.
Are you interested in using your new speaker with Spotify Connect, check out this tutorial. https://learnraspberrypi.com/2019/08/09/spotify-connect-for-raspberry-pi/