Skip to content
You Start with one site
Amounts in Start with one site
Microcontrollers · Concept

PWM

One idea Beginner 5 min read
Found this useful?

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.

Lower the voltage?The pin has no half voltage. It is 0 V or it is full.Not possible on a digital pin
Add a resistor?It works, and the resistor turns the rest of the power into heat.Wasteful
Switch it very fast?On for half the time, off for half the time.This is PWM

See it

Predict first

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?

See it: one pin, switched on and off

Duty cycle
On time in each cycle
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.

Vaverage = duty × Vsupply

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

The question

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
  1. Find the length of one cycle

    1,000 cycles a second means each cycle lasts 1 ÷ 1,000 s = 1 ms.

  2. Find the share that is on

    Duty = on time ÷ cycle time = 0.25 ms ÷ 1 ms = 0.25, which is 25%.

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

PWM is not a steady voltage. A meter set to DC shows 1.25 V, because a meter averages. The pin is still jumping between 0 V and 5 V. A part that responds fast, such as an audio amplifier or the analog input of another chip, sees every pulse. If you need a truly steady voltage, pass the PWM through a low-pass filter, or use a real analog output.

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

Question 1

A 3.3 V pin runs at a duty of 40%. What average voltage does an LED on that pin feel?

Question 2

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?

Question 3

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.
Key points
  • 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.
Practice

Found this useful?

Discussion

    Comments show your name. Be kind, stay on the topic.