Raspberry pi analogue output

3.5: Analog video output

The Raspberry models B have a 3.5mm jack that looks like a regular headphone jack but is actually a 4-pins audio and video jack. When configured correctly, you can use this to connect to an older tv without needing HDMI converter boxes. This analog video signal is also known as composite or s-video.

Here follow the steps to get this working. You’ll need a 4-pins a/v cable, which usually came (or still come) with DSLRs or video cameras.

Edit the boot config

The easiest way to enable this is to edit the file /boot/config.txt . You can even do this before installing your Pi by editing the file on the SD-card.

First, disable HDMI output by commenting or removing this line:

and adding this line:

This disables the HDMI output. Then find the following and uncomment it to enable analog video output:

Set its value to one of the following:

  • 0 — NTSC
  • 1 — Japanese NTSC
  • 2 — PAL
  • 3 — Brazilian PAL

Save the file, and reboot your Pi to see the effects!

During boot

There’s another way that I weirdly never got to work (simply nothing happened). According to this source (amongst others), you can boot the Pi while holding shift, and then press the keys 1-4 to switch outputs.

Toggle output

To toggle HDMI/analog video output on the fly, try the following commands:

Disable HDMI output:

Enable composite output (example):


Here are a few things you should know if the screen stays blank:

  • Note that the Pi 3 B(+) does not send an analog video signal over the 4-pins a/v jack. I have not found a clear source on this other than some forum posts and the fact that I could not get it to work on my Pi 3B+. Allegedly the new 4 does send analog video over the jack again.
  • If you followed the steps above but your TV doesn’t show anything, try reducing the output resolution using the sudo raspi-config tool. I’ve seen the video bug out on large resolutions and work correctly on 1024×720, even though that resolution is still larger than what both PAL and NTSC support.
  • As of July 2019, there are issues that break analog video output. I’ve had success simply running rpi-update , however, if you’re running RetroPie, that command will break the emulationstation! So if the instructions above are not working on your Retropie, downgrade your kernel like so: sudo


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

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.

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
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.

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

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

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

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:


Raspberry pi analogue output

If you want to enable the Raspberry Pi to play sounds using the analogue 3.5mm jack output you need to make sure the right kernel modules are loaded and that the audio mixer is configured to use the right output.

Start by checking that the sound module for Raspberry Pi is loaded using the following command to list all the active kernel modules and check to see if any match the name of the sound module.

To do this you need to be running as root.

Then list the installed kernel modules.

If it you don’t see any output then this means that the modules are not yet loaded, so you will have to load them manually.

If the module is not available you will get an error message, which probably means that you need to update your firmware and kernel to a newer version.

To check that this worked repeat the first command again.

This time it shows the sound modules have been loaded, if they were not automatically loaded then you need to add the sound module for the Raspberry Pi to /etc/modules.

As well as configuring the sound module to load automatically you also need to install ALSA (Advance Linux Sound Architecture) which provides audio and MIDI functionality.

If the HDMI cable is connected then the audio output will default to HDMI, otherwise it should use the analogue 3.5mm headphone output. If necessary you can force the Raspberry Pi to use the analog output with the following command.

To test that everything is working use the following command to play one of the test sounds installed with the ALSA package.

To play MP3 files from the command line you will need to install an MP3 player, the example below shows how to install mpg123 which worked for me.

You can also use omxplayer to play MP3s and videos, however is wasn’t in the official repository so you may have to download the package your self from here.

I used version 0.3 since I did not need DBUS support. When you have download the installation package use dpkg to install it.

When running omxplayer you can specify which output to use from the command line. To use the analogue audio output.

Alternativly to use the HDMI audio output.

The plus and minus keys can be used to adjust the volume.

Raspberry Pi is a trademark of the Raspberry Pi Foundation


Raspberry pi analogue output

In a previous post, I showed how you can use the Pulse Width Modulation feature of the Pi to create a rather simple but precise (static) analog output level. Here is that post:

In this post we’ll go one step further and create some dynamic wave forms and a very precise voltage level by using a DAC chip.
I used an ADC chip in another post to create a digital representation of an analog signal, the reverse of what we will be doing here. You can have a look at that post here :
This post also deals with the installation of the SPI drivers for the kernel that we need for this application as well.

For the DAC, I selected an MPC4911. It has 10 bits resolution and an SPI interface, is commonly available and inexpensive. For more details, look at the datasheet:
http://ww1.microchip.com/downloads/en/D . 22248a.pdf

The conversion process for the DAC uses a reference voltage. In this model, the reference is not included on the chip, so we need to supply one. For the most simple applications, we can use the 3V3 that is available on the P1 interface. The reference voltage also limits the output level, so we can only go up to 3V3 for the output level. We could have also used the 3V3 as a supply voltage, because the chip is happy with 2V7 already. In that case, just connect pin 1 and pin 6 together and connect it to the 3V3 supply. The diagram below shows the connections for this simple circuit with a separate reference voltage.

As with the MCP 3002, we’ll use the following pins to communicate with the device through the SPI bus. The SCLK for the clock, MOSI to send the data and one of the CE pins to select the device. We don’t get anything back from this chip, so MISO is not used.
The LDAC input can be used to select the device, and you could use a GPIO pin to turn the device on and off through this pin. In this example, we just tie it to ground so it is always selected and powered.

Let us now switch to the required software.

The conversion process we need works as follows. A digital number represents the analog output level in 10 bit steps, or in 1024 different values. With a reference voltage of 3V3, you can set the output in 3300mV / 1024 = 3.22mV steps, which is also the precision of the output, analog to the MPC 3002.

A very simple program can be constructed as follows:

With this, you can program a voltage with a 3.22mV resolution between 0 and 3V3. If you need to have a higher voltage, look at the post for the ADC, it details how you can use an Op Amp solution to get higher voltages.

If you want to get one step further with the DAC, the previous program can easily be changed to generate other wave forms. Here is one example for a saw tooth (a ramp).

This again can easily be changed to create a triangle waveform, by adding another for-next loop counting down to 0.

Finally, here is a program that outputs a sine wave.

With this under your belt, you can now create your own function generator, albeit within limits of the possible frequency. The frequency is determined by the SPI clock speed, and works differently then you probably guessed. Search this forum for more details about the spi.max_speed_hz settings for the SPI interface.

You will have noticed that the resolution per step is a bit awkward, because of the division by 1024, the resolution of the 10-bit chip. Luckily, there are voltage reference chips available that output a voltage reference level of 1024 mV or multiples of that value. This allows you to create more sensible steps, and with the gain function of the DAC set to 2x, you have now more flexibility if your application needs it.

Re: Add an analog output to the Pi (DAC)

What in your first example code would need to change if I were to use the MCP4921 (12-bit chip)? I have been playing around and I can get the 12-bit chip to work with the 10-bit functions/code, but I cannot figure out what to change to get the code to work in 12-bit.

Re: Add an analog output to the Pi (DAC)

You need to look at page 18 of the MCP4921 datasheet where the bits of the write command register are detailed. The difference between the two chips is only in the lower byte of the register. Instead of 6 data bits for the MCP4911, the MCP4921 has all 8 bits occupied.

Basically, you can go through the code in the first example and change all references from 10 bit to 12 bits.