From d84272922e8694f45b57e458eea58f8a8d300db5 Mon Sep 17 00:00:00 2001 From: Misch369 Date: Thu, 21 May 2026 01:44:04 -1100 Subject: [PATCH] docs: add Class D Audio Amplifier HAT tutorial --- .../pi-hats/class-d-audio-amplifier-hat.mdx | 210 ++++++++++++++++++ 1 file changed, 210 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..3c8a05e9 --- /dev/null +++ b/docs/tutorials/pi-hats/class-d-audio-amplifier-hat.mdx @@ -0,0 +1,210 @@ +--- +title: Building a Class D Audio Amplifier HAT +description: >- + This tutorial will walk you through building a Raspberry Pi HAT with a PAM8403 + Class D audio amplifier using tscircuit, providing 3W per channel stereo output. +--- + +## Overview + +This tutorial will guide you through designing a Raspberry Pi Audio HAT featuring the **PAM8403**, a highly efficient Class D audio amplifier. It provides 3W per channel stereo output, making it perfect for driving small speakers in arcade cabinets, media centers, or voice assistants. + +Our HAT will feature: +- A PAM8403 Class D amplifier chip +- A dual potentiometer for hardware volume control +- Speaker terminal blocks for easy connection +- RC low-pass filters to convert the Raspberry Pi's PWM audio into a smooth analog signal + +import CircuitPreview from "@site/src/components/CircuitPreview" +import TscircuitIframe from "@site/src/components/TscircuitIframe" + + ( + + {/* PAM8403 Class D Amplifier */} + + + {/* Dual Potentiometer for Volume Control (simplified as two resistors for footprint) */} + + + + {/* Input Coupling Capacitors (1uF) */} + + + + {/* PWM Low-Pass Filter (Resistors & Capacitors) */} + + + + + + + {/* Speaker Terminals (Left and Right) */} + + + + {/* Power and GND Traces */} + + + + + + {/* Left Channel Filter & Volume */} + + + + + + + + {/* Right Channel Filter & Volume */} + + + + + + + + {/* Amplifier to Speaker Terminals */} + + + + + + +) +`} /> + +## Understanding Class D Amplifiers + +Unlike traditional Class AB amplifiers that waste excess energy as heat, **Class D amplifiers** act like high-speed electronic switches. They convert the incoming analog audio into a high-frequency PWM (Pulse Width Modulation) signal. + +Because the internal transistors are either fully "ON" or fully "OFF", they dissipate very little heat, allowing the PAM8403 to achieve up to **90% efficiency**. This is why a chip smaller than a fingernail can output a massive 3W per channel without needing a heatsink! + +## Circuit Requirements + +To build this audio HAT, we need: +- **PAM8403 Chip:** The core amplifier. +- **RC Low-Pass Filter:** The Raspberry Pi outputs audio as a digital PWM signal on GPIO 18/19. We use resistors (270Ω) and capacitors (33nF) to smooth this high-frequency PWM into a clean analog sine wave. +- **Input Coupling Capacitors:** 1µF capacitors are placed before the PAM8403 inputs to block any DC voltage bias from entering the amplifier, preventing speaker popping. +- **Potentiometer:** A dual-gang 10k potentiometer acts as a physical volume knob, dividing the voltage before it reaches the amplifier. + +## Building the Circuit Step by Step + +### Step 1: Add the Raspberry Pi Board and Amplifier + +First, we set up the board and drop in the PAM8403 IC using a standard SOIC-16 footprint. + +```tsx +import { RaspberryPiHatBoard } from "@tscircuit/common" + +export default () => ( + + + +) +``` + +### Step 2: Input Filtering and Volume Control + +We must filter the Pi's digital PWM output into an analog signal. We'll add our RC filter, the volume potentiometers (represented here as SIP3 chips), and the DC-blocking capacitors. + + ( + + + + {/* Left Channel Filter + Vol */} + + + + + + {/* Right Channel Filter + Vol */} + + + + + +) +`} /> + +### Step 3: Add Speaker Terminals and Trace Routing + +Finally, we connect the power lines (using the Pi's 5V rail since the PAM8403 runs perfectly on 5V) and route the high-power outputs to screw terminals (SIP-2) for easy speaker wire connection. + + ( + + + + + + + + + + + +) +`} /> + +## Raspberry Pi Audio Configuration + +By default, modern Raspberry Pi operating systems try to route audio through HDMI. To route audio through the GPIO pins (PWM audio), you need to configure your Pi's device tree. + +1. Open a terminal on your Pi and edit the boot config file: + \`\`\`bash + sudo nano /boot/firmware/config.txt + # On older OS versions: sudo nano /boot/config.txt + \`\`\` + +2. Add or uncomment the following line to enable PWM audio on GPIO 18 (Left) and GPIO 19 (Right): + \`\`\`text + dtoverlay=audremap,pins_18_19 + \`\`\` + +3. Save the file (Ctrl+O, Enter, Ctrl+X) and reboot: + \`\`\`bash + sudo reboot + \`\`\` + +4. Test your speakers using the terminal! + \`\`\`bash + speaker-test -c2 -t wav + \`\`\` + +## Next Steps +- Add an I2S DAC (like the PCM5102A) instead of using PWM audio for audiophile-grade high-fidelity sound. +- Add an LED to indicate when the amplifier is powered on. +- Experiment with the \`MUTE\` and \`SHDN\` pins of the PAM8403 to disable audio programmatically via a separate GPIO pin.