Bird
Raised Fist0
Raspberry Piprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does GPIO.input(pin) do in Raspberry Pi programming?
easy
A. Resets the pin to default state
B. Sets the pin mode to output
C. Writes a value to the pin
D. Reads the current digital state (True/False) of the specified pin

Solution

  1. Step 1: Understand GPIO.input function

    GPIO.input(pin) reads the current digital signal on the given pin, returning True if the pin is HIGH and False if LOW.
  2. Step 2: Differentiate from other GPIO functions

    Functions like GPIO.setup set pin modes, and GPIO.output writes values. Only GPIO.input reads the pin state.
  3. Final Answer:

    Reads the current digital state (True/False) of the specified pin -> Option D
  4. Quick Check:

    GPIO.input reads pin state = A [OK]
Hint: Remember: input reads pin state, output writes to pin [OK]
Common Mistakes:
  • Confusing GPIO.input with GPIO.output
  • Thinking GPIO.input sets pin mode
  • Assuming GPIO.input writes values
2. Which of the following is the correct way to set up GPIO pin 17 as an input pin?
easy
A. GPIO.setup(17, GPIO.OUT)
B. GPIO.input(17, GPIO.IN)
C. GPIO.setup(17, GPIO.IN)
D. GPIO.input(17)

Solution

  1. Step 1: Identify the correct function to set pin mode

    Use GPIO.setup(pin, mode) to set pin mode. Here, mode should be GPIO.IN for input.
  2. Step 2: Check each option

    GPIO.setup(17, GPIO.OUT) sets pin as output, wrong mode. GPIO.input(17, GPIO.IN) uses GPIO.input incorrectly with two arguments. GPIO.input(17) calls GPIO.input without setup. Only GPIO.setup(17, GPIO.IN) correctly sets pin 17 as input.
  3. Final Answer:

    GPIO.setup(17, GPIO.IN) -> Option C
  4. Quick Check:

    Setup input pin = B [OK]
Hint: Use GPIO.setup(pin, GPIO.IN) to prepare input pins [OK]
Common Mistakes:
  • Using GPIO.input to set pin mode
  • Setting pin as output instead of input
  • Calling GPIO.input before setup
3. What will be the output of this code snippet if the button connected to pin 22 is pressed (assuming pressed means HIGH)?
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.IN)
state = GPIO.input(22)
print(state)
medium
A. True
B. 22
C. False
D. Error

Solution

  1. Step 1: Understand pin setup and input reading

    Pin 22 is set as input. When the button is pressed, the pin reads HIGH, so GPIO.input(22) returns True.
  2. Step 2: Check the print output

    The variable state holds True, so print(state) outputs True.
  3. Final Answer:

    True -> Option A
  4. Quick Check:

    Pressed button = True output [OK]
Hint: Pressed button means pin HIGH, so GPIO.input returns True [OK]
Common Mistakes:
  • Assuming pressed means False
  • Confusing pin number with state
  • Expecting numeric output instead of boolean
4. Identify the error in this code snippet:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.input(18)
GPIO.setup(18, GPIO.IN)
medium
A. GPIO.input called before GPIO.setup
B. GPIO.setup uses wrong pin number
C. GPIO.setmode is missing
D. No error, code is correct

Solution

  1. Step 1: Check order of GPIO calls

    GPIO pins must be set up before reading input. Here, GPIO.input(18) is called before GPIO.setup(18, GPIO.IN), which is incorrect.
  2. Step 2: Verify other parts

    Pin number 18 is consistent, and GPIO.setmode(GPIO.BCM) is present. So no error there.
  3. Final Answer:

    GPIO.input called before GPIO.setup -> Option A
  4. Quick Check:

    GPIO.input called before GPIO.setup = D [OK]
Hint: Always setup pins before reading input [OK]
Common Mistakes:
  • Calling GPIO.input before GPIO.setup
  • Ignoring pin mode setup
  • Assuming code runs without setup
5. You want to detect if a motion sensor connected to pin 27 is active (HIGH) and print "Motion detected" only once when it changes from LOW to HIGH. Which code snippet correctly implements this behavior?
hard
A. GPIO.setup(27, GPIO.OUT) while True: if GPIO.input(27): print("Motion detected")
B. GPIO.setup(27, GPIO.IN) prev_state = False while True: current_state = GPIO.input(27) if current_state and not prev_state: print("Motion detected") prev_state = current_state
C. GPIO.setup(27, GPIO.IN) while True: if GPIO.input(27): print("Motion detected")
D. GPIO.setup(27, GPIO.IN) prev_state = True while True: current_state = GPIO.input(27) if current_state and not prev_state: print("Motion detected") prev_state = current_state

Solution

  1. Step 1: Setup pin as input and track previous state

    Pin 27 must be set as input. To detect change from LOW to HIGH, store previous state starting as False (LOW).
  2. Step 2: Detect rising edge and print once

    Inside the loop, read current state. If current is True and previous is False, print message once. Then update previous state.
  3. Step 3: Analyze options

    GPIO.setup(27, GPIO.IN) prev_state = False while True: current_state = GPIO.input(27) if current_state and not prev_state: print("Motion detected") prev_state = current_state correctly implements this logic. GPIO.setup(27, GPIO.OUT) while True: if GPIO.input(27): print("Motion detected") sets pin as output, wrong. GPIO.setup(27, GPIO.IN) while True: if GPIO.input(27): print("Motion detected") prints repeatedly while HIGH, not once. GPIO.setup(27, GPIO.IN) prev_state = True while True: current_state = GPIO.input(27) if current_state and not prev_state: print("Motion detected") prev_state = current_state starts prev_state as True, so first detection missed.
  4. Final Answer:

    GPIO.setup(27, GPIO.IN), prev_state = False (rising edge detection) -> Option B
  5. Quick Check:

    Detect rising edge with prev_state = False first [OK]
Hint: Track previous state to detect change from LOW to HIGH [OK]
Common Mistakes:
  • Setting pin as output instead of input
  • Printing repeatedly instead of once
  • Initializing previous state incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard