C1221 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C12 · Lesson 21 · PWM for LEDs and motors

Analog-looking control from a digital pin

A digital pin is either on or off, yet a microcontroller dims LEDs smoothly and runs motors at any speed. The trick is pulse-width modulation: switch the pin on and off thousands of times a second and vary the fraction of each period it spends on, the duty cycle. The eye averages the flicker into brightness; a motor's inductance and inertia average the pulses into a steady current and speed. The PWM peripheral does the switching in hardware, so the processor only sets a number when it wants a change.

  • A potentiometer on ADC0 sets the duty cycle of two PWM channels: the LED on P0 and the motor on P3. Move the pot slider and run again: the LED's brightness and the motor's speed follow it.
  • At 20 kHz the timeline can't draw each pulse, so it labels the lane with the PWM's frequency and duty. The rpm lane shows the motor speeding up to its new speed over a few tenths of a second.
  • Both channels share one frequency: this board's PWM, like many, has one period register for all four channels.

Predict

A motor rated for 12 V is driven from 12 V through a transistor switched by PWM at 60% duty. What average voltage does it see?

Name it

Duty and average

A pin switching between 0 and V with duty cycle D averages

V¯=DV

and an LED driven that way averages D times its full current.

The peripheral

The PWM counts from 0 to PERIOD − 1 in clock cycles and holds each channel's pin high for its first DUTY counts:

fPWM=fclkPERIOD,D=DUTYPERIOD

DUTY can only take PERIOD + 1 values, so the resolution is log2⁡(PERIOD) bits: faster PWM means coarser steps. At 20 kHz, PERIOD = 500 counts, about 9 bits.

CPWM_PERIOD = 500; PWM_DUTY(2) = 150; PWM_CTRL |= 1 << 2; (channel 2 on P2)
MicroPythonp = PWM(Pin(2), freq=20000), then p.duty_u16(d) with d from 0 to 65535

Choosing the frequency

  • LEDs: above about 100 Hz the eye sees no flicker (cameras may, up to a few kHz).
  • Motors: above about 20 kHz the windings don't whine audibly; higher frequencies waste more power in the switching transistor at each edge.
  • Brightness looks roughly logarithmic to the eye, so dimmers map a knob to duty through a curve (gamma correction), not linearly.

Worked example

What PERIOD gives 20 kHz PWM, and how many bits of resolution does it leave?

Show the solution
  1. PERIOD. PERIOD=10720000=500counts
  2. Resolution. log2⁡500=8.97 just under 9 bits, 501 possible duty settings.
  3. At a lower frequency. At 1 kHz, PERIOD = 10,000 gives over 13 bits.

Answer PERIOD = 500, which leaves just under 9 bits of resolution.

Worked example

An LED draws 3.94 mA when on. At 30% duty, what's its average current?

Show the solution
  1. Scale by the duty. 0.3×3.94mA=1.18mA
  2. Compare with a resistor. Dimming with PWM saves power in proportion, unlike dimming with a resistor, which wastes the rest as heat.

Answer 1.18 mA on average.

Worked example

A hobby servo needs 50 Hz PWM with pulses from 1 ms to 2 ms. What PERIOD and what DUTY for the center, 1.5 ms?

Show the solution
  1. PERIOD for 50 Hz. PERIOD=10750=200000counts
  2. DUTY for the center. A 1.5 ms pulse is 15,000 counts.
  3. The usable range. 10,000 to 20,000 counts is only 5% of the period, but that's still 10,000 steps, far finer than a servo can follow.

Answer PERIOD = 200,000 and DUTY = 15,000.

Check

A pin switches between 0 and 5 V at 15% duty. What's the average voltage?

Write units the way you normally would: 1.5 V, 1500 mV and 0.0015 kV are the same answer.

Check

What PWM PERIOD, in clock cycles, gives 20000 Hz?

Check

With PERIOD = 500, how many bits of duty resolution does the PWM have?

Check

With PERIOD = 1000, what DUTY value gives 12.5%?

Check

MicroPython's duty_u16(d) takes d from 0 to 65535. What duty cycle is duty_u16(32768)?

Check

A motor driven by 2 kHz PWM whines audibly. What change fixes it?

Check

Drive the LED on P1 with PWM at 5000 Hz and 25% duty.

MicroPython on the course's board: machine (Pin, PWM, ADC, DAC, UART, I2C, SPI, Timer, WDT) and time. Each call into the hardware takes about 40 µs. Tab indents; press Esc and then Tab to move on.

Check

Run the motor on P2 at D% (20%) of full speed using PWM channel 2 at F Hz (4000 Hz), writing the PWM registers directly.

C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.

Check

PWM on the 10 MHz board runs at 1000 Hz.

(a)

What PERIOD?

(b)

How many bits of duty resolution?

Check

PWM with PERIOD = 10000 drives a load that draws 20 mA when on, at 10% duty.

(a)

What DUTY value?

(b)

What average current?

Write units the way you normally would: 1.5 mA, 1500 µA and 0.0015 A are the same answer.

Explain why

Why does PWM control a motor's speed efficiently, while a series resistor that drops the voltage wastes power?

Practice

Keep practicing

12 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.