0
0
Raspberry Piprogramming~20 mins

Button with interrupt (GPIO.add_event_detect) in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GPIO Interrupt Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when the button is pressed?

Consider this Raspberry Pi Python code using GPIO interrupts. What will be printed when the button connected to GPIO pin 17 is pressed?

Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)


def button_pressed(channel):
    print("Button pressed!")

GPIO.add_event_detect(17, GPIO.FALLING, callback=button_pressed, bouncetime=200)

try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    GPIO.cleanup()
AButton pressed! (printed once per press)
BNo output, program runs silently
CButton pressed! printed repeatedly without pressing
DSyntaxError due to missing colon
Attempts:
2 left
💡 Hint

Think about what GPIO.add_event_detect does when the button is pressed.

Predict Output
intermediate
2:00remaining
What error occurs if callback function has wrong parameters?

What error will this code produce when the button is pressed?

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def button_pressed():
    print("Pressed")

GPIO.add_event_detect(17, GPIO.FALLING, callback=button_pressed)
ANo error, prints 'Pressed' on button press
BTypeError: button_pressed() takes 0 positional arguments but 1 was given
CSyntaxError: missing colon in function definition
DRuntimeError: GPIO pin not set up
Attempts:
2 left
💡 Hint

Callback functions for GPIO interrupts receive the channel number as an argument.

🔧 Debug
advanced
2:00remaining
Why does the callback run multiple times on one press?

This code prints "Pressed" multiple times for a single button press. What is the likely cause?

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def button_pressed(channel):
    print("Pressed")

GPIO.add_event_detect(17, GPIO.FALLING, callback=button_pressed)

try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    GPIO.cleanup()
ASyntax error in add_event_detect call
BCallback function missing required argument
CNo debounce time set, causing multiple triggers due to button bounce
DGPIO pin not set as input
Attempts:
2 left
💡 Hint

Mechanical buttons can cause multiple signals quickly when pressed.

📝 Syntax
advanced
2:00remaining
Which option correctly adds a button interrupt with debounce?

Choose the correct code snippet that sets up a button interrupt on GPIO 17 with a 300ms debounce time.

AGPIO.add_event_detect(17, GPIO.FALLING, callback=button_pressed, bouncetime='300')
BGPIO.add_event_detect(17, GPIO.FALLING, callback=button_pressed bouncetime=300)
CGPIO.add_event_detect(17, GPIO.FALLING, callback=button_pressed, debounce=300)
DGPIO.add_event_detect(17, GPIO.FALLING, callback=button_pressed, bouncetime=300)
Attempts:
2 left
💡 Hint

Check the parameter name and syntax for debounce in add_event_detect.

🚀 Application
expert
2:00remaining
How to safely clean up GPIO after using interrupts?

You wrote a program using GPIO.add_event_detect for a button. What is the best way to ensure GPIO pins are cleaned up properly when the program ends?

AUse a try-except block catching KeyboardInterrupt and call <code>GPIO.cleanup()</code> in finally
BCall <code>GPIO.cleanup()</code> immediately after <code>add_event_detect</code>
CDo nothing; GPIO pins reset automatically on program exit
DCall <code>GPIO.cleanup()</code> inside the callback function
Attempts:
2 left
💡 Hint

Think about program termination and resource cleanup.