Building a Class D Audio Amplifier HAT
Overview
This tutorial builds a small Raspberry Pi audio HAT around a PAM8403-style stereo Class D amplifier. The HAT takes left and right audio from the Pi, routes it through a dual-gang volume control, drives two speaker terminal blocks, and includes local power filtering for the amplifier.
What You Are Building
The HAT contains:
- PAM8403-style 3 W per channel stereo Class D amplifier
- Dual-gang 10 kOhm volume control
- Left and right speaker terminal blocks
- Power filtering near the amplifier
- Pull-down resistors on both audio inputs
- Power LED for quick bring-up feedback
The PAM8403 is commonly used from a 5 V supply. Use speakers that match the amplifier board and power source you choose.
Bill of Materials
| Reference | Part | Value or package | Notes |
|---|---|---|---|
| U1 | PAM8403 stereo amplifier | SOIC/QFN module-friendly footprint | 3 W + 3 W Class D output |
| RV1 | Dual audio potentiometer | 10 kOhm, logarithmic preferred | Shared volume control |
| J1, J2 | Speaker terminals | 2 pin, 5.08 mm | Left and right speaker outputs |
| R1, R2 | Resistors | 100 kOhm | Input pull-downs |
| R3 | Resistor | 1 kOhm | LED current limit |
| C1 | Capacitor | 10 uF or larger | 5 V bulk filtering |
| C2 | Capacitor | 100 nF | Local high-frequency bypass |
| LED1 | LED | Green, 0603 | Power indicator |
Step 1: Add the Amplifier
Start with the amplifier and label the audio inputs, speaker outputs, supply, and control pins clearly.
Step 2: Add Volume Control
A dual-gang potentiometer keeps the left and right channels at the same volume. The wipers feed the amplifier inputs, and both low sides connect to signal ground.
Step 3: Add Speaker Terminals
Class D outputs are usually bridge-tied load outputs. Do not connect either speaker terminal to ground unless the amplifier datasheet explicitly says it is safe.
Step 4: Add Power Filtering
Place the bulk and bypass capacitors close to the amplifier. Audio boards are sensitive to supply bounce, especially when both speakers draw current at the same time.
<capacitor name="C1" capacitance="10uF" footprint="0805" />
<capacitor name="C2" capacitance="100nF" footprint="0402" />
<trace from=".HAT1_chip .V5_1" to=".U1 .VDD" />
<trace from=".HAT1_chip .GND_1" to=".U1 .GND" />
<trace from=".C1 > .pin1" to=".U1 .VDD" />
<trace from=".C1 > .pin2" to=".U1 .GND" />
<trace from=".C2 > .pin1" to=".U1 .VDD" />
<trace from=".C2 > .pin2" to=".U1 .GND" />
Raspberry Pi Audio Setup
For quick testing, route PWM audio to GPIO18 and GPIO19, or feed the HAT from an external DAC module. A simple /boot/firmware/config.txt test setup can enable the Pi audio path:
dtparam=audio=on
Then test with:
speaker-test -t sine -f 440 -c 2
Start with the volume low, confirm left and right channels separately, then raise the volume slowly.
PCB Layout Notes
- Keep speaker output traces short and wide.
- Keep input traces away from the switching speaker outputs.
- Put C1 and C2 near the amplifier power pins.
- Route left and right input traces as similar lengths when practical.
- Place the volume control on a board edge so the knob is reachable.
- Add silkscreen labels for left and right speaker polarity.
Bring-Up Checklist
- Inspect for shorts between 5 V and GND.
- Power the HAT without speakers and check that the LED turns on.
- Confirm the amplifier supply pin is near 5 V.
- Connect one speaker at low volume and play a sine wave.
- Repeat for the other channel.
- Check that the amplifier stays cool after several minutes at normal listening volume.
Common Fixes
- Hum or hiss: shorten the input wiring and keep it away from the speaker outputs.
- One channel silent: check the potentiometer wiper and speaker terminal polarity.
- Distortion at low volume: confirm the audio source is not clipping before the amplifier.
- Amplifier overheats: use a lower volume, higher-impedance speakers, or a stronger 5 V supply.