TactivergeLite

Lite / Building Machines That Respond / Week 25

PWM: dimming with an on/off switch

A pin can only be fully on or fully off. So how does it dim an LED to 30% or run a motor at half speed?

About 30 min · 3 goals · Checked by automated tests · Full interactive version

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.

Predict

A 12 V supply is switched on for 25% of every period and off for 75%. What average voltage does a motor feel?

  1. 12 V
  2. 9 V
  3. 3 V
  4. 0.25 V
Guess first, then reveal

Average = 25% × 12 V = **3 V**. The voltage is never actually 3 V: it is 12 V a quarter of the time. A motor's inertia smooths that into the effect of 3 V.

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.

What the interactive shows (text version)

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.

You can also open the interactive on the full site when you have a faster connection.

Watch (optional)

Watch for: 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.

“PWM - Pulse Width Modulation (Basics, Circuit, Working, & Waveforms) Explained” by Engineering Funda (opens YouTube; streaming video uses a lot of data)

The video is optional. Everything you need is in this lesson's text.

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

Intuitive

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.

Technical

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.

Mathematical

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%.

Applied

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
  1. In one period T, the output is V_high for the on-time t_on and 0 V for the rest.

  2. Duty cycle D = t_on ÷ T (as a fraction, or × 100 as a percentage).

  3. The average over a period is the area under the wave divided by the period: (V_high × t_on + 0 × (T − t_on)) ÷ T.

  4. So V_avg = D × V_high. 40% of 12 V = 0.40 × 12 = 4.8 V.

  5. 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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

V

V

%

%

Practice 5. An LED driven by PWM at 20 Hz visibly flickers. What is the fix?

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%.

Reflect

Why is PWM more efficient than dropping the voltage across a resistor to slow a motor?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Timers: hardware that counts for youDoing several things at once (without waiting) →