0
0
Arduinoprogramming~10 mins

attachInterrupt() function in Arduino - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - attachInterrupt() function
Setup interrupt pin
Call attachInterrupt()
Wait for interrupt event
Interrupt triggers ISR
ISR runs quickly
Return to main program
The attachInterrupt() function sets up a pin to watch for a signal change and runs a small special function (ISR) when that happens.
Execution Sample
Arduino
volatile int count = 0;
void setup() {
  attachInterrupt(digitalPinToInterrupt(2), increment, RISING);
}
void increment() {
  count++;
}
This code increases 'count' by 1 every time pin 2 detects a rising signal.
Execution Table
StepActionPin StateInterrupt Triggered?ISR Called?count Value
1Setup startsLOWNoNo0
2attachInterrupt called on pin 2 for RISING edgeLOWNoNo0
3Pin 2 signal changes from LOW to HIGHHIGHYesYes0
4ISR 'increment' runsHIGHYesYes1
5ISR finishes, main continuesHIGHNoNo1
6Pin 2 signal stays HIGHHIGHNoNo1
7Pin 2 signal changes from HIGH to LOWLOWNoNo1
8Pin 2 signal changes from LOW to HIGH againHIGHYesYes1
9ISR 'increment' runs againHIGHYesYes2
10ISR finishes, main continuesHIGHNoNo2
💡 No more signal changes, program continues running main loop.
Variable Tracker
VariableStartAfter Step 4After Step 9Final
count0122
Key Moments - 3 Insights
Why does the ISR run only when the pin signal changes from LOW to HIGH?
Because attachInterrupt() was set with the RISING mode, it triggers the ISR only on a LOW to HIGH change, as shown in steps 3 and 8 in the execution_table.
Why must the variable 'count' be declared volatile?
Because 'count' is changed inside the ISR and read outside, declaring it volatile tells the compiler it can change anytime, preventing wrong optimizations. This is important as seen in variable_tracker.
What happens if the ISR takes too long to run?
The main program pauses during the ISR, so long ISRs can cause delays or missed interrupts. Here, the ISR just increments 'count' quickly (step 4 and 9), keeping the program responsive.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'count' after the ISR runs the second time?
A1
B0
C2
D3
💡 Hint
Check the 'count Value' column at step 9 where ISR runs again.
At which step does the interrupt trigger for the first time?
AStep 2
BStep 3
CStep 5
DStep 7
💡 Hint
Look at the 'Interrupt Triggered?' column to find the first 'Yes'.
If the attachInterrupt mode changed from RISING to FALLING, when would the ISR run?
AWhen pin changes from HIGH to LOW
BWhen pin stays HIGH
CWhen pin changes from LOW to HIGH
DWhen pin stays LOW
💡 Hint
RISING triggers on LOW to HIGH; FALLING triggers on HIGH to LOW signal changes.
Concept Snapshot
attachInterrupt(pin, ISR, mode)
- Sets an interrupt on 'pin'
- Runs ISR function when 'mode' condition met
- Modes: RISING, FALLING, CHANGE, LOW
- ISR should be short and fast
- Use volatile for variables shared with ISR
Full Transcript
The attachInterrupt() function in Arduino sets up a pin to watch for signal changes. When the specified event happens, it runs a small special function called an ISR (Interrupt Service Routine). In the example, the program watches pin 2 for a rising edge (signal going from LOW to HIGH). When this happens, the ISR 'increment' runs and increases the 'count' variable by one. The variable 'count' is declared volatile because it changes inside the ISR and is used outside. The ISR runs quickly so the main program can continue without delay. The execution table shows each step: setting up, waiting, detecting signal changes, running ISR, and updating 'count'. This helps beginners see how interrupts work step-by-step.