From e456cc9eb929d8938ad981b4c4cae3188900088d Mon Sep 17 00:00:00 2001 From: nguyentamdat Date: Thu, 21 May 2026 16:59:47 +0700 Subject: [PATCH] Teach Raspberry Pi Class D amplifier HAT design Constraint: tscircuit/docs issue #603 requests a PAM8403 Class D Audio Amplifier HAT tutorial covering stereo output, volume control, speaker terminals, Raspberry Pi integration, and audio configuration.\nRejected: Wiring Pi GPIO as analog audio | Raspberry Pi GPIO does not provide line-level analog output, so the tutorial uses an external/DAC line input.\nConfidence: high\nScope-risk: narrow\nDirective: Keep the tutorial electrically honest about differential speaker outputs and Raspberry Pi audio-source limits.\nTested: bun install --frozen-lockfile; bun run typecheck; bun run build; git diff --cached --check\nNot-tested: Live browser visual QA of rendered tscircuit iframes. --- .../pi-hats/class-d-audio-amplifier-hat.mdx | 377 ++++++++++++++++++ 1 file changed, 377 insertions(+) create mode 100644 docs/tutorials/pi-hats/class-d-audio-amplifier-hat.mdx diff --git a/docs/tutorials/pi-hats/class-d-audio-amplifier-hat.mdx b/docs/tutorials/pi-hats/class-d-audio-amplifier-hat.mdx new file mode 100644 index 00000000..6079d322 --- /dev/null +++ b/docs/tutorials/pi-hats/class-d-audio-amplifier-hat.mdx @@ -0,0 +1,377 @@ +--- +title: Building a Class D Audio Amplifier HAT +description: >- + Design a Raspberry Pi HAT that powers a PAM8403-style stereo Class D amplifier, + adds volume control, and exposes screw-terminal speaker outputs. +--- + +## Overview + +This tutorial walks through a compact Raspberry Pi HAT for a PAM8403-style +Class D audio amplifier. The HAT takes 5V power from the Raspberry Pi header, +accepts stereo line-level audio from a DAC or external audio header, adds a +front-panel volume control, and drives left/right speaker terminal blocks. + +import CircuitPreview from "@site/src/components/CircuitPreview" +import TscircuitIframe from "@site/src/components/TscircuitIframe" + + ( + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +) +`} /> + +## What you will build + +The finished board includes: + +- A PAM8403-compatible Class D stereo amplifier for about 3W per channel into + suitable small speakers +- A dual-gang 10k audio-taper potentiometer for left/right volume control +- A three-pin stereo line input header (`L`, `R`, `GND`) +- Left and right two-pin speaker terminals +- Local 100nF and bulk 470uF supply capacitors near the amplifier +- A shutdown pull-up so the amplifier starts when the HAT is powered + +The Raspberry Pi 40-pin header is used for **5V and ground**. Raspberry Pi GPIO +pins do not provide line-level analog audio, so feed this HAT from a DAC, USB +audio adapter, HDMI audio extractor, or another line-level source. + +## Component choices + +| Reference | Part | Purpose | +| --------- | ---- | ------- | +| U1 | PAM8403-style Class D amplifier | Efficient stereo speaker drive from 5V | +| RV1 | Dual 10k audio-taper potentiometer | Tracks left/right volume together | +| J_AUDIO | 3-pin audio input header | Line-level left, right, and ground input | +| J_LEFT_SPK, J_RIGHT_SPK | 2-pin terminal blocks | Speaker outputs | +| C1, C2 | 1uF coupling capacitors | Block DC from the upstream audio source | +| C3 | 100nF ceramic capacitor | High-frequency supply decoupling | +| C4 | 470uF electrolytic capacitor | Handles speaker-current transients | +| R1 | 10k resistor | Pulls shutdown high to enable the amplifier | + +## How Class D amplification works + +A Class D amplifier rapidly switches its output devices instead of operating +linearly like a Class AB amplifier. The speaker sees a filtered version of this +switching waveform, so the amplifier wastes less power as heat. That efficiency +is why a small 5V HAT can drive useful speaker volume without a large heatsink. + +For layout, treat the speaker outputs as noisy switching nets. Keep them short, +route them away from the line-level input traces, and keep the decoupling +capacitors close to the amplifier power pins. + +## Step 1: Add the HAT and amplifier + +Start with the Raspberry Pi HAT outline, then place the amplifier near the +speaker terminals so high-current output traces stay short. + + ( + + + +) +`} /> + +## Step 2: Add line input and volume control + +Use a 3-pin header for stereo line input and model the dual-gang potentiometer +as a six-pin component. Each channel has an input end, a wiper, and a grounded +end of the resistive track. + + ( + + + + + + +) +`} /> + +## Step 3: Add power filtering and shutdown control + +The PAM8403 runs from 5V. Put the 100nF ceramic close to the amplifier power +pins, then place the bulk electrolytic nearby to supply speaker current peaks. +A 10k pull-up keeps shutdown deasserted. + + ( + + + + + + + + + + + + + + + +) +`} /> + +## Step 4: Add speaker terminals and complete the circuit + +Class D bridge outputs are differential. Do not tie either speaker terminal to +ground unless the amplifier datasheet explicitly says it is allowed. + + ( + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +) +`} /> + +## PCB layout checklist + +- Put C3 within a few millimeters of U1 VDD/GND. +- Keep C4 close to the amplifier and use a short, wide 5V path from the HAT + header. +- Route `LIN` and `RIN` as quiet traces away from `LOUT*` and `ROUT*`. +- Place speaker terminals at the board edge and label polarity clearly. +- Leave mechanical clearance around the potentiometer shaft or panel connector. +- If the speakers are off-board, twist each speaker pair to reduce radiated + noise. + +## Raspberry Pi audio integration + +The HAT receives audio on `J_AUDIO`; it does not synthesize analog audio from +GPIO. Common options are: + +1. Use a USB audio dongle and wire its line output to `J_AUDIO`. +2. Add an I2S DAC HAT or breakout and feed the DAC line output into `J_AUDIO`. +3. Use HDMI audio extraction if your product already has HDMI in the enclosure. + +On Raspberry Pi OS, verify the selected audio device before connecting speakers: + +```bash +aplay -l +speaker-test -c 2 -t wav +``` + +Start with the volume low. If the output clips, reduce the software mixer level +or the upstream DAC gain before increasing the HAT volume. + +## Bring-up checklist + +1. With the HAT unpowered, check for shorts between 5V and ground. +2. Power the Raspberry Pi without speakers attached and confirm 5V reaches U1. +3. Confirm `SHDN` is high through R1. +4. Connect speakers, keep the volume low, then play a stereo test file. +5. Check both speaker terminals for correct left/right channel mapping. +6. Let the board play audio for several minutes and confirm U1 and C4 stay cool. + +## Next steps + +- Add an I2S DAC section so the HAT accepts digital audio directly. +- Add an amplifier mute GPIO by driving `SHDN` from the Raspberry Pi through a + small transistor or level-safe buffer. +- Add ferrite beads or an LC filter if your speaker cables are long. +- Add mounting holes and a panel footprint for the volume knob.