1.Why should I care?
Timers are the peripheral behind every blink, every PWM signal, every timeout and every 'millis()' clock. Knowing how they count lets you predict timing exactly instead of hoping your loop runs at the right speed.
- ◇ Explain what a hardware timer and its prescaler do.
- ◇ Calculate tick rates, ticks in a time window and the time until a counter overflows.
- ◇ Choose a prescaler for a required timing resolution and range.
2.Predict
A 16 MHz clock feeds a timer through a prescaler of 64. How many times a second does the timer count?
Guess first. Being wrong here is how the surprise works.
3.Discover
Ignore the tasks for now and use the timer readout. Scrub to 1 ms, 100 ms and 1,000 ms with a prescaler of 64, then 1,024. When does the 16-bit counter overflow for each prescaler?
Loading the interactive…
Can't use the interactive? Read a description instead
The timing scrubber includes a timer readout: with a 16 MHz clock and the chosen prescaler (1, 8, 64, 256 or 1,024), it shows the tick rate (16,000,000 ÷ prescaler), the ticks counted by the scrubbed time, and how many times a 16-bit counter (65,536 values) has overflowed.
4.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1 · a fresh version every time
A timer counts at 16 MHz divided by a prescaler of 1. How many ticks does it count in 20 ms?
5.Understand
A counter in hardware
A timer is a peripheral that adds 1 to a counter on every tick, whatever the CPU is doing. Firmware reads the counter to know how much time has passed, or asks the timer to raise a flag (or an interrupt) when it reaches a value. Because it is hardware, its timing does not wobble when the loop gets busy.
The prescaler
Counting at the full 16 MHz would fill a 16-bit counter in about 4 ms. A prescaler divides the clock first: ÷8, ÷64, ÷256, ÷1,024 are common. Dividing by 64 gives 4 µs ticks and about 0.26 s before overflow; dividing by 1,024 gives 64 µs ticks and about 4.2 s.
Overflow and wrap-around
A 16-bit counter can only hold 65,536 values (0–65,535). One tick after 65,535 it wraps back to 0: an overflow. Firmware either counts overflows to extend the range, or measures intervals with subtraction that copes with wrap-around (week 25's non-blocking pattern does this with a 32-bit millisecond clock).
Explain it another way
A timer is a turnstile clicker at a stadium gate: it counts by itself while the manager does other jobs. The prescaler is only clicking for every 64th person, so the clicker fills up more slowly.
Timers can count up to a compare value and reset (clear-on-compare), toggle a pin, capture the count when an input changes, or generate PWM. Arduino-style millis() uses a timer overflow interrupt to keep a 32-bit millisecond count.
ticks = f_clock·t/prescaler; t_overflow = 2^bits·prescaler/f_clock; resolution = prescaler/f_clock.
A washing machine's controller uses one timer for its motor PWM, another for its 1 ms system clock, and counts minutes for each wash stage from that clock.
Derivation: Ticks, prescalers and overflow
Tick rate: f_tick = f_clock ÷ prescaler. 16 MHz ÷ 64 = 250,000 ticks per second.
Ticks in a time t: ticks = f_tick × t. In 20 ms: 250,000 × 0.020 = 5,000 ticks.
Resolution (time per tick) = 1 ÷ f_tick = 4 µs.
A 16-bit counter holds 0 to 65,535 and then wraps to 0: it overflows every 65,536 ÷ f_tick seconds = 65,536 ÷ 250,000 ≈ 0.262 s.
Bigger prescaler: coarser ticks but a longer time before overflow. Choose the smallest prescaler whose range covers the longest interval you need.
Worked example 1
You need to time intervals up to 2 s with a 16-bit timer on a 16 MHz clock. Which prescaler from 1, 8, 64, 256, 1,024 should you choose?
Overflow time = 65,536 × prescaler ÷ 16,000,000. ÷256: 1.05 s (too short). ÷1,024: 4.19 s (enough), with 64 µs resolution. Choose 1,024.
Common mix-ups
- The CPU has to count time itself in a loop.
✓ Timers are hardware: they count on their own and the CPU just reads them. - Overflow means the timer broke.
✓ Overflow is normal wrap-around. Good firmware expects it and handles it.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
A timer counts at 8 MHz divided by a prescaler of 1024. How many ticks does it count in 10 ms?
Practice 3
A 16-bit timer counts at 16 MHz ÷ 256. How long until it overflows, in seconds? (Give 3 decimal places.)
Practice 4
A timer counts at 8 MHz ÷ 8. How long is one tick, in microseconds?
Practice 5
What do you gain and lose by increasing the prescaler?
7.Apply it
Plan the timers for a greenhouse controller: a 1 ms system tick, a 20 kHz fan PWM and a 10-minute logging interval. For each, choose how you would make it (prescaler, counts, or counting ticks in firmware) and show the arithmetic.
8.Reflect
Concepts: Inside a microcontroller · Real-time computing on robots · Fixed-width integers, bits and registers