0
0
Raspberry Piprogramming~20 mins

RPi.GPIO library setup in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GPIO Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this GPIO setup code?
Consider this Python code snippet using the RPi.GPIO library. What will be printed when this code runs?
Raspberry Pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
print(GPIO.gpio_function(18))
A0
B1
C2
D3
Attempts:
2 left
💡 Hint
GPIO.gpio_function(pin) returns a number representing the pin mode. GPIO.OUT corresponds to 1.
Predict Output
intermediate
2:00remaining
What error does this GPIO setup code raise?
What error will this code produce when run on a Raspberry Pi?
Raspberry Pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(22, "OUT")
ARuntimeError
BTypeError
CValueError
DNo error
Attempts:
2 left
💡 Hint
The setup function expects a constant like GPIO.OUT, not a string.
🔧 Debug
advanced
3:00remaining
Why does this GPIO output not toggle as expected?
This code is intended to toggle GPIO pin 17 on and off once. Why does it not work as expected?
Raspberry Pi
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, GPIO.HIGH)
GPIO.output(17, GPIO.LOW)
GPIO.cleanup()
AGPIO.output requires a boolean True/False, not GPIO.HIGH/LOW.
BGPIO.cleanup() resets the pin before setting LOW, so the LOW command fails.
CGPIO.setmode(GPIO.BCM) must be called after setup, not before.
DThe pin is set HIGH and immediately set LOW without delay, so the change is too fast to observe.
Attempts:
2 left
💡 Hint
Think about how fast the commands run and what you can actually see.
🧠 Conceptual
advanced
2:30remaining
What is the difference between GPIO.BCM and GPIO.BOARD modes?
When setting up GPIO pins, what is the key difference between using GPIO.setmode(GPIO.BCM) and GPIO.setmode(GPIO.BOARD)?
AGPIO.BCM is for input pins only; GPIO.BOARD is for output pins only.
BGPIO.BCM uses Broadcom chip numbering; GPIO.BOARD uses physical pin numbering on the header.
CGPIO.BCM requires root access; GPIO.BOARD does not.
DGPIO.BCM disables pull-up resistors; GPIO.BOARD enables them by default.
Attempts:
2 left
💡 Hint
Think about how pins are identified on the Raspberry Pi.
🚀 Application
expert
3:00remaining
How many pins are set as outputs after this code runs?
Given this code, how many GPIO pins are configured as outputs at the end?
Raspberry Pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
pins = [5, 6, 13, 19]
for pin in pins:
    GPIO.setup(pin, GPIO.OUT)
GPIO.setup(6, GPIO.IN)
GPIO.setup(19, GPIO.IN)
A2
B4
C3
D1
Attempts:
2 left
💡 Hint
Consider the effect of the last two setup calls on pins 6 and 19.