The analogue audio output on the Raspberry Pi gets a bad rap, although I reckon it’s just fine for many things.
Raspberry Pi
If you want to produce an error or warning tone, or if you’re building a toy that says something or makes a sound effect, or even a communication device, it’s generally good enough.
When you plug in a sweet pair of headphones – or connect it to full range monitors – you begin to notice this is a $35 device that is not primarily intended for high quality analogue audio. It works, but it lacks detail, sounds squashed and you sometimes hear weird artifacts.
Out of the box, the easiest way around this is to use the HDMI output for your audio instead. That’s great if you are plugging it into a home theatre setup with high quality digital-analogue conversion.
But, what if you’re building a portable device that’s meant to deliver an immersive media or gaming experience? This is where it’s nice to have better audio and the Pi just can’t quite provide.
The other main reason you might want to replace the audio output is that the Raspberry Pi’s headphone jack uses the PWM hardware for analogue audio (more on this below). If you want to use this for something else, you’re best off moving the analogue elsewhere.
Or perhaps you don’t truly need to build your own analogue output at all, it’s just an excuse to start exploring the fascinating world of audio. You know what? That’s cool too.
It’s actually quite an achievement that it doesn’t sound far, far worse.
A Raspberry Pi is a digital device. It’s built to send and receive streams of 1s and 0s with voltages that are either all the way on or all the way off.
But headphones receive an analogue signal: a voltage that’s neither all the way on nor all the way off, but constantly varying in the same shape of the sound waves that the headphones produce.
The Raspberry Pi can go some way toward simulating an analogue signal using Pulse Width Modulation, or PWM. This simulates a voltage level somewhere in the middle by flickering on and off really fast – 19.2 million times per second.
This PWM signal then runs through a low pass filter which smooths it out into a real analogue signal.
The problem with this is that – by design – a low pass filter removes a lot of the high frequency content. The one in the Raspberry Pi starts rolling frequencies off somewhere before 7kHz. This cuts out a lot of the air and harmonic detail.
The other trade-off is that, to get this much frequency range, the dynamic range is reduced to 11 bits. That’s 32 times less depth than the 16-bit audio of a CD. Again, what’s missing is detail. The loud bits are still loud and the quiet bits still quiet, but more subtle variations are lost.
We’ll use an off-the-shelf DAC board from Adafruit: the I2S Stereo Decoder. Out of the box, it’s nearly ready to go. We just need to solder the header pins, wire it to the GPIO pins and install a driver.
I2S is short for Inter-Integrated Circuit Sound. This sounds so similar to I2C, or Inter-Integrated Circuit, that you might easily confuse them.
They’re really quite different though. I2C is a protocol that allows low bandwidth communication in both directions to hundreds of slaved components, while I2S is a high bandwidth protocol for digital audio.
This I2S Stereo Decoder comes with a built-in 3.5mm stereo socket. If you need a different output, you can either use an adapter, or wire your own connector to the pins.
This line out is loud enough to drive most headphones at a good listening volume. If you’re using basic earbuds, it may even be too loud.
Unfortunately, there’s no volume control on the built-in 3.5mm socket, so let’s connect our own one to the pins, with a simple potentiometer as a volume control.
For this, you’ll need:
A Raspberry Pi and some way of typing into it | ![]() |
A solderless breadboard | ![]() |
A GPIO breakout board (optional, but recommended) | ![]() |
A PIR motion sensor
(I’m using an XC-4444) |
![]() |
Stereo 3.5mm socket
(or 6.35mm if you prefer) |
![]() |
Adafruit I2S Stereo Decoder | ![]() |
10k dual gang logarithmic potentiometer | ![]() |
220uF electrolytic capacitor | ![]() |
You’ll also need a few more generic pieces of equipment, such as a power supply, some jumper cables, wire cutters, plus a solder and soldering iron.
The Adafruit I2S Stereo Decoder will be the most difficult thing to find, though it’s widely available online. Everything else should be readily available at a local electronics shop.
This has been tested to work with the official power supply from the Raspberry Pi Foundation, which has 120mV of ripple and noise. If your power supply is noisier, you may want to tinker with the filter capacitor.
A couple of these components need wires and pins attached to easily connect to the breadboard.
You might get away with an unsoldered connection, but it will be a lot more fiddly and annoying. Besides, if you’re a bit shy about soldering, this is ideal to do first.
Take three jumper cables and clip one of the ends off, making sure that the other end has a male connector so that it can plug into the breadboard. Use the wire cutters to strip away some of the insulation.
Wrap the uninsulated wire around the contacts and then solder them.
This board ships with a pin header which you will need to snap into 6 and 9 pin pieces and then solder on.
The easiest way to do this is to place the pins into the breadboard and use it as a stand. Be sure to get the solder joints hot enough that the solder fuses to the connectors and creates a circuit.
This is based on the excellent and very thorough documentation Adafruit has provided for using the I2S Stereo Decoder with a Raspberry Pi. If you want to explore the full capabilities of this board, please have a read through it.
Place the GPIO breakout board on the left of the breadboard and then the I2S Stereo Decoder immediately next to it, with the 3.5mm socket facing to the right. All the pins we’ll connect to are across the bottom, so be sure to leave space here for your jumper wires.
Guiding Image #1
Connect the 3.3v power pin to the positive power rail on the bottom of the breadboard, and the ground pin to the negative rail.
The cleaner this power, the less noisy our audio will be. We can filter the power somewhat by placing one of our 220uF electrolytic capacitors across the power rails. Electrolytic capacitors need to be placed the right way around, or they can explode! The longer leg connects to positive.
Then connect the power rails to the power and ground pins of the I2S Stereo Decoder.
Guiding Image #2
Now we’re ready to connect the signal pins. Connect the word selector pin to GPIO pin #19, the digital input to GPIO pin #21, and the bit clock to GPIO pin #18.
Guiding Image #3
Now that the DAC is connected, we need to configure our Raspberry Pi to start using it. Happily, Adafruit provides a shell script that automates this for us.
Run this script from the terminal by typing:
1 |
curl -sS https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/i2samp.sh | bash |
The first thing you’ll read here is a warning to be careful about running scripts from just any source on the internet. This is in fact great sense.
This script comes from Adafruit’s github account, so it should be fairly trustworthy; it’s up to you to decide your tolerance for risk here.
You’ll then be asked whether you want the Raspberry Pi to run a background routine to prevent popping and clicking when the DAC is enabled and disabled. If you are unsure what to choose here, select “yes”.
When you’re asked to reboot the Raspberry Pi, select “yes” and then run the script again.
You’ll now be asked to test the audio. The easiest way to do this is to plug some powered speakers or some headphones into the 3.5mm stereo socket on the I2S Stereo Decoder.
You should hear the words “front left” and “front right” on each side, loud and clear.
If you can’t hear anything, check your solder joints and that everything is wired correctly and then try again.
If you’re happy with how this sounds, you can stop here.
If you plug in earbuds, it may be too loud. It may even overload some consumer amplifiers. Let’s add a volume pot to tame it.
A stereo potentiometer is really two mono potentiometers controlled by the same handle, for ease of use. This gives us one for the left audio channel and the other for the right audio channel.
Connect one of side terminals to the audio pin and the other side terminal to ground. The I2S Stereo Decoder offers an “audio ground” pin which you can use – it offers a little bit more filtering.
The middle terminals of the potentiometer then connect to the left and right contacts of the 3.5mm socket we soldered earlier.
Connect the ground contact on the socket to ground. You now have volume control!
This is hardly an ideal headphone output, though it’s a good way to start playing and it will work.
Typical consumer headphones tend to have low impedance, far lower than the amplifier it’s built to connect to. This output is current-limited, so you won’t damage anything, but it will sound distorted.
Audiophile headphones tend to have a higher impedance and will distort less. With an impedance over 150 ohms or so, they will also need much more power to drive them to an enjoyable volume.
For a good headphone output, you will need something more sophisticated. You have a few different options here:
As always, the right solution will depend on what you are building. Have fun exploring the exciting world of analogue audio!