Skip to content
University of California, Davis Mechanical and Aerospace Engineering
ARMS Laboratory
EME154 · Calculator

From timing to working code.

Configure a CTC timer interrupt. Understand the numbers. Take the code to your board.

Arduino Uno R3 / classic Nano · ATmega328P · 8- and 16-bit timers Not for Uno R4, Nano Every, or other MCU families
01 / Configure

What timing do you need?

For intervals longer than one hardware timer cycle, count multiple compare interrupts before running the task.

Auto compares every valid prescaler. Ties use the smallest prescaler for hardware-only timing. With software counting enabled, ties favor fewer interrupts to reduce CPU work.

Results and code update as you edit. This calculator uses CTC mode with OCRnA as TOP.

The clock must match the board's actual clock. Changing this input does not change the hardware.

02 / Inspect

Your timer configuration

Achievable task interval
Prescaler
Frequency error
Timer tick

Explore this setting in the simulator →
Compare prescalers
DividerOCRnAISR countTask rateErrorSelection

03 / Copy & run

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.