Class D Audio Amplifier HAT
Overview
This tutorial builds a compact stereo Class D amplifier HAT using a PAM8403. The PAM8403 can drive small left and right speakers at up to 3W per channel from a 5V rail, making it a practical choice for simple Raspberry Pi audio projects.
The HAT includes audio input pins, a dual-gang volume control, the amplifier IC, speaker terminal outputs, and local decoupling for the power rail.
How Class D Amplification Works
A Class D amplifier rapidly switches its output transistors instead of operating them in a continuous linear region. The speaker receives a filtered version of that switching waveform. This approach is efficient, produces less heat than a linear amplifier, and works well for battery-powered or compact boards.
Circuit Requirements
The board needs:
- PAM8403 stereo Class D amplifier
- 5V and ground from the Raspberry Pi HAT header
- left and right audio inputs
- volume control before the amplifier inputs
- left and right speaker terminal outputs
- decoupling capacitors near the amplifier supply pins
Final Circuit Preview
Step 1: Add the HAT Base
Use RaspberryPiHatBoard to start with the expected HAT outline and 40-pin GPIO
header. The amplifier can use the 5V and ground pins from this header.
Step 2: Add Volume Control
A dual-gang potentiometer lets one knob adjust both channels. Route the left and right input signals to the potentiometer inputs, then route each wiper to the matching amplifier input.
Step 3: Add Speaker Outputs
The PAM8403 uses bridge-tied-load outputs, so each speaker connects between a positive and negative output pair. Do not connect either speaker terminal to ground unless the amplifier datasheet explicitly allows it.
Raspberry Pi Audio Configuration
For a Raspberry Pi analog audio source, route left, right, and ground from your audio output connector to the HAT input header. If you use I2S audio, add a DAC module before this amplifier stage and feed the DAC's analog outputs into the volume control.
Example /boot/firmware/config.txt setting for analog audio:
dtparam=audio=on
Then use raspi-config or your desktop audio settings to select the analog
output. Keep the Pi output volume moderate and use the HAT potentiometer for
listening-level adjustment.
Layout Notes
Place the 10uF and 100nF capacitors close to the PAM8403 supply pins. Keep the speaker traces wider than the signal traces, separate speaker output routing from the low-level audio inputs, and keep the volume-control traces short to reduce noise pickup.
Bill of Materials
| Reference | Part | Notes |
|---|---|---|
| U1 | PAM8403 | 3W per channel stereo Class D amplifier |
| RV1 | Dual 10k audio potentiometer | Left/right volume control |
| J1 | 3-pin audio input header | Left, right, ground |
| J2, J3 | 2-pin speaker terminals | Left and right speaker outputs |
| C1 | 10uF capacitor | Bulk supply decoupling |
| C2 | 100nF capacitor | High-frequency decoupling |
| R1 | 10k resistor | Pulls shutdown/enable high |