Building a Class D Audio Amplifier HAT
Overview
This tutorial will guide you through designing a Raspberry Pi Audio HAT featuring the PAM8403, a highly efficient Class D audio amplifier. It provides 3W per channel stereo output, making it perfect for driving small speakers in arcade cabinets, media centers, or voice assistants.
Our HAT will feature:
- A PAM8403 Class D amplifier chip
- A dual potentiometer for hardware volume control
- Speaker terminal blocks for easy connection
- RC low-pass filters to convert the Raspberry Pi's PWM audio into a smooth analog signal
Understanding Class D Amplifiers
Unlike traditional Class AB amplifiers that waste excess energy as heat, Class D amplifiers act like high-speed electronic switches. They convert the incoming analog audio into a high-frequency PWM (Pulse Width Modulation) signal.
Because the internal transistors are either fully "ON" or fully "OFF", they dissipate very little heat, allowing the PAM8403 to achieve up to 90% efficiency. This is why a chip smaller than a fingernail can output a massive 3W per channel without needing a heatsink!
Circuit Requirements
To build this audio HAT, we need:
- PAM8403 Chip: The core amplifier.
- RC Low-Pass Filter: The Raspberry Pi outputs audio as a digital PWM signal on GPIO 18/19. We use resistors (270Ω) and capacitors (33nF) to smooth this high-frequency PWM into a clean analog sine wave.
- Input Coupling Capacitors: 1µF capacitors are placed before the PAM8403 inputs to block any DC voltage bias from entering the amplifier, preventing speaker popping.
- Potentiometer: A dual-gang 10k potentiometer acts as a physical volume knob, dividing the voltage before it reaches the amplifier.
Building the Circuit Step by Step
Step 1: Add the Raspberry Pi Board and Amplifier
First, we set up the board and drop in the PAM8403 IC using a standard SOIC-16 footprint.
import { RaspberryPiHatBoard } from "@tscircuit/common"
export default () => (
<RaspberryPiHatBoard name="HAT1">
<chip
name="U1"
footprint="soic16"
manufacturerPartNumber="PAM8403"
pinLabels={{
pin1: ["OUTL+"], pin2: ["PGND"], pin3: ["OUTL-"], pin4: ["PVDD"],
pin5: ["MUTE"], pin6: ["VDD"], pin7: ["INL"], pin8: ["VREF"],
pin9: ["NC"], pin10: ["INR"], pin11: ["GND"], pin12: ["SHDN"],
pin13: ["PVDD"], pin14: ["OUTR-"], pin15: ["PGND"], pin16: ["OUTR+"]
}}
pcbX={0}
pcbY={0}
/>
</RaspberryPiHatBoard>
)
Step 2: Input Filtering and Volume Control
We must filter the Pi's digital PWM output into an analog signal. We'll add our RC filter, the volume potentiometers (represented here as SIP3 chips), and the DC-blocking capacitors.
Step 3: Add Speaker Terminals and Trace Routing
Finally, we connect the power lines (using the Pi's 5V rail since the PAM8403 runs perfectly on 5V) and route the high-power outputs to screw terminals (SIP-2) for easy speaker wire connection.
import { RaspberryPiHatBoard } from "@tscircuit/common"
export default () => (
<RaspberryPiHatBoard name="HAT1">
<chip name="U1" footprint="soic16" pinLabels={{pin1: ["OUTL+"], pin3: ["OUTL-"], pin14: ["OUTR-"], pin16: ["OUTR+"]}} pcbX={0} pcbY={0} />
<chip name="J_SPK_L" footprint="sip2" pcbX={15} pcbY={-10} />
<chip name="J_SPK_R" footprint="sip2" pcbX={15} pcbY={10} />
<trace from=".U1 .OUTL+" to=".J_SPK_L > .pin1" />
<trace from=".U1 .OUTL-" to=".J_SPK_L > .pin2" />
<trace from=".U1 .OUTR+" to=".J_SPK_R > .pin1" />
<trace from=".U1 .OUTR-" to=".J_SPK_R > .pin2" />
</RaspberryPiHatBoard>
)
Raspberry Pi Audio Configuration
By default, modern Raspberry Pi operating systems try to route audio through HDMI. To route audio through the GPIO pins (PWM audio), you need to configure your Pi's device tree.
-
Open a terminal on your Pi and edit the boot config file: ```bash sudo nano /boot/firmware/config.txt
On older OS versions: sudo nano /boot/config.txt
```
-
Add or uncomment the following line to enable PWM audio on GPIO 18 (Left) and GPIO 19 (Right): ```text dtoverlay=audremap,pins_18_19 ```
-
Save the file (Ctrl+O, Enter, Ctrl+X) and reboot: ```bash sudo reboot ```
-
Test your speakers using the terminal! ```bash speaker-test -c2 -t wav ```
Next Steps
- Add an I2S DAC (like the PCM5102A) instead of using PWM audio for audiophile-grade high-fidelity sound.
- Add an LED to indicate when the amplifier is powered on.
- Experiment with the `MUTE` and `SHDN` pins of the PAM8403 to disable audio programmatically via a separate GPIO pin.