Spotify is such an awesome music service. Raspberry Pi Zero is such an awesome ultra-mini computing device. Obviously, combining the two is something I must do!!! The idea here is to make something that’s stylish, portable, can play Spotify, and hopefully also display visuals such as album art. If you’re interested in following this project, feel free to click on the “Follow” icon just above this paragraph. Click the video below to see an overview, otherwise, scroll down to see how I made this musical masterpiece!
The Pi Zero W is a phenomenal full linux computer that’s basically the size of a stick of gum. And the “W” version has both bluetooth and wireless built in! That opens it up to so many amazing potential projects. One that I have been really wanting to do is a portable streaming Spotify client that displays album art or other visuals. Does it have to be a Pi Zero? Nope. You can use any other Pi or Pi alternative, but I just love the portable size and power consumption of the Pi Zero. The only issue is that the Pi Zero (at least the current iteration at the time of writing this guide) doesn’t support Audio Out, unless it’s through HDMI, but I’ll touch on that a little later on. There are pre-built audio output “hat’s” available, if you want to go that route. But anyway, here’s the basic goals and obstacles I’ll have to overcome for making this project work:
- Add audio output to the Raspberry Pi
- Add an amp/speaker to the Raspberry Pi
- Find a way to stream Spotify as a “headless” client
- Display album art on a small LCD screen.
Setting Up The Pi Zero
Before we do anything else, the first thing is to get the Pi Zero up and running. This is pretty much the standard setup for any Raspberry Pi that you get. Here’s the basic items a Raspberry Pi requires to setup, if you are unfamiliar with the process:
- Pi Zero “W” board
- 4GB or Larger Micro SD Card
- HDMI Adapter
- HDMI Monitor
- USB OTG adapter
- Micro USB power supply
- Mouse & Keyboard
The first step is to load the Raspbian Operating System onto the SD card. The Raspbian OS can be downloaded from here. It downloads as a zip file that contains the Raspbian image file, and the way to transfer that image file to the SD card on Windows is by using the Win32 Disk Imager. Once you launch Win 32 Disk Imager, you can choose the drive letter for your SD card, choose the Raspbian image, and then click “Write” to write the image to the SD card. Then eject the SD card and put it in the Raspberry Pi, along with the monitor, keyboard/mouse, and power.
After the Pi boots up, you can click on the wireless icon in the upper right and choosing your wireless network. Then by right clicking on that same wireless icon and choosing “Wireless & Wired Network Settings”, you can set a static IP address for your Pi. Lastly, you want to click on the menu icon in the upper left and choose Preferences > Raspberry Pi Configuration. Then click on the “Interfaces” tab and make sure SSH is enabled. After that, everything should be setup on the Pi and we can begin the dirty work.
The first biggest hurdle to overcome is the fact that the Pi Zero (at least at the time of this post) doesn’t have audio output. There are a few different ways to get around that.
- Purchase a digital audio extension: Adafruit MAX98357 or Pi Zero “hat” extension
- Make a simple circuit to extract audio from the GPIO pins
- Split out the audio from the HDMI output
If you want to go with option 3 and do the DIY route, you can follow these steps to make your own Audio Output. This method will also require either making your own Amp to use speakers, or you could purchase a simple stereo amp.
However, the method I chose to go with is quite a bit simpler. I decided to go with the Adafruit MAX98357 audio amp.
It serves several purposes: it creates an audio output, serves as a mono-amp, and can be powered through the Pi zero GPIO pins. It’s pretty simple to wire up. You can use the diagram below to connect it to your Pi.
- Amp Vin to Raspbery Pi 5V
- Amp GND to Raspbery Pi GND
- Amp DIN to Raspbery Pi #21
- Amp BCLK to Raspbery Pi #18
- Amp LRCLK to Raspbery Pi #19
Now that it’s all wired up, we have to do a simple software tweak to get it running. Connect to your Pi through SSH and run this script:
curl -sS https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/i2samp.sh | bash
and then reboot the pi
When it comes back up, you can run a test audio file to see if it all works
speaker-test -c2 --test=wav -w /usr/share/sounds/alsa/Front_Center.wav
If it doesn’t work or you have any issues, you can refer to this guide over on the Adafruit website.
Adding the LCD
The key to make my streaming radio stand out and give it a unique touch is to have a screen that displays the album art of the current song that is playing. Obviously this is going to require an LCD. I’m going with a 1.8″ TFT LCD screen.
This screen as well can be powered through the Raspberry Pi GPIO pins. So now the Pi, the amp, and the LCD screen can all be powered through a single source and can be self contained. Here’s a diagram of how to wire the LCD to the Pi Zero
To get the LCD working, you need to run a few command line scripts. So again, open up an SSH terminal and type these commands:
(go to Interface options and enable SPI)
sudo modprobe bcm2835-v4l2 sudo modprobe fbtft_device name=adafruit18
then you can download an image and push it to the LCD screen
wget http://static.musictoday.com/store/bands/1346/product_medium/BGCDMS02.JPG sudo fbi -T 2 -d /dev/fb1 -noverbose -a BGCDMS02.jpg
Installing the Spotify Software
Now it’s time to add the magic smoke! Since the goal here is to have a radio that streams Spotify, we’ll need to find some software that connects to it and plays whatever song we choose. Another requirement is that we essentially want this to run in “headless” mode, which means we want the software to run automatically and startup with no user intervention.
At first, the logical solution would be to use the Spotify web API. This would allow us to write a program that can get song information, get playlists, play songs, etc. The biggest issue is, however, that the song tracks are limited to 30 seconds, so that essentially takes it off the list as a viable candidate.
After doing more research, I stumbled across Mopidy, which is a python based music playing application for the Raspberry Pi. It can play music from several sources, including locally, Google Music, Soundcloud, and of course Spotify. The only catch, however, is that it requries a Spotify Premium account.
Setting up Mopidy with the Spotify client on the raspberry Pi is super simple. Just run the command below:
pip install Mopidy-Spotify
Now we’ll need to edit some of the configuration settings to make sure it connects and plays correctly.
Scroll down to the [spotify] section and enter in your username and password.
[spotify] enabled = true username = your_username password = your_password
Also, check the [mpd] section to make sure it’s enabled and set to the correct host and port names, like below.
[mpd] enabled = true hostname = 0.0.0.0 port = 6600 password =
Then type CTRL+X to exit the nano editor, and Y to save the changes. Then reboot your Pi with
After the Pi comes back up, and you can establish another SSH session, you can start Mopidy simply by typing
It will start up the mopidy server so that you can connect to it….but how do you connect to it?
Mopidy is also a Music Player Daemon (MPD) server. And MPD has many different clients across many different platforms. So you can download an MPD client on Windows, Mac, Linux, iOS, and Andriod and use that to control your music in Mopidy. You can use whichever one works best for you.
So with the Mopidy server running on your Pi, you should be able to enter the Pi’s IP address (that we set earlier) into the settings of the MPD software to connect to it. Then you can remotely control what’s playing on your Pi.
Creating The Cover Art Software
Now that we have the music playing, let’s see if we can make something that detects the current song playing and pulls the Cover Art for that song from the web and displays it on our little LCD screen.
Let’s start by figuring out how to know what song is currently playing on MPD. Searching the internet, I found python-mpd2 a simple python client that can extract information from an MPD server (such as artist, album, playtime, etc.)
pip install python-mpd2
Also, we will need to install the python “urllib” so that we can retrieve images from a URL.
pip install urllib2
Now, let’s create a new python file called “cover_art.py”
And in it we can paste this code
MORE STEPS COMING SOON! CLICK THE “FOLLOW” BUTTON AT THE TOP OF THE PAGE TO GET NOTIFICATIONS!