📖 Scenario: You are building a simple Arduino project to detect button presses. The button can be pressed down or released, and you want to know when the button is pressed (rising edge), released (falling edge), or changed state (either pressed or released).
🎯 Goal: Create an Arduino sketch that reads a button input and prints messages when the button state rises (goes from LOW to HIGH), falls (goes from HIGH to LOW), or changes state.
📋 What You'll Learn
Use a variable called buttonPin set to pin 2
Use a variable called buttonState to read the current button state
Use a variable called lastButtonState to store the previous button state
Detect rising edge (LOW to HIGH) and print "Button pressed"
Detect falling edge (HIGH to LOW) and print "Button released"
Detect any change in button state and print "Button state changed"
💡 Why This Matters
🌍 Real World
Detecting button presses and releases is common in devices like remote controls, game controllers, and user interfaces.
💼 Career
Understanding how to detect input changes is important for embedded systems programming and hardware interface development.
Progress0 / 4 steps
1
Set up the button input pin
Create an integer variable called buttonPin and set it to 2. In the setup() function, set buttonPin as an input using pinMode(buttonPin, INPUT). Also, start serial communication at 9600 baud with Serial.begin(9600).
Arduino
Hint
Use pinMode to set the pin mode and Serial.begin(9600) to start serial communication.
2
Create variables to track button state
Create two integer variables called buttonState and lastButtonState. Initialize lastButtonState to LOW before setup(). In the loop() function, read the current button state from buttonPin using digitalRead(buttonPin) and store it in buttonState.
Arduino
Hint
Use digitalRead(buttonPin) to get the current button state.
3
Detect rising, falling, and any change in button state
In the loop() function, use if statements to detect when the button state changes compared to lastButtonState. If buttonState is HIGH and lastButtonState is LOW, print "Button pressed". If buttonState is LOW and lastButtonState is HIGH, print "Button released". If buttonState is different from lastButtonState, print "Button state changed". Finally, update lastButtonState to the current buttonState.
Arduino
Hint
Compare buttonState and lastButtonState to detect changes and print messages accordingly.
4
Print the button state changes
Add Serial.println statements inside the if blocks to print the exact messages: "Button pressed" for rising edge, "Button released" for falling edge, and "Button state changed" for any change. Run the program and observe the serial monitor output when pressing and releasing the button.
Arduino
Hint
Press and release the button to see the messages in the serial monitor.
Practice
(1/5)
1. Which interrupt trigger type activates when a signal changes from LOW to HIGH on an Arduino pin?
easy
A. RISING
B. FALLING
C. CHANGE
D. LOW
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 A
Quick Check:
LOW to HIGH = RISING [OK]
Hint: RISING means signal goes up from LOW to HIGH [OK]
Common Mistakes:
Confusing RISING with FALLING
Thinking CHANGE triggers only on RISING
Mixing LOW trigger with RISING
2. Which of the following is the correct syntax to attach an interrupt on pin 2 for a FALLING trigger in Arduino?
easy
A. attachInterrupt(2, ISR, RISING);
B. attachInterrupt(digitalPinToInterrupt(2), ISR, FALLING);
C. attachInterrupt(2, ISR, CHANGE);
D. attachInterrupt(digitalPinToInterrupt(2), ISR, LOW);
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 B
Quick Check:
Use digitalPinToInterrupt with FALLING [OK]
Hint: Use digitalPinToInterrupt(pin) with trigger type [OK]
Common Mistakes:
Using pin number directly without digitalPinToInterrupt
Mixing trigger types with wrong syntax
Using LOW trigger incorrectly
3. What will be the output on the serial monitor if the following Arduino code is run and the input pin changes from LOW to HIGH, then HIGH to LOW?