Building a Class D Audio Amplifier HAT
Overview
This tutorial shows how to build a Raspberry Pi HAT around a PAM8403-based stereo Class D amplifier stage. The design focuses on the parts that matter in practice: line-level stereo input, volume control, speaker terminals, and solid power decoupling for the 5V rail.
Class D operation explanation
Class D amplifiers switch their output devices instead of running them in a purely linear mode. That matters for a small Raspberry Pi accessory board because it keeps heat and power loss down while still driving small speakers cleanly.
For this HAT, that means:
- the PAM8403 runs efficiently from 5V
- the speaker outputs can stay direct and compact
- the board does not need a large analog power stage
- short traces and local decoupling help keep the audio path stable
The practical tradeoff is that layout matters. Switching edges and noisy power return paths can leak into the audio path if the board is crowded, so the placement and routing in the circuit are part of the design, not just the parts list.
Raspberry Pi integration
The HAT should plug into the Pi as a simple audio accessory and keep the control story easy for someone following the guide. A good build flow is:
- Mount the HAT on the Raspberry Pi header.
- Feed audio from the Pi or another line-level source.
- Power the amplifier from the Pi-friendly 5V rail.
- Keep speaker wiring separate from the input side so the channels stay easy to trace.
For the build note, call out that the Pi should provide a stable power path and that the amplifier needs enough current headroom for the target speakers. If the project uses another audio source, mention the expected line-level input so readers know what to connect before they power anything on.
Audio configuration guide
For a practical Raspberry Pi setup, the important part is making sure the Pi audio output is routed to the header the tutorial uses and that the system is set to a reasonable default volume before first power-up.
Use this checklist in the guide:
- confirm the Pi is using the intended audio output path
- start at a low system volume
- verify left and right channels before connecting speakers
- test with a quiet audio file first
- increase volume gradually until the amplifier output is clearly audible
If the repo supports config snippets, add them next to the tutorial so a reader can match the board wiring with the software output path. The key outcome is a straightforward end-to-end setup from Pi audio to speaker terminals without guesswork.
Why Class D?
The PAM8403 is a 3W stereo Class D amplifier. In practice, that means:
- high efficiency compared with linear audio amplifiers
- low heat for a compact HAT enclosure
- direct drive for small speakers without a bulky output filter
- enough power for desk speakers, media boxes, and audio add-ons
The official Diodes page for the part is here: PAM8403.
Circuit Requirements
Our HAT needs to:
- accept stereo line-level audio from the Raspberry Pi or an external DAC
- provide independent or shared volume control
- drive left and right speakers from the amplifier outputs
- stay stable on the Pi's 5V rail with nearby decoupling capacitors
Building the Circuit Step by Step
Step 1: Place the HAT board and amplifier stage
The base of the design is the Raspberry Pi HAT form factor plus the PAM8403 amplifier block.
Step 2: Add the stereo input and volume control
For a clean layout, the tutorial uses one volume trimmer per channel. The audio source enters through a 3-pin connector, then each channel passes through a coupling capacitor and a potentiometer before reaching the amplifier input.
Step 3: Add the speaker terminals
Class D outputs should go to the speaker terminals directly. Do not tie either speaker lead to ground.
Step 4: Add decoupling and power
The amplifier needs a clean 5V supply. Put a small ceramic capacitor and a larger bulk capacitor close to the amplifier power pins.
import { RaspberryPiHatBoard } from "@tscircuit/common"
export default () => (
<RaspberryPiHatBoard name="HAT1">
<chip
name="U1"
footprint="soic8"
manufacturerPartNumber="PAM8403"
pinLabels={{
pin1: "LIN",
pin2: "L_OUTP",
pin3: "L_OUTN",
pin4: "GND",
pin5: "R_OUTN",
pin6: "R_OUTP",
pin7: "RIN",
pin8: "VDD",
}}
schPinArrangement={{
leftSide: {
direction: "top-to-bottom",
pins: ["LIN", "L_OUTP", "L_OUTN", "GND"],
},
rightSide: {
direction: "top-to-bottom",
pins: ["R_OUTN", "R_OUTP", "RIN", "VDD"],
},
}}
pcbX={0}
pcbY={-6}
/>
<capacitor name="C3" capacitance="1uF" footprint="0603" pcbX={10} pcbY={-20} />
<capacitor name="C4" capacitance="100uF" footprint="1206" pcbX={16} pcbY={-20} />
<trace from=".U1 .VDD" to=".HAT1_chip .V5_1" />
<trace from=".U1 .GND" to=".HAT1_chip .GND_1" />
<trace from=".U1 .VDD" to=".C3 > .pin1" />
<trace from=".C3 > .pin2" to=".HAT1_chip .GND_1" />
<trace from=".U1 .VDD" to=".C4 > .pin1" />
<trace from=".C4 > .pin2" to=".HAT1_chip .GND_1" />
</RaspberryPiHatBoard>
)
Understanding the Amplifier
Class D amplifiers work by switching their output stage rapidly instead of running it in a linear region. The speaker and output network average those switching pulses back into audio, which is why the part is efficient and runs cooler than many linear designs.
The practical takeaway is simple: use short speaker traces, give the chip good decoupling, and keep the speaker outputs floating. The outputs are not meant to be shorted to ground.
Raspberry Pi Integration
The HAT itself uses the Pi's 40-pin header for power and mounting. For audio, the safest assumption is line-level stereo input from the Pi's audio path or an external DAC. If you want a self-contained product, you can swap the input header for a jack or DAC module without changing the rest of the amplifier section.
Practical connections to keep in mind:
- 5V and ground come from the HAT header
- the audio source should stay line-level, not speaker-level
- the amplifier output goes only to speakers
- a mute or enable GPIO can be added later if you want software control
Audio Configuration Guide
On Raspberry Pi OS, make sure audio output is enabled and test the source before you blame the amplifier.
- Open
raspi-configand enable the relevant audio output. - Confirm the device tree and firmware audio settings are active.
- Test playback with a sine wave or a short WAV file.
- If the output sounds weak, check the source level before increasing gain in the amplifier path.
For a production board, double-check the exact PAM8403 package pinout against the datasheet for the chip variant you source.
Next Steps
- add a mute pin or GPIO-controlled enable line
- swap the input header for a 3.5 mm jack footprint
- add silkscreen labels for the left and right speaker channels
- pair the HAT with a small enclosure and panel-mounted terminals