0
0
Raspberry Piprogramming~20 mins

Digital input (GPIO.input) in Raspberry Pi - Practice Problems & Coding Challenges

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

What is the output of this Raspberry Pi GPIO code when the button connected to GPIO pin 17 is pressed?

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

button_state = GPIO.input(17)
print(button_state)
ARaises RuntimeError
B1
C0
DNone
Attempts:
2 left
💡 Hint

Remember that the button is connected with a pull-up resistor, so pressing it connects the pin to ground.

Predict Output
intermediate
2:00remaining
Detecting button release state

What will be printed by this code snippet if the button connected to GPIO pin 22 is not pressed?

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

state = GPIO.input(22)
print(state)
A0
B1
CNone
DRaises RuntimeError
Attempts:
2 left
💡 Hint

Think about what the pull-down resistor does when the button is not pressed.

🔧 Debug
advanced
2:00remaining
Why does this GPIO.input code always print 1?

Consider this code snippet. The button is connected to GPIO pin 5 with a pull-up resistor. The code always prints 1, even when the button is pressed. What is the most likely cause?

Raspberry Pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.IN)

print(GPIO.input(5))
AGPIO.input always returns 1 by default.
BThe button is wired incorrectly, so it never connects to ground.
CThe pull-up resistor is missing in setup, so the pin floats and reads 1 randomly.
DThe code is missing pull_up_down=GPIO.PUD_UP in setup, so the pin floats and reads 1.
Attempts:
2 left
💡 Hint

Think about what happens if you don't specify pull_up_down in GPIO.setup for an input pin.

🧠 Conceptual
advanced
2:00remaining
Understanding GPIO.input behavior with pull resistors

Which statement correctly describes the behavior of GPIO.input(pin) when a button is connected with a pull-down resistor?

AGPIO.input(pin) returns 0 when the button is pressed because the pin is connected to ground through the button.
BGPIO.input(pin) returns 1 when the button is pressed because the pin is connected to 3.3V through the button.
CGPIO.input(pin) returns 1 when the button is not pressed because the pull-down resistor pulls the pin high.
DGPIO.input(pin) returns None if the button is pressed.
Attempts:
2 left
💡 Hint

Think about what a pull-down resistor does and what pressing the button does to the pin voltage.

🚀 Application
expert
3:00remaining
Counting button presses with GPIO.input

You want to count how many times a button connected to GPIO pin 18 is pressed. Which code snippet correctly increments count only when the button is pressed (assuming pull-up resistor)?

A
if not GPIO.input(18):
    count += 1
B
if GPIO.input(18) == 0:
    count += 1
C
if GPIO.input(18):
    count += 1
D
if GPIO.input(18) == 1:
    count += 1
Attempts:
2 left
💡 Hint

Remember that with a pull-up resistor, the pin reads 1 when not pressed and 0 when pressed.