Bird
Raised Fist0
Arduinoprogramming~20 mins

attachInterrupt() function in Arduino - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Interrupt Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Arduino sketch using attachInterrupt()?

Consider the following Arduino code snippet. What will be printed on the Serial Monitor after pressing the button connected to pin 2?

Arduino
volatile int count = 0;

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), incrementCount, FALLING);
}

void loop() {
  Serial.println(count);
  delay(1000);
}

void incrementCount() {
  count++;
}
AThe Serial Monitor prints random numbers due to interrupt noise.
BThe Serial Monitor prints increasing numbers every second starting from 0, incrementing each time the button is pressed.
CThe Serial Monitor prints 0 repeatedly and never changes.
DThe Arduino resets continuously causing no output.
Attempts:
2 left
💡 Hint

Think about what happens when the button is pressed and how the interrupt updates the variable.

🧠 Conceptual
intermediate
1:30remaining
Which statement about attachInterrupt() is true?

Choose the correct statement about the attachInterrupt() function in Arduino.

A<code>attachInterrupt()</code> allows you to specify a function to run when a pin changes state without polling in <code>loop()</code>.
B<code>attachInterrupt()</code> can only be used with pins 0 and 1 on all Arduino boards.
CThe interrupt service routine (ISR) can safely use <code>delay()</code> to wait inside the function.
DYou must call <code>detachInterrupt()</code> before using <code>attachInterrupt()</code> again on the same pin.
Attempts:
2 left
💡 Hint

Think about what interrupts do compared to normal code flow.

🔧 Debug
advanced
2:00remaining
Why does this attachInterrupt() code cause a compile error?

Identify the cause of the compile error in this Arduino code snippet:

Arduino
void setup() {
  attachInterrupt(2, blinkLED(), RISING);
}

void blinkLED() {
  digitalWrite(13, HIGH);
  delay(100);
  digitalWrite(13, LOW);
}
AThe function blinkLED() must return an int, not void.
BThe pin number 2 is invalid for attachInterrupt() on Arduino Uno.
CThe ISR blinkLED() uses delay(), which is not allowed inside interrupts.
DThe interrupt function blinkLED() is called immediately instead of passing its name as a pointer.
Attempts:
2 left
💡 Hint

Look carefully at how the function is passed to attachInterrupt().

📝 Syntax
advanced
1:30remaining
Which option correctly uses attachInterrupt() to trigger on a falling edge?

Choose the correct syntax to attach an interrupt on pin 3 that triggers on a falling edge and calls the function handleInterrupt.

AattachInterrupt(3, handleInterrupt, FALLING);
BattachInterrupt(3, handleInterrupt(), FALLING);
CattachInterrupt(digitalPinToInterrupt(3), handleInterrupt, FALLING);
DattachInterrupt(digitalPinToInterrupt(3), handleInterrupt(), FALLING);
Attempts:
2 left
💡 Hint

Remember how to convert a pin number to an interrupt number and how to pass the function.

🚀 Application
expert
2:30remaining
How many times will the variable 'count' increment in this scenario?

Given this Arduino code, if a noisy button connected to pin 2 is pressed once causing multiple rapid FALLING edges, how many times will count increment after the press?

Arduino
volatile int count = 0;

void setup() {
  pinMode(2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), incrementCount, FALLING);
}

void loop() {}

void incrementCount() {
  count++;
}
AMultiple times, because each FALLING edge triggers the interrupt, including noise.
BCount will increment randomly due to debounce hardware.
CZero times, because interrupts are disabled by default.
DExactly 1 time, because the interrupt triggers only once per press.
Attempts:
2 left
💡 Hint

Think about how mechanical button noise affects interrupts triggered on edges.

Practice

(1/5)
1. What is the main purpose of the attachInterrupt() function in Arduino?
easy
A. To start the main program loop
B. To set the baud rate for serial communication
C. To run a function automatically when a specific pin changes state
D. To declare a variable as volatile

Solution

  1. Step 1: Understand the role of attachInterrupt()

    The function attachInterrupt() is used to make Arduino respond immediately when a pin changes, without waiting for the main loop.
  2. Step 2: Identify the correct purpose

    It triggers a specific function automatically on pin state changes, unlike other options which are unrelated to interrupts.
  3. Final Answer:

    To run a function automatically when a specific pin changes state -> Option C
  4. Quick Check:

    attachInterrupt() triggers functions on pin changes [OK]
Hint: attachInterrupt() runs code on pin change instantly [OK]
Common Mistakes:
  • Confusing attachInterrupt() with serial communication setup
  • Thinking it starts the main loop
  • Mixing it with variable declarations
2. Which of the following is the correct syntax to attach an interrupt on pin 2 that triggers on a rising edge and calls the function handleInterrupt?
easy
A. attachInterrupt(2, handleInterrupt, RISING);
B. attachInterrupt(digitalPinToInterrupt(2), handleInterrupt, RISING);
C. attachInterrupt(handleInterrupt, 2, RISING);
D. attachInterrupt(2, RISING, handleInterrupt);

Solution

  1. Step 1: Recall correct attachInterrupt() syntax

    The correct syntax uses digitalPinToInterrupt(pin) to convert the pin number to an interrupt number.
  2. Step 2: Match parameters order

    The order is: interrupt number, function name, mode (like RISING). Only attachInterrupt(digitalPinToInterrupt(2), handleInterrupt, RISING); follows this correctly.
  3. Final Answer:

    attachInterrupt(digitalPinToInterrupt(2), handleInterrupt, RISING); -> Option B
  4. Quick Check:

    Use digitalPinToInterrupt(pin) for correct interrupt number [OK]
Hint: Use digitalPinToInterrupt(pin) for correct interrupt number [OK]
Common Mistakes:
  • Passing pin number directly without digitalPinToInterrupt()
  • Mixing parameter order
  • Using pin number instead of interrupt number
3. What will be the output on the serial monitor after pressing the button connected to pin 2 once, given this code?
volatile int count = 0;

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), incrementCount, FALLING);
}

void loop() {
  Serial.println(count);
  delay(500);
}

void incrementCount() {
  count++;
}
medium
A. The count will increase continuously without button presses
B. The count will stay 0 because interrupts are not enabled
C. The program will crash due to volatile misuse
D. The count will increase by 1 each time the button is pressed

Solution

  1. Step 1: Analyze interrupt setup and button wiring

    The button is connected to pin 2 with INPUT_PULLUP, so pressing it pulls pin 2 LOW, triggering FALLING edge interrupt.
  2. Step 2: Understand interrupt effect on count

    Each button press triggers incrementCount(), increasing count by 1, which is printed repeatedly in loop.
  3. Final Answer:

    The count will increase by 1 each time the button is pressed -> Option D
  4. Quick Check:

    Interrupt increments count on button press [OK]
Hint: FALLING triggers on button press with INPUT_PULLUP [OK]
Common Mistakes:
  • Ignoring INPUT_PULLUP logic and edge type
  • Assuming count won't change without delay
  • Thinking volatile causes crashes
4. Identify the error in this code snippet using attachInterrupt():
volatile int flag = 0;

void setup() {
  pinMode(3, INPUT);
  attachInterrupt(3, interruptHandler, CHANGE);
}

void loop() {
  if (flag == 1) {
    Serial.println("Interrupt triggered");
    flag = 0;
  }
}

void interruptHandler() {
  flag = 1;
}
medium
A. Pin number 3 is used directly instead of digitalPinToInterrupt(3)
B. Interrupt mode CHANGE is invalid
C. Flag variable should not be volatile
D. Interrupt handler function must return int

Solution

  1. Step 1: Check attachInterrupt() parameter usage

    The first parameter must be the interrupt number, obtained by digitalPinToInterrupt(pin), not the pin number directly.
  2. Step 2: Verify other parts

    CHANGE is a valid mode, flag should be volatile, and interrupt handlers return void, so no other errors.
  3. Final Answer:

    Pin number 3 is used directly instead of digitalPinToInterrupt(3) -> Option A
  4. Quick Check:

    Use digitalPinToInterrupt(pin) for correct interrupt number [OK]
Hint: Always convert pin to interrupt number with digitalPinToInterrupt() [OK]
Common Mistakes:
  • Passing pin number directly to attachInterrupt()
  • Misunderstanding valid interrupt modes
  • Incorrect return type for interrupt handler
5. You want to count how many times a sensor connected to pin 4 triggers a LOW signal, but only if the sensor stays LOW for at least 50 milliseconds to avoid false triggers. Which approach using attachInterrupt() is best?
hard
A. Use attachInterrupt() on CHANGE, record the time in ISR, and check duration in loop()
B. Use attachInterrupt() on FALLING edge and increment count immediately without delay
C. Use attachInterrupt() on RISING edge and increment count immediately
D. Use attachInterrupt() on FALLING edge and in the ISR check if LOW lasts 50ms using delay()

Solution

  1. Step 1: Understand the need for debouncing or filtering

    To avoid false triggers, you must check if the LOW state lasts at least 50ms, which requires timing logic outside the interrupt.
  2. Step 2: Evaluate options for timing and counting

    Using CHANGE interrupt and recording time in ISR, then checking duration in loop() allows safe timing without delay() inside ISR, which is not allowed.
  3. Step 3: Reject unsafe or incorrect options

    Using delay() inside the ISR is bad practice; incrementing the count immediately on an edge without checking duration ignores the 50ms requirement.
  4. Final Answer:

    Use attachInterrupt() on CHANGE, record the time in ISR, and check duration in loop() -> Option A
  5. Quick Check:

    Record time in ISR, check duration in loop for debounce [OK]
Hint: Never use delay() in ISR; check timing in main loop [OK]
Common Mistakes:
  • Using delay() inside interrupt routine
  • Ignoring debounce timing
  • Incrementing count immediately on edge
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard