0
0
Raspberry Piprogramming~20 mins

Button press detection in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Button Press Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Detecting a button press with GPIO input

What will be the output of this Raspberry Pi Python code 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)

try:
    while True:
        if GPIO.input(17) == GPIO.LOW:
            print("Button pressed")
            break
        time.sleep(0.1)
finally:
    GPIO.cleanup()
ANo output, program runs forever
BGPIO.setup error
CButton pressed
DRuntimeError due to missing cleanup
Attempts:
2 left
💡 Hint

Remember that the button is connected with a pull-up resistor, so pressing it pulls the pin LOW.

Predict Output
intermediate
2:00remaining
Detecting button release event

What will this code print when the button connected to GPIO pin 22 is released after being pressed?

Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

try:
    while True:
        if GPIO.input(22) == GPIO.HIGH:
            print("Button released")
            break
        time.sleep(0.1)
finally:
    GPIO.cleanup()
ARuntimeError due to wrong pull_up_down setting
BButton released
CNo output, program runs forever
DButton pressed
Attempts:
2 left
💡 Hint

Check the pull-down resistor and what HIGH means for the button state.

🔧 Debug
advanced
2:00remaining
Why does this button press detection code not print anything?

Consider this code snippet intended to detect a button press on GPIO pin 5. Why does it never print "Pressed"?

Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.IN)

while True:
    if GPIO.input(5) == GPIO.LOW:
        print("Pressed")
        break
    time.sleep(0.1)
AThe button pin lacks a pull-up or pull-down resistor, so input is floating
BGPIO.setup should use GPIO.OUT for button input
CThe code needs GPIO.cleanup() before the loop
Dtime.sleep(0.1) is too short to detect the press
Attempts:
2 left
💡 Hint

Think about what happens if the input pin is not stabilized with a resistor.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this button detection code

Which option correctly fixes the syntax error in this Raspberry Pi button detection code?

Raspberry Pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)

if GPIO.input(18) == GPIO.LOW
    print("Button pressed")
AAdd a colon at the end of the if statement line
BChange GPIO.LOW to GPIO.HIGH
CRemove the parentheses from GPIO.input
DIndent the print statement at the same level as if
Attempts:
2 left
💡 Hint

Check the syntax of the if statement in Python.

🚀 Application
expert
2:00remaining
How many times will the message print if button bounces?

This code detects button presses on GPIO pin 23 and prints "Pressed" each time the input reads LOW. The button is known to bounce (rapidly change state) when pressed. How many times will "Pressed" print if the button is pressed once?

Raspberry Pi
import RPi.GPIO as GPIO
import time

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

for _ in range(10):
    if GPIO.input(23) == GPIO.LOW:
        print("Pressed")
    time.sleep(0.01)
AOnly once, because time.sleep(0.01) filters out bounces
BExactly once, because the loop runs 10 times but only one LOW reading occurs
CNever, because the pull_up_down setting is incorrect
DMultiple times, due to button bouncing causing repeated LOW readings
Attempts:
2 left
💡 Hint

Think about how button bouncing affects the input signal during rapid reads.