logo-mobile

ROHM

ROHM
Menu
  • Arduino –
  • Raspberry Pi –
  • Trending –
  • Others –
  • About –
  • Contact –

Raspberry Pi

Get better sound from your Raspberry Pi by replacing its analogue audio output

DevicePlus Editorial Team
Published by DevicePlus Editorial Team at May 17, 2021
Categories
  • Raspberry Pi
Tags
Get better sound from your Raspberry Pi by replacing its analogue audio output

Raspberry Pi

Replacing the Analogue Audio Output on the Raspberry Pi

 

The analogue audio output on the Raspberry Pi gets a bad rap, although I reckon it’s just fine for many things.

Raspberry Pi

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.

Why Does This Analogue Audio Output Sound so Average?

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.

Building Our Own Analogue Audio Output

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.

What about headphones?

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.

Gathering Bits and Pieces

For this, you’ll need:

A Raspberry Pi and some way of typing into it Raspberry Pi
A solderless breadboard breadboard
A GPIO breakout board (optional, but recommended) GPIO breakout board
A PIR motion sensor

(I’m using an XC-4444)

PIR motion sensor
Stereo 3.5mm socket

(or 6.35mm if you prefer)

Stereo 3.5mm socket
Adafruit I2S Stereo Decoder Adafruit I2S Stereo Decoder
10k dual gang logarithmic potentiometer 10k dual gang logarithmic potentiometer
220uF electrolytic capacitor 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.

Preparing Components for Breadboard

A couple of these components need wires and pins attached to easily connect to the breadboard.

Headphone Socket

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.

I2S Stereo Decoder

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.

Attaching the DAC

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

Guiding Image #1

Connecting the Pins

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

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

Guiding Image #3

Installing the Software

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.

Building a Simple Headphone Output

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.

Wiring the Stereo Potentiometer

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!

Improving the Headphone Output

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:

  • Use a different product instead of the I2S Stereo Decoder that offers a headphone output rather than a line-out. If you don’t need a line-out, then this is probably the easiest choice.
  • Connect a stereo amplifier board or module. You can even power speakers this way.
  • Build your own headphone amplifier! Check out the Cmoy pocket amplifier.

As always, the right solution will depend on what you are building. Have fun exploring the exciting world of analogue audio!

 

DevicePlus Editorial Team
DevicePlus Editorial Team

Check us out on Social Media

  • Facebook
  • Twitter

Recommended Posts

  • Setting Up Raspberry Pi as a Home Media ServerSetting Up Raspberry Pi as a Home Media Server
  • An Introduction to Raspberry Pi GPIO PinsAn Introduction to Raspberry Pi GPIO Pins
  • Raspberry Radio Part 2 – Using MPD and a smartphone app to turn your Pi into an easy to use radio!Raspberry Radio Part 2 – Using MPD and a smartphone app to turn your Pi into an easy to use radio!
  • Raspberry Radio Part 1 – Internet Radio Receiver and Remote AccessRaspberry Radio Part 1 – Internet Radio Receiver and Remote Access
  • Introduction to Raspberry Pi 4: Mastering the Use of the Raspberry Pi 4 in Headless Mode!Introduction to Raspberry Pi 4: Mastering the Use of the Raspberry Pi 4 in Headless Mode!
  • Building a Wavetable Synthesizer with a Raspberry Pi and an Arduino UnoBuilding a Wavetable Synthesizer with a Raspberry Pi and an Arduino Uno
Receive update on new postsPrivacy Policy

Recommended Tutorials

  • How to integrate an RFID module with Raspberry Pi How to integrate an RFID module with Raspberry Pi
  • How to Use the NRF24l01+ Module with Arduino How to Use the NRF24l01+ Module with Arduino
  • How to Run Arduino Sketches on Raspberry Pi How to Run Arduino Sketches on Raspberry Pi
  • Setting Up Raspberry Pi as a Home Media Server Setting Up Raspberry Pi as a Home Media Server

Recommended Trends

  • SewBot Is Revolutionizing the Clothing Manufacturing Industry SewBot Is Revolutionizing the Clothing Manufacturing Industry
  • All About The Sumo Robot Competition And Technology All About The Sumo Robot Competition And Technology
  • 5 Interesting Tips to Calculating the Forward Kinematics of a Robot 5 Interesting Tips to Calculating the Forward Kinematics of a Robot
  • Go Inside the Drones That Are Changing Food Delivery Go Inside the Drones That Are Changing Food Delivery
Menu
  • Arduino –
    Arduino Beginner’s Guide
  • Raspberry Pi –
    Raspberry Pi Beginner's Guide
  • Trending –
    Updates on New Technologies
  • Others –
    Interviews / Events / Others

Check us out on Social Media

  • Facebook
  • Twitter
  • About
  • Company
  • Privacy Policy
  • Terms of Service
  • Contact
  • Japanese
  • 简体中文
  • 繁體中文
Don’t Forget to Follow Us!
© Copyright 2016-2023. Device Plus - Powered by ROHM
© 2023 Device Plus. All Rights Reserved. Muffin group

istanbul escort istanbul escort istanbul escort