Raspberry Pi HQ Audio, PCM5102 and MPD

by FlyingDrMike in Circuits > Raspberry Pi

418 Views, 4 Favorites, 0 Comments

Raspberry Pi HQ Audio, PCM5102 and MPD

title.jpg

This instructable details setting up a Raspberry Pi for high quality audio using a PCM5102 DAC (digital audio converter) driven by MPD (Music Player Daemon) in headless mode. It is directed at beginners but will also speed installation for those with more experience.

It uses the 2025 Raspberry Pi OS 'Bookworm' that has required several additional steps over that needed for earlier OS versions.

It took a while to get this working. I could not find a good guide. Hence I hope this will fill the gap.

The setup here can be built upon using Node-RED as in my earlier instructables: High Quality Music player and Internet Radio and Internet Radio, radio recorder and MP3 Player with Smartphone Control

Supplies

You will need:

  1. Raspberry Pi and micro SD card
  2. PCM5102A module (ebay/AliExpress)
  3. 5v power supply
  4. USB to Ethernet lead (or WiFi dongle) if no Wifi on Pi
  5. Speaker system for testing audio
  6. PC for programming


This should work with any current Raspberry Pi. A Pi Zero v1.3 is used here. A 16Gb SD card should give reasonable space for music. Use larger if your collection needs it.

The PCM5102 module is shown as below. It has a 3.5mm jack socket for audio out.

A suitable USB to ethernet dongle is this one.

Available from e.g. https://thepihut.com/products/ethernet-hub-and-usb-hub-w-micro-usb-otg-connector

Configure Hardware

The PCM5102A module needs 4 solder bridges on the underside and one on the top. Sometimes the bottom ones are pre-soldered.

The I2S pins are defined by a dtoverlay setting later. 5 leads are required from the board - 2 power and 3 data/clocks.


BoardConnect to   PiFunction
SCKNC-internal via link
BCKGPIO18pin 12data bit clock
DINGPIO21pin 40data
LCKGPIO19pin 35data word clock
GNDGNDpin 6 or 14Ground
VCC5vpin 2 or 4Power


Connected up ready for power, ethernet and audio out.

Load Operating System to SD Card

Raspberry Pi imager is the best option to setup the SD card. This does three important things in addition to loading the operating system. First it sets username and password. The earlier default 'pi' and 'raspberry' have been dropped as an obvious security risk. These details are encrypted so it is much easier to set via the imager that sorts this. It also can set WiFi details, and lastly will enable SSH so this does not have to be done separately. Download from: https://www.raspberrypi.com/software/


Open Imager and enter the Pi model, the operating system (Raspberry Pi OS (other)>Pi OS Lite (32-bit)) and the SD card.


Then click ‘NEXT’ and select ‘EDIT SETTINGS’ to get the OS Customisation page. Enter hostname, username and password, WiFi details if required, and locale settings.


Next click the ‘SERVICES’ tab and Enable SSH and Use password authentication. The click ‘SAVE’ and then ‘YES’ to apply OS customisation settings. Then click ‘YES’ to continue to the Warning page.


The write will then start and progress bar will appear. The write will then be verified and confirmed OK and the card will be ready to remove.


Next connect the Pi to an Ethernet USB hub or Wifi dongle and to an amplifier, and plug in the SD card.

Now power up and wait a minute or so to load. Then start Putty on your PC (free from https://www.putty.org/). To connect enter the host name (entered as part of the sd card setup) or the IP address (find by logging into your router). Port should be 22 and SSH selected.


Then log in and all is ready for updating and adding firmware.


The following instructions can be pasted into Putty. Copy text and right click in Putty to paste. This saves a load of time.


First update and upgrade:

sudo apt-get -y update


when complete:

sudo apt-get -y upgrade


The latter takes a while (10-20mins) and sometimes fails due to not getting a connection. If it fails just enter the instruction again. All done:


We now need to edit some files to sort where the audio will go. First disable the (unsatisfactory) on-board sound:

sudo nano /etc/modprobe.d/alsa-blacklist.conf


This opens a text editor (nano). The file will be blank. Add:

blacklist snd_bcm2835


Then enter ^X followed by shift+Y, enter to save.


Next set the Pi to use the DAC:

sudo nano /boot/firmware/config.txt


comment out dtparam=audio=on (# in col 0)


and add:

dtoverlay=hifiberry-dac

at the end of the file.


^X, Y, enter to save.


Next add some alsa (sound) configuration:

sudo nano /etc/asound.conf

Paste the text below:

pcm.!default {
type hw
card 0
}

ctl.!default {
type hw
card 0
}


Save and exit (^X, Y, enter).


Now reboot. So:

sudo reboot


Leave putty open. When the Pi has restarted right click the task bar and select Restart Session. Log on again.


Now we can see if the DAC is connected:

aplay -l


Should give:


Now we can test the sound using:

speaker-test -D default -c 2 -twav


The speakers should be saying Front Left, Front right alternately. Stop with ^C.

Sound hardware is now setup and working.

Load MPD

The last stage is installing the MPD music player (Music Player Daemon) and setting up some music and playlist files.


MPD is controlled by a program called MPC so both have to be installed:

sudo apt-get -y install mpd mpc


Next we need to add Music and playlists directories. This is done most easily using an ftp program that is also to easiest way to transfer music files. FileZilla is another great free open source program that will do this. Download from href=https://filezilla-project.org/download.php?platform=win64


Start FileZilla and enter the Pi IP address, username, password and 22 for the port and click Quickconnect. The Pi directory is on the right hand side. Right click in the lower box and select Create directory, then Music. Then click on Music to enter it and right click again to add a playlists directory.


Then add a .config directory and mpd directory within it. This is for additional files mpd requires.

While we are here we might as well add some music. So add a directory in the Music folder with the album name (in my case Elegy). Open the new folder and drag and drop the music files from your PC.

The file structure should look something like:


Now we have to configure MPD. This needs more editing than under Buster.

sudo nano /etc/mpd.conf


Change the music and playlist directories as below. ~ is a shortcut for the users directory.


Next change the user to the Pi’s username (mike in this case).


Now move the location of the other required files to .config/mpd/.....


Lastly go to the Audio output section and uncomment the alsa section (excepting the optional) and note the }:


^X, Y, enter to save.


Reboot to read new settings.

sudo reboot


Log on again. Get MPC to read the music files

mpc update


Now clear the list held and list the music for a playlist

mpc clear

mpc ls Elegy | mpc add


Save the playlist for the album.

mpc save Elegy


now clear with

mpc clear


and load the playlist with

mpc load Elegy


and play with

mpc play


At this point we have a music system working from the command line.

Further Thoughts

The system now needs a user interface. A great approach is to use Node-RED. There is an MPD plug in to enable the programs to communicate. Node-RED can provide a virtual interface viewable on a smartphone or PC/laptop or anything that has access to the local network. I have two instructables that should give good inspiration. High Quality Music player and Internet Radio and Internet Radio, radio recorder and MP3 Player with Smartphone Control

If you want volume and tone controls a PT2314 module is worth considering. However the modules I bought (ebay/AliExpress) used the earlier chip (no revision letter) that has a weak pull-down on the I2C bus that does not work with the 1k8 pullup resistors used on the Pi. So either increase the Pi resistors (very careful soldering!) or swap the chip. I did the latter and the PT2314E chip is fine, as is the PT2314A version used in the JBL On-Time. Hack into one of those and you have software volume and tone controls plus amplifier!