Audio HAT for the Raspberry Pi Zero

The Raspberry Pi Zero (RBPiZ) and its successor, the Raspberry Pi Zero Wireless (RBPiZW) are very small single board computers. The Pi Foundation wanted to keep their cost and size to the low side, so they did not include either a 3.5 mm audio jack or any other audio port. Although this may seem like a setback for many users, some of them were went ahead and figured out how to get audio out of the board with a little hacking.

Another reason for not including an audio port is the Broadcom chipset used for the RBPiZ and RBPiZW does not have a true analog output. Instead, there are two pulse width modulated (PWM) pins that spew out digital output at very high speeds. To get audio out of these two PWM output pins, one has to filter the signal to the audio frequency range. This allows one to fake an audio signal by adjusting the duty cycle of the PWM pins.

According to physicists, for simulating any analog frequency from a PWM signal, the PWM frequency should necessarily be at least ten times higher than the highest frequency to be replicated in the analog range. As the audio signals humans can hear range from 20 Hz to 20 KHz, the minimum PWM frequency should ideally be about 200 KHz. However, the PWM output from the two RBPis is 50 MHz, so we can comfortably filter out the audio part while suppressing the higher frequencies.

The schematic of the audio HAT for the RBPis shows that the two stereo audio channels, left and right, are designated as PWM0_OUT and PWM1_OUT. On the PWM0_OUT, R21 and R20 are two resistors acting as a voltage divider to bring down the 3.3 V signal to about 1.1 V peaks. The corresponding voltage divider on the PWM1_OUT is formed of R27 and R26. Therefore, the stereo audio line level can give an output of 1.1 V peak-to-peak.

The RC low-pass filter that prevents the high frequencies from passing through is made up of capacitors C20 and C26, working in conjunction with R21 and R27 respectively. With the values of the components used on the board, the cut-off frequency for this RC low-pass filter is 17865 Hz, which is very close to the upper limit of audio frequencies, or 20 KHz.

That still leaves the DC voltage part of the signal on the lines, and one must remove it to prevent damage to any speakers or headphones subsequently connected to them. This is done by capacitors C48 and C34, which allow only AC part of the signal to pass through, and block all DC voltages.

As the PWM pins are being taken to the outside of the board, one must also protect the RBPi from ElectroStatic Discharge (ESD), which can travel back and destroy the RBPi. This is taken care of by ESD protection diodes.

All the above sounds very good and simple, but on the RBPi, the actual PWM0 signal on pin #40, and the PWM1 signal on pin #45, are not available, as they have not been terminated into exposed pads. To circumvent this problem, the PWM0 signal has to be rerouted through software to GPIO pin #18, and the PWM1 signal to GPIO pin #19.