PWM is the bridge between digital logic and analog behavior.
It allows a microcontroller to simulate analog voltage or power control using only digital ON/OFF switching.
What is PWM?
PWM stands for Pulse Width Modulation.
It is a technique where a digital signal switches between:
- HIGH (Vcc)
- LOW (0V)
at a fixed frequency, but with a variable ON-time.
Instead of changing voltage level like DAC, we change: Width of the HIGH pulse
Basic PWM Signal:
Voltage
^
| ____ ____ ____
| | | | | | |
|_____| |______| |______| |____ --> Time
<----T------>
Where:
- T = Period
- Ton = ON time
- Toff = OFF time
Duty Cycle
The most important PWM parameter: Duty Cycle (%) = (Ton / T) × 100
Examples:
0% → Always LOW
25% → Short ON, long OFF
50% → Equal ON and OFF
75% → Long ON, short OFF
100% → Always HIGH
25% Duty
_____ _____ _____
| |______| |______| |
50% Duty
_____ _____ _____
| |____| |____| |
75% Duty
_______ _______ _______
| |____| |____| |
How PWM Creates Analog Behavior
Although PWM is digital, the load "averages" the signal.
For example:
If:
Vcc = 5V
Duty = 50%
Average voltage ≈ 2.5V
if
Vcc = 12V
Duty = 25%
Average voltage ≈ 3V
Mathematically:
Vavg = Duty × Vcc
Example
Imagine controlling LED brightness.
If you give constant 5V → LED full brightness.
If you give PWM 50% → LED looks half brightness.
Why?
Because the LED cannot respond instantly to switching — your eye averages it.
MCU ----> PWM ----> Resistor ----> LED
+5V
|
|
[MCU PWM Pin] ----[R]---->|---- GND
DC Motor Speed Control
Motor speed depends on average voltage.
MCU PWM → MOSFET → Motor → GND
+12V
|
|
[Motor]
|
+----- Drain
MOSFET
+----- Source
|
GND
Gate <---- PWM from MCU
Higher duty → more average voltage → higher speed.
PWM in Hardware
PWM is generated by a Timer + Compare Register.
Timer Counter:
0 → 1 → 2 → 3 → ... → MAX → 0 → repeat
When:
Counter < Compare Value → Output HIGH
Counter >= Compare Value → Output LOW
Counter: 0 1 2 3 4 5 6 7 8 9 10 (reset)
Compare = 4
Output:
HIGH when counter < 4
LOW when counter ≥ 4
Waveform:
_____ _____ _____
| |_____| |_____| |
+----------------+
Clock --------->| Prescaler |
+----------------+
|
v
+----------------+
| Counter (CNT) |
+----------------+
|
v
+----------------+
| Compare (CCR) |
+----------------+
|
v
+----------------+
| Output Control |
+----------------+
|
PWM Pin
PWM Frequency
Frequency depends on:
PWM Frequency = Timer Clock / (Prescaler × Period)
Where:
- Timer Clock → MCU clock
- Prescaler → divides clock
- Period → auto-reload register (ARR)
Example:
MCU Clock = 72 MHz
Prescaler = 72
Period = 1000
PWM = 72,000,000 / (72 × 1000)
PWM = 1000 Hz
PWM Modes
There are two main modes:
- Edge-Aligned PWM
- Center-Aligned PWM
Edge-Aligned PWM
Counter counts up only.
0 → MAX → 0
Used in most simple applications.
Center-Aligned PWM
Counter counts:
0 → MAX → 0 → MAX → 0
/\ /\ /\ /\
\ / \ / \ / \
Better for:
- Motor control
- Power electronics
- Lower EMI
PWM Filtering
If you add:
PWM → RC Filter → Analog Output
PWM ---[R]----+-----> Analog
|
[C]
|
GND
Now you created a simple DAC.
Used in low-cost systems without real DAC hardware.