1.Why should I care?
Pulse-width modulation is how almost every digital system controls power: LED brightness, motor speed, heater power, even the sound in cheap speakers. It wastes very little energy because the switch is always fully on or fully off.
- ◇ Define duty cycle and calculate it from the high time and the period.
- ◇ Calculate the average voltage of a PWM signal from the duty cycle and the supply.
- ◇ Explain why the PWM frequency must be fast enough for the load.
2.Discover
With a 12 V supply, find the duty cycle that gives 6 V average, then 3 V. Watch the motor's model speed. Then drop to 50 Hz and read what happens to an LED and a motor.
Loading the interactive…
Can't use the interactive? Read a description instead
The PWM visualiser draws three periods of a square wave between 0 V and the chosen supply. The wave is high for the duty-cycle fraction of each period; a dashed line marks the average, duty × supply. A simplified motor's speed is proportional to that average (no movement below 0.8 V), and an LED swatch shows brightness rising with duty. At 50 Hz the readout warns of flicker; at 20 kHz it notes the switching is above most people's hearing.
3.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1 · a fresh version every time
A PWM output switches between 0 V and 9 V with a duty cycle of 0%. What is the average voltage, in volts?
4.Predict
A 12 V supply is switched on for 25% of every period and off for 75%. What average voltage does a motor feel?
Guess first. Being wrong here is how the surprise works.
5.Understand
Duty cycle
PWM switches an output on and off many times a second. The period is one on-plus-off cycle; the duty cycle is the fraction of the period spent on. 2 ms on in a 10 ms period is 20% duty. 0% is always off; 100% is always on.
Average voltage
Loads that respond slowly (a motor's spinning mass, an LED seen by your eye, a heater's thermal mass) react to the average: V_avg = duty × V_high. On a 5 V pin, 60% duty acts like 3 V. Because the switch is either fully on (tiny voltage across it) or fully off (no current), very little power is wasted as heat.
Fast enough, but not too fast
The PWM frequency must be fast compared with the load. At 50 Hz an LED flickers and a motor can judder. LEDs are typically driven at hundreds of hertz or more; motors at a few kilohertz to about 20 kHz, the upper end being above most people's hearing so the whine disappears. Too fast and the switching itself starts to waste energy in the driver.
Explain it another way
Flick a light switch on and off very fast and the room looks dimmer. Keep it on longer in each flick and it looks brighter.
Timers generate PWM in hardware: the counter runs from 0 to TOP (setting the period) and the pin flips when it passes a compare value (setting the duty). Resolution is TOP + 1 steps of duty. Motor drivers add a freewheeling path so current keeps flowing smoothly during the off-time.
D = t_on/T; V_avg = D·V_high; f = 1/T. With a timer TOP of 255, duty steps are 1/256 ≈ 0.39%.
A drone's speed controllers, a laptop's screen backlight and an electric toothbrush all use PWM, at frequencies chosen so you neither see nor hear the switching.
Derivation: Average voltage of a PWM wave
In one period T, the output is V_high for the on-time t_on and 0 V for the rest.
Duty cycle D = t_on ÷ T (as a fraction, or × 100 as a percentage).
The average over a period is the area under the wave divided by the period: (V_high × t_on + 0 × (T − t_on)) ÷ T.
So V_avg = D × V_high. 40% of 12 V = 0.40 × 12 = 4.8 V.
The frequency f = 1 ÷ T does not appear: it changes how smooth the effect is, not the average.
Worked example 1
A 1 kHz PWM signal is high for 0.35 ms each period. Duty cycle and average on a 5 V pin?
T = 1 ÷ 1,000 = 1 ms. D = 0.35 ÷ 1 = 35%. V_avg = 0.35 × 5 = 1.75 V.
Common mix-ups
- PWM makes the pin output a lower voltage.
✓ The pin is always at 0 V or at the supply. Only the average, felt by a slow load, is lower. - Higher PWM frequency gives a higher average voltage.
✓ Only the duty cycle sets the average. Frequency sets how smooth the effect is.
6.Watch
→ Watch for the definitions of period, on-time and duty cycle, and check that the average-voltage formula matches this lesson's.
A lecture-style explanation of PWM basics, circuits and waveforms, a step more formal than this lesson.
Length: see YouTube · captions on YouTube · source
Nothing loads from YouTube until you press play.
The video is optional: everything you need is also in the lesson text and the interactive.
7.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
A PWM output switches between 0 V and 3.3 V with a duty cycle of 85%. What is the average voltage, in volts?
Practice 3 · a fresh version every time
A PWM signal has a period of 2 ms and stays high for 1 ms each period. What is the duty cycle, as a percentage?
Practice 4
You want an average of 4.5 V from a 12 V supply. What duty cycle is needed, in percent?
Practice 5
An LED driven by PWM at 20 Hz visibly flickers. What is the fix?
8.Apply it
A greenhouse fan runs from 12 V and should have three speeds: 33%, 66% and 100% of full voltage. Give the duty cycles and the average voltages, choose a PWM frequency with a reason, and write a Python function duty_for(volts) with a check that clamps the result to 0–100%.
9.Reflect
Concepts: Timers and pulse-width modulation · Motors, servos and steppers