These triggers help your Arduino notice when a button or sensor changes from off to on, on to off, or just changes state. This way, your program can react right away.
Rising, falling, and change triggers in Arduino
attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);
// pin: the pin number
// ISR: the function to run when triggered
// mode: RISING, FALLING, or CHANGERISING triggers when the signal goes from LOW to HIGH.
FALLING triggers when the signal goes from HIGH to LOW.
CHANGE triggers on any change, either LOW to HIGH or HIGH to LOW.
buttonPressed function when pin 2 changes from LOW to HIGH.attachInterrupt(digitalPinToInterrupt(2), buttonPressed, RISING);buttonReleased function when pin 3 changes from HIGH to LOW.attachInterrupt(digitalPinToInterrupt(3), buttonReleased, FALLING);sensorChanged function whenever pin 4 changes state.attachInterrupt(digitalPinToInterrupt(4), sensorChanged, CHANGE);This program counts how many times a button connected to pin 2 is pressed. It uses a FALLING trigger because the button connects the pin to ground when pressed (going from HIGH to LOW). The count increases each time the button is pressed, and the number is printed every second.
#define buttonPin 2 volatile int count = 0; void setup() { pinMode(buttonPin, INPUT_PULLUP); Serial.begin(9600); attachInterrupt(digitalPinToInterrupt(buttonPin), countPresses, FALLING); } void loop() { // Just print the count every second Serial.print("Button pressed: "); Serial.print(count); Serial.println(" times"); delay(1000); } void countPresses() { count++; }
Interrupt functions (ISR) should be short and fast. Avoid using delay() or Serial.print() inside them.
Use volatile for variables shared between the ISR and main code to keep values correct.
Not all pins support interrupts on every Arduino board. Check your board's documentation.
RISING triggers when a signal goes from LOW to HIGH.
FALLING triggers when a signal goes from HIGH to LOW.
CHANGE triggers on any change in the signal.
Practice
Solution
Step 1: Understand the meaning of RISING trigger
The RISING trigger activates when the signal goes from LOW (0) to HIGH (1).Step 2: Compare with other triggers
FALLING triggers on HIGH to LOW, CHANGE triggers on any change, LOW triggers when signal is LOW.Final Answer:
RISING -> Option AQuick Check:
LOW to HIGH = RISING [OK]
- Confusing RISING with FALLING
- Thinking CHANGE triggers only on RISING
- Mixing LOW trigger with RISING
Solution
Step 1: Identify correct function usage
attachInterrupt requires pin number converted by digitalPinToInterrupt for portability.Step 2: Match trigger type FALLING
attachInterrupt(digitalPinToInterrupt(2), ISR, FALLING); uses digitalPinToInterrupt(2) and FALLING correctly.Final Answer:
attachInterrupt(digitalPinToInterrupt(2), ISR, FALLING); -> Option BQuick Check:
Use digitalPinToInterrupt with FALLING [OK]
- Using pin number directly without digitalPinToInterrupt
- Mixing trigger types with wrong syntax
- Using LOW trigger incorrectly
volatile int count = 0;
void ISR() { count++; }
void setup() {
Serial.begin(9600);
pinMode(2, INPUT);
attachInterrupt(digitalPinToInterrupt(2), ISR, CHANGE);
}
void loop() {
Serial.println(count);
delay(1000);
}Solution
Step 1: Understand CHANGE trigger behavior
CHANGE triggers on any signal change: LOW to HIGH and HIGH to LOW, so ISR runs twice.Step 2: Count increments in ISR
Each ISR call increments count by 1, so after two changes count = 2.Final Answer:
2 2 2 ... (count increments twice) -> Option CQuick Check:
CHANGE triggers twice, count = 2 [OK]
- Assuming CHANGE triggers only once
- Forgetting volatile keyword importance
- Thinking ISR cannot modify variables
void ISR() {
// interrupt code
}
void setup() {
attachInterrupt(3, ISR, RISING);
}Solution
Step 1: Check attachInterrupt pin argument
Pin number must be converted with digitalPinToInterrupt(pin) for correct interrupt mapping.Step 2: Validate other parts
ISR has correct signature (no parameters), RISING is valid, attachInterrupt is correctly called in setup.Final Answer:
Pin number 3 should be converted using digitalPinToInterrupt() -> Option DQuick Check:
Use digitalPinToInterrupt for pin numbers [OK]
- Passing raw pin number directly
- Thinking ISR needs parameters
- Calling attachInterrupt in loop()
Solution
Step 1: Understand button state changes
A button press changes signal from HIGH to LOW or LOW to HIGH depending on wiring.Step 2: Choose trigger to count all changes
CHANGE triggers on any signal change, so it counts both presses and releases.Final Answer:
CHANGE, because it counts both presses and releases -> Option AQuick Check:
Count all changes = use CHANGE trigger [OK]
- Using RISING or FALLING and missing half the events
- Confusing LOW trigger with change detection
- Not considering both press and release
Ready to prove it?
Pass the test and this pattern moves unknown
