Home / Learn Embedded C / What Is PWM?
What Is PWM?
Pulse Width Modulation — simulating an analog-like output using a fast digital on/off signal.
How to think about this
Definition
PWM (Pulse Width Modulation) rapidly switches a digital output on and off at a fixed frequency, varying the fraction of time it spends HIGH (the "duty cycle") to simulate an analog-like average voltage.
Why you need this
Many things a microcontroller needs to control in a graded, analog-like way (LED brightness, motor speed, servo position) can be driven from simple digital output pins via PWM, without needing a true analog output.
How it works
A duty cycle of 100% looks fully on, 0% looks fully off, and 50% looks "half" on — the receiving device or human eye/ear perceives the time-average of the fast-switching signal rather than the individual pulses.
Where you'll see this
- LED dimming
- Motor speed control
- Servo positioning
Try it
PWM_WRITE in this sandbox takes a duty value out of 255, mirroring the common 8-bit PWM convention on many boards.
Try it: computing duty cycle percentage from a raw PWM value
Most PWM peripherals accept a raw count out of some maximum (here 255), and converting to a percentage is just simple integer math.
Test what you just read
1. What does PWM "duty cycle" refer to?
Duty cycle is the percentage of each fixed-length cycle that the signal is driven HIGH, which determines the time-averaged effective level.