PWM in Audio Signal Processing: Techniques and Tools

Pulse Width Modulation (PWM) Control / Visits:10

Why Pulse Width Modulation Still Matters in a World of Digital Audio

Pulse width modulation has a reputation problem. Mention PWM to a room full of audio engineers, and half of them will grimace and recall the switching noise from a cheap Class-D amplifier, while the other half will nod politely and think about motor drivers. Neither reaction does justice to what PWM actually is: one of the most flexible, power-efficient, and surprisingly musical ways to represent and manipulate signals. And nowhere is that flexibility more visible today than in the humble micro servo motor, a device that has quietly become a crossover star between robotics, embedded audio, and real-time signal processing.

This article walks through how PWM works in audio contexts, where it shines, where it falls apart, and how the same techniques that drive a micro servo can be repurposed to shape, filter, and even synthesize sound. Along the way, we will look at concrete tools, code patterns, and hardware choices that make PWM audio practical rather than theoretical.

The Basic Physics of PWM, Stripped of Jargon

Duty Cycle as an Analog Stand-In

At its core, PWM encodes a value not in voltage level but in time. A square wave toggles between two rails, and the ratio of on-time to total period is the duty cycle. If the period is short enough relative to the load being driven, the load responds to the average value rather than the instantaneous switching. An RC low-pass filter turns that average into a smooth analog voltage. A speaker cone, with its own mechanical inertia, does something similar: it integrates the fast edges and reproduces the underlying envelope.

That is the entire trick. PWM is not magic; it is a time-domain encoding that exploits the bandwidth limits of whatever comes next.

Sampling Rate, Resolution, and the Nyquist Shadow

The catch is that PWM audio lives or dies by its carrier frequency. If you want 8-bit resolution at a 44.1 kHz effective sample rate, the PWM carrier needs to run at 44,100 × 256, or roughly 11.3 MHz. That is far beyond what most microcontrollers can do in software. Real-world PWM audio therefore trades resolution for carrier headroom, often landing at 7 or 8 bits with carriers in the 30–200 kHz range. The Nyquist theorem does not disappear; it simply gets pushed into a region where the reconstruction filter has to work harder.

Where PWM Meets the Micro Servo Motor

A Shared Language of Timing

A standard hobby micro servo motor expects a pulse train at roughly 50 Hz, with pulse widths between 1 ms and 2 ms mapping to its angular range. That is PWM in its most literal form: not a carrier for audio, but a control signal whose duty cycle directly commands position. The fascinating part is that the same timer peripheral generating those 50 Hz pulses can, with a register change, produce a 100 kHz carrier suitable for audio playback. This is not a coincidence. Both applications exploit the same hardware primitive: a counter, a comparator, and an output pin.

Why Servo Projects Keep Drifting into Audio

Anyone who has built a pan-tilt rig with micro servos eventually notices the buzzing. That buzz is the servo's motor being driven by a PWM signal, and the mechanical resonance of the gear train turns it into an audible tone. Some makers lean into this. There are projects that play melodies by commanding servo positions at audio rates, effectively turning the motor into a crude speaker. The results are lo-fi, gritty, and unmistakably physical. It is PWM audio in its most tactile form, and it is a useful reminder that the boundary between control signals and audio signals is thinner than most curricula suggest.

Techniques for Clean PWM Audio

Oversampling and Noise Shaping

The single biggest improvement you can make to PWM audio is to push quantization noise out of the audible band. Oversampling raises the effective sample rate, and noise shaping redistributes the error so that it concentrates at higher frequencies where a simple filter can remove it. A second-order noise shaper running on a 200 kHz PWM carrier can deliver effective resolution approaching 12 bits in the audio band, even though the raw PWM resolution is only 8 bits.

Differential Output and Bridge-Tied Loads

Driving a speaker differentially from two complementary PWM outputs doubles the effective voltage swing and cancels even-order harmonics. This is standard practice in Class-D amplifier design, and it applies equally to microcontroller-driven PWM audio. The cost is two pins and a more complex filter, but the payoff in headroom and distortion is substantial.

Filter Topology Matters More Than You Think

A single RC low-pass is the default choice, but it is rarely sufficient. A second-order Butterworth filter with a cutoff around 20 kHz will attenuate the carrier far more effectively while preserving the passband. For micro servo-adjacent projects where board space is tight, an LC filter using a small ferrite bead and a ceramic capacitor can outperform a bulky RC network. The key is to simulate the filter with the actual speaker impedance, not a idealized 8-ohm resistor.

Tools of the Trade

Microcontroller Peripherals

The STM32 family offers some of the most flexible PWM peripherals for audio. Timers with complementary outputs, dead-time insertion, and DMA-driven duty cycle updates make it possible to stream audio from a buffer with almost no CPU intervention. The ESP32 is another favorite, with its LEDC peripheral and I2S interface allowing hybrid approaches where PWM handles low-frequency content and I2S handles the rest.

Software Libraries and Frameworks

On the software side, the Arduino ecosystem has several PWM audio libraries, though their quality varies widely. For serious work, the Teensy Audio Library is hard to beat. It provides a graph-based audio processing framework that runs on top of the Teensy's PWM and I2S outputs, and it includes filters, oscillators, and effects that can be chained together with minimal code. For those working in Python, the pigpio library on Raspberry Pi offers hardware-timed PWM with microsecond resolution, which is more than adequate for servo control and surprisingly usable for audio-rate experiments.

Measurement and Debugging

You cannot improve what you cannot see. A cheap USB oscilloscope and a spectrum analyzer app will reveal more about your PWM audio quality than any amount of theorizing. Look for the carrier fundamental and its harmonics, and check whether your reconstruction filter is actually attenuating them. If you see spurs in the audio band, your filter is not doing its job, or your PWM timer is jittering.

When PWM Is the Wrong Choice

PWM audio has real limits. It is inherently noisy, it demands careful analog design, and it scales poorly to high channel counts. If you need studio-quality audio, use a dedicated DAC or an I2S codec. If you need precise motor control with minimal acoustic noise, consider a servo driver with a higher PWM frequency or a stepper motor instead. The micro servo motor is a wonderful entry point into PWM, but it is not the final word.

A Practical Starting Point

If you want to explore PWM audio with a micro servo motor as your gateway, start small. Generate a 440 Hz sine wave using a timer interrupt and a lookup table, feed it to an RC filter, and listen. Then add a second-order filter, then noise shaping, then a differential output. Each step will teach you something about the relationship between time, frequency, and perception. The tools are cheap, the theory is well documented, and the results are immediate. That combination is rare in audio engineering, and it is exactly why PWM refuses to fade away.

Copyright Statement:

Author: Micro Servo Motor

Link: https://microservomotor.com/pulse-width-modulation-pwm-control/pwm-audio-signal-processing-techniques-tools.htm

Source: Micro Servo Motor

The copyright of this article belongs to the author. Reproduction is not allowed without permission.

About Us

Lucas Bennett avatar
Lucas Bennett
Welcome to my blog!

Tags