News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Class D Audio from PWM

Started by Fanie, Aug 20, 2025, 09:22 AM

Previous topic - Next topic

Fanie

I wonder if anyone tried using a pic to play sounds or create music using the PWM peripheral ?
Class D amplifiers use a PWM technique converted into pulse width modulated signal and then it is amplified.

Seems the lower frequencies is going to be a problem ?

Most of the time when I need a lower frequency PWM I end up writing code and not use the PWM peripheral.

charliecoutas

I feel the answer is probably yes, so I asked ChatGPT. Here is what she said:

Short answer: yes—as the modulator/controller, not as the power stage.

A Microchip PIC can generate the high-frequency PWM a Class-D amp needs and drive an external half/full-bridge, but the PIC itself can't source the speaker current. In practice you do:

Generate PWM on the PIC

Use CCP/ECCP (or Motor-Control PWM on dsPIC33).

Pick a switching freq ≈ 250–500 kHz so it's well above audio.

Add complementary outputs with dead-time (≈50–150 ns) to avoid shoot-through.

Feed the PWM into a MOSFET gate driver (e.g., high/low-side driver) and power MOSFETs.

Low-pass filter + speaker

After the H-bridge, use an LC filter sized for the load (e.g., for 8 Ω: L ≈ 10–22 µH, C ≈ 0.47–1 µF gives a cutoff around 40–70 kHz), then the speaker.

Audio input path

Easiest digital path: sample audio with the PIC ADC (≥ 48 kS/s), map the sample to PWM duty, output on the next PWM period. Keep ISR latency tight to minimize jitter.

Or do an analog comparator modulator (triangle + comparator) and let the PIC handle drive/housekeeping.

What a basic 12 V, 8 Ω build looks like

Full bridge on 12 V can deliver up to ~9 W ideal (Vrms ≈ 12/√2; P ≈ Vrms²/R).

A reasonable starting filter: L = 15 µH, C = 0.68 µF (fc ≈ 50 kHz).

Include OC/OT protection, UVLO on the driver, and pop-suppression on startup.

Gotchas & limits

Resolution vs. frequency: PWM steps = Fclk / Fpwm. With a 16 MHz PIC at 400 kHz you only get ~40 counts (~5.3 "bits")—fine for a demo, not hi-fi. A faster dsPIC (e.g., 70–100+ MHz) gives far better effective resolution (and features like programmable dead-time).

Jitter & THD+N: Timer jitter, ISR latency, and supply bounce will raise distortion/noise.

Efficiency is great, but EMI control (snubbers, tight loops, proper grounding, LC layout) is mandatory.

When to use a dedicated IC
If you want real hi-fi numbers (low THD+N, wide SNR, protections, pop control), a Class-D driver/controller IC (or an all-in-one Class-D amp IC) will beat a general-purpose PIC for effort and performance. A dsPIC33 can land in the middle ground if you also want built-in DSP (EQ, limiters, xovers).

TL;DR

Feasible: a PIC can run the PWM and control logic and—paired with a gate driver, MOSFETs, and an LC filter—make a working Class-D amp (great for learning/prototyping).

For quality audio: use a faster dsPIC (motor-control PWM) or a dedicated Class-D amp IC.

top204

#2
With 8-bit to 32-bit microcontrollers, PWM is the standard method of implementing a DAC for audio. It is, generally, the dedicated microcontrollers that have one or more standard DACs on them, that do not need to use PWM.

In my experience, PWM is an excellent DAC for audio, and as long as the PWM is operating at a frequency above any obvious harmonics, and has a decent bit-rate, it works well with both low and high frequencies.

I have even combined two 8-bit PWMs to make a 16-bit DAC, and it sounded incredible, with a bit of clever coding to combine them, and even scale up some lower bit-rate audio data so it gave a lot better sound.

The important part of PWM as a DAC is the integrator and filtering that is required at its output. But for normal audio, as a toy or a small speaker etc, a resistor and a capacitor is sometimes all that is needed for decent sound.

The Class-D power amplifiers have been developed over many decades, but they never caught on for true Hi-Fi because of some querks in them that are hard to eliminate, but high speed DSP is helping to make them better. However, for a DAC, PWM operates excellently.

keytapper

#3
I recall a website from a Greek engineer that has made a MP3 player using a 12F1840 reading a SD card. I think that would be simpler if one whenever uses a serial EEPROM for shorter messages.
Ignorance comes with a cost