PWM
On this page
A tap that is only ever fully open or fully shut can still fill a bucket slowly: open it for one second in every four. A microcontroller pin is that tap. It can only be at 0 V or at full voltage, and PWM (pulse width modulation) switches it between the two so fast that the load feels only the average.
See it
A 5 V pin is on for half of every cycle, 1,000 times a second. You change it to 10,000 times a second, still on for half of every cycle. What happens to the average voltage?
The black line is the real voltage on the pin: it is never anywhere except 0 V and 5 V. The dashed line is the average, which is what a motor or a lamp responds to. At 1,000 cycles a second the lamp looks steady. Slowed to 2 cycles a second, you can see that it was switching all the time.
The idea
One full on and off is a cycle. The share of the cycle that the pin is on is the duty cycle, written as a percentage. The average voltage is that share of the supply.
duty as a fraction: 25% is 0.25
The number of cycles each second is the frequency. It does not change the average. It decides only whether the load can follow the switching. A lamp switched 1,000 times a second looks steady, because the eye cannot follow. A motor has weight and cannot speed up and stop 1,000 times a second, so it turns at a steady speed set by the average.
Almost nothing is wasted. A switch that is fully on or fully off turns very little power into heat, which is why PWM replaced the big resistors that once controlled motor speed.
One worked number
A 5 V pin switches 1,000 times a second and is on for 0.25 ms in each cycle. What is the duty cycle, and what average voltage does the load feel?
Show the worked solution, 3 steps
- Find the length of one cycle
1,000 cycles a second means each cycle lasts 1 ÷ 1,000 s = 1 ms.
- Find the share that is on
Duty = on time ÷ cycle time = 0.25 ms ÷ 1 ms = 0.25, which is 25%.
- Take that share of the supply
Average = 0.25 × 5 V = 1.25 V. Check it in the figure: set the slider to 25%.
The mistake to avoid
A second trap: a hobby servo is not controlled by the average. It reads the width of one pulse, from about 1.0 ms to 2.0 ms, repeated every 20 ms. That is a duty of only 5% to 10%, and the number that matters is the milliseconds.
Check yourself
A 3.3 V pin runs at a duty of 40%. What average voltage does an LED on that pin feel?
A motor on a 12 V supply runs at a duty of 60% and makes a high whine at 1,000 cycles a second. You raise the frequency to 20,000 cycles a second and keep the duty. What changes?
You connect a PWM pin at 50% directly to the analog input of another board, to give it "2.5 V". The readings on that board jump between about 0 and full scale. Why?
Where you meet it
- Motor and fan speedA fan in a cold room or an enclosure runs at the speed the duty sets, with almost no power lost in the switch.
- Lamp and screen brightnessNearly every LED lamp with a dimmer, and every phone screen backlight, is dimmed this way.
- HeatersA heater is slow, so its cycle can be seconds long. The idea is the same: on for a share of the time.
- Variable speed drivesA drive for a three phase motor builds its output from thousands of pulses a second. It is the same idea at high voltage.
- The pin is only ever fully on or fully off. PWM changes the share of the time it is on.
- Average voltage = duty × supply voltage.
- Frequency does not change the average. It must be high enough that the load cannot follow the switching.
- It is an average, not a steady voltage. Fast parts see the pulses.
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.