You want to modify your Raspberry Pi LED toggle program so the LED only toggles when the button is pressed and released (not multiple toggles if held down). Which approach is best?
hard
A. Remove the pull-up resistor from the button pin
B. Increase the delay after toggling LED to 5 seconds
C. Use a while loop inside the main loop to wait until button is released
D. Add a variable to track previous button state and toggle LED only on state change from released to pressed
Solution
Step 1: Understand button press and hold behavior
Holding the button causes the input to stay low, triggering multiple toggles if not tracked.
Step 2: Use previous state tracking
By saving the last button state, the program can detect the transition from released to pressed and toggle LED only once per press.
Final Answer:
Add a variable to track previous button state and toggle LED only on state change from released to pressed -> Option D
Quick Check:
Track button state changes to toggle once = C [OK]
Hint: Track previous button state to toggle once per press [OK]
Common Mistakes:
Using long delays instead of state tracking
Removing pull-up resistor causing unstable input
Busy waiting inside loops causing CPU load
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.