PWM
PWM, pulse-width modulation, controls power by switching a signal fully on and off very fast and varying how long each pulse stays on. The share of time it is on is the duty cycle, and the average voltage follows it, so 25% duty from a 5 V pin averages 1.25 V.
Vavg = duty × Vhigh
where Vhigh is the on voltage, so 25% duty from a 5 V pin averages 1.25 V.
Because the switch is always fully on or off, it wastes little power, which is why PWM dims LEDs and sets motor speeds.
What is pulse width modulation (PWM)?
Pulse-width modulation, or PWM, is a way of controlling power with a digital switch: it turns fully on and off many times a second, and the width of the on pulses sets how much power gets through.
Wider pulses deliver more on average, narrower ones less. In Figure 1, the lab’s LED dims as its duty cycle falls, glowing as brightly as the average current.
25% duty, analogWrite(64): the pin is HIGH 25% of each 2.04 ms cycle, so the LED gets 20 mA for that share of the time: 25% × 20 mA = 5 mA on average, as bright as a steady 5 mA.
What is a duty cycle?
The duty cycle is the share of each cycle that the signal is on, as a percentage: on time ÷ period.
0% is always off, 100% always on, and 25% on for a quarter of each cycle. The frequency, how many cycles a second, is separate: the same duty cycle can repeat 5 or 50,000 times a second.
What frequency should PWM be?
PWM should be fast enough that what it drives can’t follow each pulse: a few hundred hertz or more for LEDs, and often 20 kHz or more for motors so they don’t whine.
Too slow and an LED blinks or flickers, as Figure 2 shows. An Arduino Uno’s analogWrite uses about 490 Hz on most pins and 980 Hz on pins 5 and 6.
5 Hz: each pulse lasts 100 ms, long enough to see, so the LED visibly blinks instead of dimming.
What is PWM used for?
PWM is used for dimming LEDs, setting motor speeds, positioning servos, and running switching power supplies and audio amplifiers.
Its strength is efficiency: a MOSFET that is fully on or fully off wastes far less power than a resistor or transistor holding a voltage halfway. A 555 timer can make PWM without any code.
How do you turn PWM into a steady voltage?
To turn PWM into a steady voltage, pass it through a low-pass filter whose cutoff is far below the PWM frequency, so only the average gets through.
A resistor and a capacitor are enough: they smooth the pulses into a voltage of duty × the on voltage, with a little ripple left. Figure 3 smooths 490 Hz PWM with a cutoff near 1.6 Hz.
50% duty through 10 kΩ and 10 µF: the filter averages the pulses to 50% × 5 V = 2.5 V, with only 27 mV of ripple left.
How does PWM work on an Arduino?
PWM on an Arduino is set by analogWrite(pin, value), from 0 for always off to 255 for always on, so 64 gives about 25%.
On an Arduino Uno only pins 3, 5, 6, 9, 10 and 11 can do it, marked ~. The pin still only gives 5 V or 0 V; it is the switching that makes the average.
How do you calculate PWM duty cycle and timing?
To calculate PWM duty cycle, divide the analogWrite value by 255, then multiply the period by that share for the on-time: 64 ÷ 255 ≈ 25%.
Example 1: duty cycle from analogWrite
A sketch calls analogWrite(pin, 64). What duty cycle and average voltage does the pin give?
- Given
- analogWrite(pin, 64) on a 5 V Uno
- Find
- the duty cycle and average voltage
- Solution
- Duty = 64 ÷ 255 = 25.1%Average = duty × 5 V = 1.25 V
- Answer
- 25.1% duty, 1.25 V on average
Example 2: a pulse’s on-time
How long is each pulse on for PWM at 490 Hz and 50% duty?
- Given
- PWM at 490 Hz and 50% duty
- Find
- how long each pulse is on
- Solution
- Period = 1 ÷ 490 Hz = 2.04 msOn time = 50% × 2.04 ms = 1.02 ms
- Answer
- 1.02 ms on, 1.02 ms off
Summary
Key equations
- duty = ton ÷ T the duty cycle
- Vavg = duty × Vhigh the average
- T = 1 ÷ f the period
- duty = value ÷ 255 analogWrite
Key terms
- Duty cycle
- The share of time on.
- Period
- One on and off.
- Frequency
- Cycles a second.
- Flicker
- Visible pulsing at low rates.
Practice problems
-
What is the average voltage of 50% duty PWM from a 5 V pin?
50% × 5 V = 2.5 V.
-
What does the duty cycle measure?
Duty = on time ÷ period, as a percentage.
-
What duty cycle does analogWrite(pin, 128) give?
128 ÷ 255 ≈ 50%.
-
Why does PWM have to switch fast to dim an LED?
Above a few hundred hertz the eye averages the pulses into a steady, dimmer glow.
-
How do you turn PWM into a steady voltage?
An RC low-pass filter with a cutoff far below the PWM frequency keeps only the average.
FAQ
What does PWM stand for?
Pulse-width modulation: changing the width of on pulses to control the average power delivered.
Is PWM analog or digital?
The signal itself is digital, only ever fully on or off, but its average acts like an analog voltage once smoothed or felt by a slow load.
Does PWM save power?
Yes. The switch is either fully on, dropping almost no voltage, or off, carrying no current, so little power is lost compared with a resistor.
Why does my PWM-dimmed LED flicker on camera?
A camera’s shutter can catch the LED off between pulses, showing bands or flicker the eye can’t see. A higher PWM frequency helps.
What PWM frequency does a motor need?
Often 20 kHz or more, above hearing, so the motor doesn’t whine. Slower PWM works but can be noisy.
How is PWM used to control a servo?
A hobby servo reads a pulse about every 20 ms, and the pulse’s width, roughly 1 to 2 ms, sets its angle.
Can PWM control a fan?
Yes. Computer fans with four wires take a PWM signal, usually around 25 kHz, on their control wire to set their speed.