From timing to working code.
Configure a CTC timer interrupt. Understand the numbers. Take the code to your board.
What timing do you need?
The clock must match the board's actual clock. Changing this input does not change the hardware.
Your timer configuration
The requested count exceeds this timer’s capacity
Compare prescalers
| Divider | OCRnA | ISR count | Task rate | Error | Selection |
|---|
A complete Arduino sketch
Paste into a new sketch, select your ATmega328P board, then upload. The built-in LED toggles once per task interval.
Includes setup, the interrupt function, and a nonblocking loop example.
Already have setup(), loop(), or an ISR? Merge the corresponding sections instead of pasting a second copy. The example queues events and runs the task in loop(); task execution is not guaranteed to occur exactly at the interrupt edge.
How the calculation works
Timer tick = prescaler / CPU clock. In CTC, the counter visits zero through OCRnA, so interval = (OCRnA + 1) × tick. For each valid prescaler, the calculator compares the adjacent integer tick counts and chooses the one with the smallest absolute frequency error. Error is (achievable frequency − requested frequency) / requested frequency × 100%.
“Exact” means no numerical rounding error in the nominal calculation; real clocks still have tolerance. Requests outside a prescaler's counter range are marked unavailable. Enable the optional ISR counter to divide hardware interrupts for longer intervals. It uses a 32-bit counter (up to 4,294,967,295 interrupts per task). For each prescaler, it searches hardware counts and rounds the software count to the nearest integer; ties favor a lower interrupt rate.
Timer ownership & practical limits
The generated sketch takes exclusive control of the selected timer, including its interrupt mask and PWM outputs. Do not combine it with another library using the same timer. Timer0 changes break Arduino timekeeping; Timer1 is commonly used by Servo; Timer2 is commonly used by tone(). Timer2 assumes standard synchronous Arduino startup.
Keep interrupt routines short. At high rates, servicing interrupts and running the LED task may take longer than the interval. The mathematical timer range is not a guarantee that the example can execute at that rate. First-event timing also depends on prescaler phase.