PAGE 112 OF 150 · MICROCONTROLLERS & LEDS
PWM Output on Microcontrollers
Hardware timers generate steady PWM signals.
LED SYMBOLTriangle points from anode to cathode; the arrows show light leaving the part.
Hardware PWM
Most microcontrollers have timers that produce PWM without using program time. You set a duty value, and the timer keeps the pin switching by itself.
- An 8-bit value runs from 0 for always off to 255 for always on, so duty is value ÷ 255.
- On classic Uno-style boards PWM pins are 3, 5, 6, 9, 10 and 11, running at roughly 490 Hz, or 980 Hz on pins 5 and 6.
- Timer settings can change the frequency if the default flickers on camera.
Pins without hardware PWM can use software PWM, at the cost of processor time.
REMEMBER
Hardware PWM runs in the background once it is set.Try it yourself
Sketch the circuit on paper first, calculate the values, then build it on a solderless breadboard using a low-voltage supply. Measure with a multimeter before you make anything permanent.
Explore LED clocks, stopwatches and electronics at cosycom.com.