SpotiPi Radio - Part 1 putting it all together

I've got this old radio which looks nice in our living room, but...
It's not a smart device, it can only play these oldschool radio waves (FM/am etc). They work good enough but sometimes I want to listen to my own music, I need my spotify playlists. Thus I've decided to make it smart. Using one of the Raspberry Pi Zero's I got laying around. The problem is these Pi's don't have an audio output. You can use some analog filtering on the PWM outputs to generate sound. The quality of this method is not great (I could say the same about other parts of this project but I also wanted the experience of using i2s). If you still want to try it anyway check out this detailed PDF from Adafruit

The other option is a cheap DAC from china. On aliexpress they sell PCM5102 breakout boards like pictured in the header. They work well, are easy in use and have a 3.5mm output soldered on them. Another little more expensive options is the pHAT DAC which comes as a nice shield or the pHAT BEAT which even has some nice leds on top that can be used as an vu meter. The first thing to do is solder the headers on the breakout. The pins are easy to solder on, so no biggie here. Next thing to do is to wire it all up like in the diagram below. Wiring For a detailed description of all the pins check below.

Pin Functions
Pin name Raspberry pi pin Function
VCCC Pin 2 [ 5V ] Power supply
3.3V NC Input of 3.3V source, also generated internally
GND GND Ground connection
FLT NC Filter Select, selects the audio filter to use 1
DMP NC de-emphasis selector, enables de-emphasis for 44.1khz sample rates 2
SCL NC System clock, internally generated in the pcm5102a
BCK Pin 12 Bit clock
Din Pin 40 Data input
LCK Pin 35 Left Right selector, selects if the channel for the current data
FMT GND Format Select. low=i2s, high=left-justified, since we use i2s keep it low(tied to ground). Keeping it floating will probably cause strange issues because of floating voltages 3
XMT Pin 1 [ 3.3V ] This is the mute select, it's active low, so not connecting this will cause no sound to come out. You can connect it to the 3.3V from the pi, or from the board itself.

Since this old radio uses 5 pin din connectors I soldered one to the output pads of the breakout board. When you are doing this yourself you probably don't need to do this and can use the 3.5mm plug on the breakout.

Now I want this to look semi decently and not be on a breadboard so I fixed it all onto a prototype board. Creating the traces first in Fritzing to check if it's possible without jumper wires. Fritzings auto-trace tool generated this for me: Auto trace

After tidying it up it looked like this, i drew it over on prototype board and started soldering. manual trace pcb

Unfortunately i forgot to add the FMT and XMT connections the first time, both in Fritzing and on the board. So I had to use 2 jumper wires to connect them. It's fixed in the above Fritzing files, but not worth the hassle of re-soldering for me.

Getting sound out of it

Arguably the most important part of this project is getting sound out of the DAC. Now this requires some configuration of the Raspberry Pi

Make the kernel know you are using the pcm5102

First you need to make the driver module load at boot, you can do this by modifying the /boot/config.txt file Add the following line to it dtoverlay=hifiberry-dac, and if you don't want HDMI output anymore comment out the following line: dtparam=audio=on by placing a # in front of it. This will tell the loader to combine this with the base Device Tree and pass a full device tree including this part to the kernel. The actual partial device tree is coming from the HiFi berry dac. This dac uses the same pcm5102 chip, as can be seen in this part of the source code for the overlay: 4

fragment@1 {
        target-path = "/";
        __overlay__ {
                pcm5102a-codec {
                        #sound-dai-cells = <0>;
                        compatible = "ti,pcm5102a";
                        status = "okay";
                };
        };
};

For more information on Device Trees check this article from the rpi documentation & this embeded linux page. Now reboot your pi and everything should be working.

Configuring alsa

Now you need to configure alsa. Alsa is the "advanced linux sound architecutre" it provides kernel modules for using soundcards etc. We need to tell alsa to use the pcm5102 as the default soundcard. Pretty easy to do. Check out the hw id of your sound card by running aplay -l

Now create a file /etc/asound.conf and add the following to it.

pcm.!default {
 type hw card [device_number] 
}
ctl.!default {
 type hw card [device_number] 
}

substituing the device number with the card number found from aplay -l

Testing

To test your setup install mplayer and download a test file. You can do this by connecting your pi to the internet, than install mplayer by running sudo apt install mplayer. Then run mplayer <(curl https://www.audiocheck.net/Audio/audiocheck.net_binaural_knocking.ogg) to download a stereo test file and run it. This will create a knockin sound on the left and then on the right, so you can check your speaker connection at the same time.

Now I have a working sound setup and hopefully you could follow along and build on yourself as well. If you have any questions post them in the comments and I'll try to help you out.

See you in the next article where I'll setup the software side.

Resources