Raspberry Pi £3 High Quality Audio – PCM5102A
The quality of the on-board audio on the Raspberry Pi is mediocre. Adding a £3 board and connecting this to the I2S bus on the Pi can deliver CD quality sound. The board used here has the PCM5102A chip, as used by the HiFiBerry DAC and pHAT – but at a fraction of the cost. This project includes setting up the board as the default audio output, adding a software volume control if required, and a low cost 3+3W audio amplifier working from the same 5v supply. Raspbian includes alsa (Advanced Linux Sound Architecture) that can be set to drive the new card and provides the option for the software volume control.
You will need:
- PCM5102A module (ebay/AliExpress)
- Raspberry Pi
- PAM8403 amplifier
For reference the PCM5102 board looks like:
Hardware Setup
The I2S pins are defined by a dtoverlay setting later. The connections required are(listed in PCB order with pins for the Pi A+/B+, 2, 3, Z):
Board | Connect to |   Pi | Function |
VCC | 5v | pin 2 or 4 | Power |
3.3V | XMT | Generated on-board | |
GND | GND | pin 6 or 14 | Ground |
FLT | GND | Filter select (normal low) | |
DMP | GND | De-emphasis (off low) | |
SCL | GND | System clock – internally generated | |
BCK | GPIO18 | pin 12 | data bit clock |
DIN | GPIO21 | pin 40 | data |
LCK | GPIO19 | pin 35 | data word clock |
FMT | GND | Audio format (low I2S) | |
XMT | 3.3v | Software mute (high off) |
So 5 leads are required from the board to the Pi and 5 other pins have to be connected to ground, and one pin connected to the on-board 3.3v connection. I debated whether SCL needed to be connected to ground. However it is an input pin and should not be left floating and connecting to ground would only pose a problem if it was an output as well - which it is not. Noise on this pin would prevent the internal clock starting.
I made the board ground and 3.3v connections using fine enamel coated wire (28 SWG).
For the leads to the Pi I cut some Dupont leads in half and soldered the cut end to the board.
Board connected to Pi:
Software setup
First we need to disable the onboard sound by editing alsa-blacklist.conf:
sudo nano /etc/modprobe.d/alsa-blacklist.conf
add:
blacklist snd_bcm2835
Save and exit (^X, Y, enter).
Now, to set the IO, edit config.txt:
sudo nano /boot/config.txt
Add:
dtoverlay=hifiberry-dac
Remove or comment (#) the line:
dtparam=audio=on
Leave the line:
#dtparam=i2s=on
commented out.
Save and exit (^X, Y, enter).
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).
It is no harm to do a reboot. So:
sudo reboot
Test if everything is OK:
aplay -l
This should return:
**** List of PLAYBACK Hardware Devices **** card 0: sndrpihifiberry [snd_rpi_hifiberry_dac], device 0: HifiBerry DAC HiFi pcm5102a-hifi-0 [] Subdevices: 1/1 Subdevice #0: subdevice #0
If you have an amplifier you can connect via a phono lead you can now test it via: speaker-test -D<device name> -c<channel count> -twav So:
speaker-test -D default -c 2 -twav
This is a continuous test saying ‘front left’ and ‘front right’ alternately from the appropriate speakers.
Enter ^C to stop.
There are several players that can be used to try the card out. In my case I wanted to use MPD as it can be driven from Node-RED.
To get MPD (and MPC) working:
sudo apt-get -y update
sudo apt-get -y upgrade
sudo apt-get -y install mpd mpc
Note: mpc is a client that can drive mpd and is useful for testing.
Adding software volume control
This is an optional step that adds a software volume control into the sound processing sequence. The key ingredient, that took a frustrating day to find, is that MPD needs the soft volume control to be called PCM.
sudo nano /etc/asound.conf
add the following:
pcm.sftvol { type softvol slave.pcm "plughw:0" control { name "PCM" card 0 } }
Enter or change the pcm.!default section to:
pcm.!default { type plug slave.pcm "sftvol" }
Save and exit (^X, Y, enter).
Now we can test it, as before:
speaker-test -D default -c 2 -twav
This should give alternating "front right" and "front left" from the respective speakers.
5v Audio Amplifier
I wanted to include an amplifier on my system and 3+3W would be enough power. This can be delivered from 5v into 4 ohm speakers. I had some drivers from a faulty JBL dock but also tried some (under £2) 2” speakers from ebay. The amplifier module is available on ebay/aliexpress for under £1.
A little bit of thought is needed to avoid ground loops when both the audio and digital sections are driven from the same supply, especially when the audio devices use signals referenced to ground. I used a 2.5mm DC power socket for this system. This works with the 5v 4a power supply I sourced (so I could power a 10" touchscreen as well). I ran ground and 5v from the socket to the Pi using 20awg wires to a micro USB plug. I then ran ground and 5v from the socket to the amplifier module - also using 20awg wire. For the sound board I wired the ground and L and R wires to the amplifier so it has the same ground. I would have wired the sound board 5v to the amplifier as well but lack of a spare connection made it easier to connect this to the 2.5mm socket. This arangement keeps the audio signals separate from the digital. There will be a small amount of noise between the Pi ground and the audio ground. This is at a level that will not present problems the digial signals between the two.
Photos and video to be added.
]