Pulse Width Modulation (PWM) is a technique that simulates an analog voltage level by rapidly switching a digital signal on and off at varying duty cycles. Instead of dropping excess voltage across a resistor and wasting it as heat, PWM changes the average power delivered to a load by altering the ratio of 'on' time to 'off' time, allowing highly efficient control of motors, LEDs, and heaters using simple digital logic pins.
The Core Concept: What PWM Actually Does to a Circuit
To understand PWM, think of a water valve that you snap fully open and fully closed repeatedly; the wider the open time compared to the closed time, the more water (power) fills the bucket (load). The bucket doesn't care about the rapid snapping; it only integrates the total volume of water over time. Similarly, electrical loads with mass, inductance, or thermal inertia (like DC motors, heating elements, and the human eye) integrate the rapid voltage pulses into a smooth average response.
The two defining metrics of any PWM signal are frequency (how many on/off cycles occur per second, measured in Hertz) and duty cycle (the percentage of one cycle that the signal remains HIGH).
Imagine you are driving a 12V DC cooling fan using a MOSFET controlled by a microcontroller. You set the PWM frequency to 25 kHz (above human hearing) and the duty cycle to 25%.
• Peak Voltage: 12V (when the MOSFET is ON)
• Off Voltage: 0V (when the MOSFET is OFF)
• Average Voltage: 12V × 0.25 = 3.0V
The fan's internal inductance smooths this out, and the motor spins at roughly the same speed it would if fed a steady 3.0V DC source, but your MOSFET dissipates almost zero heat because it is only ever fully ON (low resistance) or fully OFF (zero current).
Microcontroller PWM Capabilities at a Glance
Not all microcontrollers handle PWM equally. The hardware timers dictating these signals vary wildly in resolution (bit-depth) and base clock speeds, which directly impacts your ability to fine-tune frequencies and duty cycles. Below is a spec-sheet-table comparing the native PWM peripherals of the most common maker boards in 2026.
| Microcontroller Board | PWM Resolution | Default / Base Frequency | Max Hardware Channels | Timer Architecture |
|---|---|---|---|---|
| Arduino Uno (ATmega328P) | 8-bit (0-255) | 490 Hz (Pins 5,6: 980 Hz) | 6 | 3 Timers (8-bit & 16-bit) |
| ESP32 (WROOM-32 / DevKit v1) | 1 to 16-bit (Configurable) | 5,000 Hz (LEDC default) | 16 (via LEDC peripheral) | 4 High/Low Speed Groups |
| Raspberry Pi Pico (RP2040) | 16-bit (0-65535) | 125 MHz base (Divider applied) | 16 (8 Slices, A/B channels) | 8 Independent PWM Slices |
| STM32F103C8T6 (Blue Pill) | 16-bit (0-65535) | 72 MHz base (Divider applied) | 15 | 4 Advanced Timers (TIM1-4) |
Notice the massive leap in resolution from the 8-bit Arduino Uno to the 16-bit RP2040. On the Uno, an analogWrite(pin, 1) yields a 0.39% duty cycle. On the Pico, you can divide that resolution by 256, allowing for ultra-fine control of laboratory power supplies or precision laser diodes where a 0.4% step would cause thermal runaway. For deeper dives into configuring these timers, the Espressif LEDC API documentation and the Arduino analogWrite reference are the definitive starting points.
Where You Meet PWM in Practice (And What People Confuse It With)
PWM is ubiquitous in modern electronics. Here is where you will actively use it on the workbench:
- DC Motor Speed Control: Using motor drivers like the DRV8871 or L298N. PWM allows a 5V logic signal to proportionally control a 24V industrial motor's speed without massive heatsinks.
- Servo Motor Positioning: Standard RC servos ignore duty cycle percentages and instead measure the absolute width of the HIGH pulse. A 50Hz signal (20ms period) with a 1.5ms HIGH pulse centers the servo; 1.0ms moves it to 0°, and 2.0ms moves it to 180°.
- LED Dimming: Reducing an LED's voltage via analog resistance shifts its color temperature and causes uneven dimming. PWM maintains the exact forward voltage (and thus exact color) while altering perceived brightness.
- Switch-Mode Power Supplies (SMPS): Buck and boost converters use high-frequency PWM (often 100kHz to 2MHz) to control the charge cycles of inductors, stepping voltages up or down with >90% efficiency.
Beginners frequently confuse PWM average voltage with peak voltage, leading to destroyed components. If you apply a 12V PWM signal at a 10% duty cycle to a 5V-rated logic input or a 3.3V LED, your multimeter might read 1.2V DC. However, the peak voltage is still 12V. The microcontroller pin or LED will experience the full 12V spike thousands of times per second and will likely fail catastrophically. Always use a low-pass RC filter if you need true analog DC, or ensure your load can tolerate the peak voltage.
Designing with PWM: Frequency Trade-offs and Filtering
Choosing the right PWM frequency is a balancing act between acoustic noise, switching losses, and signal integrity. This is where theoretical electronics meets physical reality.
The Audible Whine Problem
If you drive a ceramic capacitor or an inductor (like those inside a DC-DC buck converter or a motor stator) with a PWM frequency between 20 Hz and 20 kHz, the physical components will vibrate due to piezoelectric effects or magnetostriction. This translates into an audible, high-pitched whine. In consumer electronics and automotive applications, the standard practice is to push the PWM frequency above 22 kHz to move it outside the human hearing range.
Switching Losses in MOSFETs
Conversely, you cannot simply set the frequency to 10 MHz to eliminate noise. Every time a MOSFET transitions from OFF to ON, it passes through a linear region where both voltage and current are non-zero, generating heat. This is known as switching loss. At 500 Hz, switching losses are negligible. At 500 kHz, switching losses can exceed conduction losses, requiring active cooling for your driver transistors. For general-purpose DC motors and high-power LED arrays, 1 kHz to 5 kHz is the practical sweet spot.
Converting PWM to True Analog DC (RC Filter)
If your project requires a true analog voltage (e.g., generating a 0-5V reference for an op-amp) but your MCU only outputs PWM, you must build a low-pass RC filter. According to fundamental timer and PWM theory, the cutoff frequency ($f_c$) of your filter should be at least one decade (10x) lower than your PWM frequency to effectively smooth the ripples.
Target PWM Frequency: 490 Hz (Arduino Uno default)
Target Cutoff Frequency ($f_c$): ~49 Hz
Formula: $f_c = \frac{1}{2 \pi R C}$
Choose Resistor ($R$): 10 kΩ
Required Capacitor ($C$): $\frac{1}{2 \times \pi \times 10000 \times 49} \approx 0.32 \mu F$
Practical choice: Use a standard 0.33 μF ceramic or film capacitor. Expect a settling time of roughly 30ms when changing duty cycles.
Frequently Asked Questions
Q: Is PWM the same as a DAC (Digital-to-Analog Converter)?
A: No. A DAC outputs a true, continuous analog voltage level. PWM outputs a digital square wave that only averages out to an analog value over time or through external filtering.
Q: Why does my ESP32 analogWrite() behave differently than my Arduino Uno?
A: The ESP32 Arduino core maps analogWrite() to its LEDC (LED Control) peripheral for backward compatibility, defaulting to an 8-bit resolution and 5,000 Hz frequency. The Uno uses hardware timers at 490 Hz. For native ESP32 control, bypass analogWrite and use the ledcSetup() and ledcWrite() functions to access 16-bit resolution.
Q: Can I use PWM to control an AC mains device?
A: Not directly with standard low-side MOSFET PWM. Controlling AC loads requires zero-cross detection and phase-angle firing (using TRIACs) or burst-fire control, which synchronizes the 'on' pulses with the 50/60Hz AC sine wave to prevent massive electromagnetic interference (EMI) and transformer saturation.






