Building a Class D Audio Amplifier HAT
Overview
This tutorial walks through a small stereo Class D amplifier HAT for a Raspberry Pi. The HAT takes low-level left/right audio from the Pi or an external DAC, routes it through a dual-gang audio potentiometer, and drives a PAM8403-compatible 3 W per-channel amplifier into left and right speaker terminal blocks.
Class D amplifiers switch their output transistors at a high frequency and use the speaker load plus output filtering to recover the audio waveform. That makes them efficient and cool-running compared with linear amplifiers, but it also means layout matters: keep the high-current speaker loops short, keep audio input traces away from speaker outputs, and put supply bypassing right next to the amplifier.
Requirements
- PAM8403-compatible 3 W per-channel stereo Class D amplifier.
- Dual-gang 10 kΩ audio potentiometer for left/right volume control.
- Left and right 2-pin speaker terminal blocks.
- 100 µF bulk capacitor plus 100 nF local bypass capacitor near the amplifier.
- Raspberry Pi HAT connector for 5 V, ground, and optional PWM/I2S audio pins.
- Optional 3-pin external audio header if you use a small DAC module instead of direct Pi PWM audio.
Audio integration choices
A Raspberry Pi does not expose a high-quality analog line output on the 40-pin header by default. Use one of these approaches:
- External I2S DAC module: route the DAC line outputs into the HAT's
AUDIO_L,AUDIO_R, andAGNDheader. This gives the best quality. - PWM audio with RC filter: use PWM pins and a low-pass filter before the potentiometer. This is acceptable for simple beeps or speech, but noisier.
- Pi 3.5 mm jack jumper: if your Pi model has analog audio, route the jack output to the HAT input header.
The example below keeps the amplifier section explicit and uses an AUDIO_IN header so the HAT can be paired with any of those sources.
Step 1: Create the HAT outline
import { RaspberryPiHatBoard } from "@tscircuit/common"
export default () => (
<RaspberryPiHatBoard name="HAT1">
{/* amplifier circuit goes here */}
</RaspberryPiHatBoard>
)
Step 2: Add the amplifier and audio controls
The PAM8403 has separate left/right inputs and differential speaker outputs. The dual-gang potentiometer attenuates both channels together.
<chip
name="U1"
footprint="sop16"
manufacturerPartNumber="PAM8403"
pinLabels={{
1: "LOUT+",
2: "PVDDL",
3: "LOUT-",
4: "LIN",
5: "GND",
6: "RIN",
7: "ROUT-",
8: "PVDDR",
9: "ROUT+",
16: "VDD",
}}
/>
<chip name="RV1" footprint="potentiometer_9mm" manufacturerPartNumber="Dual 10k audio pot" />
<chip name="J_AUDIO" footprint="pinrow3" manufacturerPartNumber="Audio input: L/R/GND" />
<chip name="J_LEFT" footprint="terminalblock_2" manufacturerPartNumber="Left speaker" />
<chip name="J_RIGHT" footprint="terminalblock_2" manufacturerPartNumber="Right speaker" />
Step 3: Add supply filtering
Place the 100 nF capacitor as close as possible to the amplifier VDD pin. Place the 100 µF bulk capacitor close to the amplifier power entry so bass transients do not pull the HAT 5 V rail down.
<capacitor name="C1" capacitance="100uF" footprint="cap_0603" />
<capacitor name="C2" capacitance="100nF" footprint="0402" />
<trace from=".C1 > .pin1" to="net.V5" />
<trace from=".C1 > .pin2" to="net.GND" />
<trace from=".C2 > .pin1" to=".U1 > .VDD" />
<trace from=".C2 > .pin2" to="net.GND" />
Schematic and 3D preview
import { RaspberryPiHatBoard } from "@tscircuit/common"
export default () => (
<RaspberryPiHatBoard name="HAT1">
<chip
name="U1"
footprint="sop16"
manufacturerPartNumber="PAM8403"
pcbX={0}
pcbY={0}
pinLabels={{
1: "LOUT+",
2: "PVDDL",
3: "LOUT-",
4: "LIN",
5: "GND",
6: "RIN",
7: "ROUT-",
8: "PVDDR",
9: "ROUT+",
16: "VDD",
}}
/>
<chip name="RV1" footprint="potentiometer_9mm" manufacturerPartNumber="Dual 10k audio pot" pcbX={-18} pcbY={0} pinLabels={{ 1: "L_IN", 2: "L_WIPER", 3: "AGND", 4: "R_IN", 5: "R_WIPER", 6: "AGND" }} />
<chip name="J_AUDIO" footprint="pinrow3" manufacturerPartNumber="Audio input: L/R/GND" pcbX={-34} pcbY={0} pinLabels={{ 1: "LEFT", 2: "RIGHT", 3: "AGND" }} />
<chip name="J_LEFT" footprint="terminalblock_2" manufacturerPartNumber="Left speaker" pcbX={24} pcbY={9} pinLabels={{ 1: "L+", 2: "L-" }} />
<chip name="J_RIGHT" footprint="terminalblock_2" manufacturerPartNumber="Right speaker" pcbX={24} pcbY={-9} pinLabels={{ 1: "R+", 2: "R-" }} />
<capacitor name="C1" capacitance="100uF" footprint="cap_0603" pcbX={-8} pcbY={10} />
<capacitor name="C2" capacitance="100nF" footprint="0402" pcbX={-4} pcbY={10} />
<trace from=".HAT1_chip > .V5_1" to="net.V5" />
<trace from=".HAT1_chip > .GND_1" to="net.GND" />
<trace from="net.V5" to=".U1 > .VDD" />
<trace from="net.V5" to=".U1 > .PVDDL" />
<trace from="net.V5" to=".U1 > .PVDDR" />
<trace from=".U1 > .GND" to="net.GND" />
<trace from=".C1 > .pin1" to="net.V5" />
<trace from=".C1 > .pin2" to="net.GND" />
<trace from=".C2 > .pin1" to=".U1 > .VDD" />
<trace from=".C2 > .pin2" to="net.GND" />
<trace from=".J_AUDIO > .LEFT" to=".RV1 > .L_IN" />
<trace from=".J_AUDIO > .RIGHT" to=".RV1 > .R_IN" />
<trace from=".J_AUDIO > .AGND" to="net.GND" />
<trace from=".RV1 > .L_WIPER" to=".U1 > .LIN" />
<trace from=".RV1 > .R_WIPER" to=".U1 > .RIN" />
<trace from=".RV1 > .AGND" to="net.GND" />
<trace from=".U1 > .LOUT+" to=".J_LEFT > .L+" />
<trace from=".U1 > .LOUT-" to=".J_LEFT > .L-" />
<trace from=".U1 > .ROUT+" to=".J_RIGHT > .R+" />
<trace from=".U1 > .ROUT-" to=".J_RIGHT > .R-" />
</RaspberryPiHatBoard>
)
Bill of materials
| Ref | Part | Notes |
|---|---|---|
| U1 | PAM8403 stereo Class D amplifier | 5 V, 3 W per channel into suitable speakers |
| RV1 | Dual-gang 10 kΩ audio potentiometer | Audio/log taper preferred |
| J_AUDIO | 3-pin audio input header | Left, right, analog ground |
| J_LEFT/J_RIGHT | 2-pin terminal blocks | Speaker outputs |
| C1 | 100 µF capacitor | Bulk 5 V storage |
| C2 | 100 nF capacitor | Local high-frequency bypass |
Raspberry Pi audio configuration
For an I2S DAC source, enable I2S in Raspberry Pi OS and select the DAC overlay required by your module. A typical /boot/firmware/config.txt setup looks like this:
dtparam=i2s=on
# Example only: use the overlay for your actual DAC board.
dtoverlay=hifiberry-dac
Then test a quiet audio file first:
speaker-test -t wav -c 2
aplay /usr/share/sounds/alsa/Front_Left.wav
Layout guidance
- Keep the audio input header and potentiometer on the quiet side of the board.
- Route speaker outputs as short, paired traces to the terminal blocks.
- Do not run speaker outputs under the potentiometer or audio input header.
- Use a solid ground return from the amplifier to the HAT ground pins.
- Put
C2directly besideU1; putC1close to the 5 V entry path.
Bring-up checks
- With no speakers connected, confirm there is no short between 5 V and ground.
- Power the Pi and verify 5 V appears at the amplifier supply pins.
- Start with the potentiometer fully down and play a low-volume stereo test.
- Connect inexpensive test speakers before connecting final speakers.
- If there is hiss or PWM noise, use an I2S DAC or improve the analog low-pass filtering before the HAT input.