0
0
Arduinoprogramming~10 mins

Timer interrupts with TimerOne library in Arduino - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Timer interrupts with TimerOne library
Setup TimerOne
Set Timer Period
Attach Interrupt Function
Timer Counts
Timer Reaches Period
Interrupt Triggered
Interrupt Service Routine Runs
Main Loop Continues
Back to Timer Counts
The TimerOne library sets a timer period and attaches an interrupt function that runs automatically when the timer reaches the set period, while the main program continues running.
Execution Sample
Arduino
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Timer1.initialize(1000000); // 1 second
  Timer1.attachInterrupt(blinkLED);
}

void loop() {
  // main code runs here
}

void blinkLED() {
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
This code sets a 1-second timer interrupt that toggles the built-in LED every second while the main loop runs continuously.
Execution Table
StepTimer Count (microseconds)ConditionActionOutput
10Timer startedTimer counting beginsNo output
2500000Timer < 1000000Continue countingNo output
31000000Timer == 1000000Interrupt triggeredblinkLED() called, LED toggled
40Timer reset after interruptTimer counting restartsNo output
5500000Timer < 1000000Continue countingNo output
61000000Timer == 1000000Interrupt triggeredblinkLED() called, LED toggled
7............
ExitN/AProgram runs indefinitelyNo exitNo exit
💡 Timer interrupts run repeatedly; main loop runs forever unless stopped
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
Timer Count (microseconds)0500000100000005000001000000
LED StateOFFOFFONOFFOFFON
Key Moments - 3 Insights
Why does the LED toggle even though the main loop does not change it?
Because the interrupt service routine blinkLED() runs automatically every time the timer reaches the set period, toggling the LED independently of the main loop (see execution_table steps 3 and 6).
Does the main loop stop when the interrupt runs?
No, the main loop continues running normally; the interrupt temporarily pauses the main code to run blinkLED() and then returns (see concept_flow).
Why does the timer count reset after reaching the period?
The timer automatically resets to zero after triggering the interrupt to start counting for the next period (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the LED state after the first interrupt triggers at step 3?
AOFF
BON
CBlinking rapidly
DNo change
💡 Hint
Check the 'LED State' row in variable_tracker after step 3
At which timer count does the interrupt trigger according to the execution table?
A500000 microseconds
B0 microseconds
C1000000 microseconds
D1500000 microseconds
💡 Hint
Look at the 'Condition' and 'Action' columns in execution_table rows 3 and 6
If the timer period is changed to 500000 microseconds, how would the execution table change?
AInterrupt triggers at 500000 microseconds instead of 1000000
BInterrupt triggers at 1000000 microseconds as before
CTimer never triggers interrupt
DLED toggles twice as slow
💡 Hint
Timer period controls when interrupt triggers; see concept_flow and execution_table
Concept Snapshot
TimerOne library lets you set a timer period in microseconds.
Attach an interrupt function that runs automatically when timer reaches that period.
The timer resets and counts again, triggering interrupts repeatedly.
Main program runs normally while interrupts toggle or update things.
Use initialize() to set period and attachInterrupt() to set ISR.
Full Transcript
This visual execution shows how the TimerOne library works on Arduino. First, the timer is set up with a period (like 1 second). Then an interrupt function is attached. The timer counts microseconds. When it reaches the set period, it triggers the interrupt. The interrupt runs the function blinkLED, which toggles the LED state. After the interrupt, the timer resets and counts again. Meanwhile, the main loop keeps running without stopping. The LED toggles every time the interrupt runs, showing how interrupts let you do things in the background automatically.