Hardware timers, prescalers and millis()
About this lesson
A hardware timer is a counter that the clock drives by itself, so it keeps time while your code does other work. Follow Timer0 on an Arduino Uno: the 16 MHz clock is divided by a prescaler of 64, an 8-bit counter overflows every 1,024 µs, and an interrupt turns those overflows into the millisecond count that millis() returns. Then use millis() to blink an LED without delay(), and see why the 49.7-day wraparound does no harm. The Hardware lesson “Interrupts and I/O: how devices get the CPU's attention” explains why polling wastes time; here you see the firmware side.
A clock, ticking very fast
The chip's clock ticks 16 million times a second. An 8-bit counter fed straight from it would fill up every 16 µs. That is too fast to be useful for counting milliseconds, so we slow the ticks down first.
clock 16,000,000 ticks per second (16 MHz) one tick 1 / 16 MHz = 0.0625 µs 8-bit counts 0 .. 255, so 256 ticks fill it 256 x 0.0625 µs = 16 µs: far too short
Read the lesson step by step
A clock, ticking very fast
The chip's clock ticks 16 million times a second. An 8-bit counter fed straight from it would fill up every 16 µs. That is too fast to be useful for counting milliseconds, so we slow the ticks down first.
clock 16,000,000 ticks per second (16 MHz) one tick 1 / 16 MHz = 0.0625 µs 8-bit counts 0 .. 255, so 256 ticks fill it 256 x 0.0625 µs = 16 µs: far too shortThe prescaler divides by 64
A prescaler is a divider in front of the timer. The Arduino core sets it to 64: one tick passes for every 64 clock ticks. So the timer gets 250,000 ticks a second, one every 4 µs.
prescaler 64 (wiring.c: "set timer 0 prescale factor to 64") choices 1, 8, 64, 256, 1024 (Timer0, datasheet) 16,000,000 / 64 = 250,000 ticks per second one timer tick = 4 µsThe counter counts by itself
Each slowed tick adds 1 to TCNT0, Timer0's 8-bit counter register. This is hardware: no code runs to count, so the CPU stays free to run your program. It counts 0, 1, 2 ... up to 255.
TCNT0 Timer0 counter register, 8 bits +1 every 4 µs, done by hardware 0, 1, 2, ... 255 the CPU runs your code meanwhile255, then back to 0
After 255 the counter rolls over to 0. That is an overflow, and it sets a flag bit, TOV0. It happens every 256 × 4 µs = 1,024 µs, slightly more than one millisecond.
255 + 1 -> 0 overflow TOV0 = 1 the overflow flag period 256 x 4 µs = 1,024 µs = 1.024 msThe flag calls an interrupt
The core enabled the overflow interrupt, so the flag makes the CPU pause and run a short function, an ISR. It adds 1 to ms and 3 to fract. Hardware clears the flag, and your program carries on.
ISR interrupt service routine TOV0 -> CPU jumps to TIMER0_OVF_vect line 2 ms += 1; fract += 3; ms = 1, fract = 3 TOV0 cleared by hardware, loop() resumesCatching up the extra 24 µs
Each overflow is 1,024 µs, not 1,000. fract keeps the extra 24 µs, in steps of 8 µs, so adds 3. When it reaches 125, a whole millisecond has built up: ms gets one more. Here 41 becomes 43.
extra 1024 - 1000 = 24 µs = 3 x 8 µs limit 125 x 8 µs = 1000 µs before ms = 41, fract = 123 (overflow 41) ISR fract 123 + 3 = 126 >= 125 fract = 1, ms = 41 + 1 + 1 = 43 check 42 x 1.024 ms = 43.008 msmillis() reads the count
millis() just returns ms. ms is 4 bytes, and the ISR could change it halfway through a read, so millis() turns interrupts off for that copy. The lesson "volatile and sharing data with an ISR" explains why.
line 1 now = millis(); millis() save SREG (SREG = status register, holds the interrupt on/off bit) cli() (clear interrupts: interrupts off) m = timer0_millis 4-byte copy restore SREG interrupts back on as before return mBlink without waiting
loop() checks the time and moves on. When now - last reaches 500, it flips the LED and adds 500 to last. No delay(), so the rest of loop() keeps running between blinks.
line 2 500 - 0 >= 500 true line 3 last = 500 line 5 pin 13 HIGH, LED on next true again at 1000 ms // Arduino Uno R3: blink the LED on pin 13 every 500 ms, no delay() unsigned long last = 0; bool led = false; void setup() { pinMode(13, OUTPUT); } void loop() { unsigned long now = millis(); if (now - last >= 500) { last += 500; led = !led; digitalWrite(13, led); } // ... other work runs here, never blocked }After 49.7 days, it wraps
ms is an unsigned long, 32 bits, so after 2³² ms, about 49.7 days, it rolls back to 0, like TCNT0 does. now - last still works: unsigned subtraction wraps too, so 204 - 4,294,967,000 gives 500.
unsigned long 32 bits, 0 .. 4,294,967,295 2^32 ms = 4,294,967.296 s = 49.7 days last = 4,294,967,000 now = 204 (after the wrap) now - last = 500 (mod 2^32): still correct but: if (now >= last + 500) breaks at the wrap
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01On a 16 MHz Uno, Timer0's prescaler is 64. How long is one timer tick?
16,000,000 / 64 = 250,000 ticks per second, so one tick lasts 1 / 250,000 s = 4 µs.
02Timer0's counter is 8 bits wide. With 4 µs ticks, how often does it overflow?
An 8-bit counter has 256 values (0 to 255), so it rolls over every 256 ticks: 256 × 4 µs = 1,024 µs.
03Why does the overflow ISR keep a second variable, fract, as well as ms?
fract adds 3 (24 µs in units of 8 µs) per overflow. When it reaches 125 (1,000 µs), it drops by 125 and ms gets one extra, so ms stays equal to the true elapsed milliseconds.
04Which test keeps working when millis() wraps back to 0?
Unsigned subtraction wraps too: just after the wrap, 204 - 4,294,967,000 gives 500 in 32-bit unsigned maths. last + 500 can wrap past 0 first and make the >= test true too early.
05You want an interrupt exactly once a second from Timer1 on a 16 MHz Uno, with a prescaler of 1024 in CTC mode. What should OCR1A be?
16,000,000 / 1024 = 15,625 ticks per second. The counter goes 0, 1, ... 15624 and then clears, which is 15,625 ticks, so OCR1A = 15624 gives one match per second.
Glossary
- Hardware timer
- A counter register inside the microcontroller that a clock steps on its own, without any code running.
- Prescaler
- A divider in front of a timer: it passes one tick for every N clock ticks, so the timer counts more slowly.
- TCNT0
- Timer0's 8-bit counter register on the ATmega328P. It counts from 0 to 255 and then rolls over.
- Overflow
- When a counter passes its largest value and rolls back to 0. On Timer0 it sets the TOV0 flag.
- ISR (interrupt service routine)
- A short function the CPU runs when an interrupt happens, before going back to the code it paused.
- millis()
- An Arduino function that returns the milliseconds since the program started, as an unsigned long. It wraps after about 49.7 days.
- CTC mode
- Clear Timer on Compare match: the counter resets to 0 when it equals a compare register, such as OCR1A, giving an exact period.
Notes and sources
- The cards rename the core's timer0_millis and timer0_fract to ms and fract, and fold its MILLIS_INC (1), FRACT_INC (3) and FRACT_MAX (125) into plain numbers. The real ISR also counts overflows for micros().
- Because ms grows by 2 about once every 41.7 overflows, some millis() values are skipped. micros() reads TCNT0 too, so it has a resolution of 4 µs on a 16 MHz board.
- Timer0 belongs to millis(), so for your own timing use Timer1, a 16-bit timer. In CTC mode it clears when it matches OCR1A: with prescaler 1024 it ticks 15,625 times a second, so OCR1A = 15624 gives exactly one interrupt per second.
- Other chips work the same way. ESP32 general-purpose timers are usually at least 54 bits wide, with a selectable tick rate, and ARM Cortex-M chips have SysTick, a timer that counts clock cycles down from a reload value.
- Arduino AVR core: wiring.c (Timer0 setup, overflow ISR, millis())
- Microchip ATmega328P datasheet (7810D): 8-bit Timer/Counter0 and 16-bit Timer/Counter1
- Arduino language reference: millis() (source)
- Arduino language reference: micros() (source)
- Arduino example: BlinkWithoutDelay
- Arduino Uno Rev3 schematic (16 MHz resonator for the ATmega328P)
- Espressif ESP-IDF: General Purpose Timer (GPTimer)
- J. W. Valvano, Embedded Systems: Shape the World, ch. 10 (SysTick timer)
Quiz Unlocks after the last step.