Pulse Width Modulation (PWM)
Introduction
PWM (pulse width modulation) is a trick to fake an analog result using only digital on/off pins. The MCU flips a GPIO high and low very fast. If the on-time is 25% of each cycle, a LED looks dim; a motor runs slowly; a filter turns the pulses into a smooth voltage.
Almost every MCU has hardware timers that generate PWM for you — you set duty cycle and frequency in registers (or HAL), not bit-bang in a loop. This article explains the idea, where you use it, and how it ties to Hardware architecture.
What PWM means
A PWM signal repeats a fixed period (one full on-off cycle). Duty cycle is the fraction of time the signal is HIGH:
1 2 | |
| Term | Meaning |
|---|---|
| Period | Time for one complete cycle |
| Frequency | 1 / period (e.g. 1 kHz = 1000 cycles per second) |
| Duty cycle | (on-time / period) × 100% |
In plain terms
Imagine flicking a light switch on and off faster than your eyes can see. The room looks half bright if it is on half the time. PWM is that flicker, but thousands of times per second.
Why frequency matters
| Too slow | Too fast |
|---|---|
| LED flicker visible | Wasted switching loss |
| Motor whines audibly | May be fine for many apps |
| — | Filtered analog output needs high enough freq |
Rule of thumb: > 1 kHz for LEDs in lab; > 20 kHz if you want motor control without whine; audio-class PWM uses tens of kHz.
Common uses in embedded projects
| Use | What duty cycle does |
|---|---|
| LED brightness | Higher duty → brighter |
| DC motor speed | Higher duty → faster (with driver) |
| Servo position | Special pulse width ~1–2 ms in 20 ms frame |
| Buzzer tone | Frequency sets pitch |
| DAC substitute | Low-pass filter smooths PWM to voltage |
See Basics of electronics for driving LEDs and motors safely — PWM does not remove the need for current limiting or a motor driver.
Hardware vs software PWM
| Hardware timer PWM | Software (bit-bang) | |
|---|---|---|
| CPU load | Low — peripheral runs alone | High — loop timing jitter |
| Accuracy | Stable | Affected by interrupts |
| Use | Production firmware | Quick test only |
Always prefer timer channels labeled PWM, TIMx_CHy, or LEDC on ESP32.
Videos — other ways to learn
Watch one beginner-friendly clip, then one that links PWM to MCU timers.
PWM concept
What is PWM? Pulse Width Modulation tutorial! — duty cycle and frequency in plain language.
PWM in electronics
Pulse Width Modulation (PWM) - Electronics Basics 23 — ties PWM to components and filters.
Relevant topics
Starting points
- Fade an LED with hardware PWM — sweep duty 0% → 100%.
- Measure PWM with an oscilloscope or logic analyzer — confirm frequency and duty.
- Listen to a motor at 100 Hz vs 20 kHz PWM — hear the difference.
- Read your MCU timer PWM chapter — find which pins map to which channel.
Focus points
- Do not drive motors directly from GPIO — use driver IC or transistor.
- Servo PWM is not the same as LED PWM (fixed 50 Hz frame, pulse width sets angle).
- ISR latency can jitter software PWM — use hardware.
- Scope ground clip to circuit GND when probing.
Key points
- PWM encodes analog-like levels with digital pulses and duty cycle.
- Frequency must be high enough to avoid visible flicker or audible whine.
- Hardware timers generate reliable PWM; bit-banging is for bring-up only.
- LEDs, motors, servos, and filtered DAC outputs all use PWM in student projects.