Challenge - 5 Problems
GPIO Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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))
Attempts:
2 left
💡 Hint
GPIO.gpio_function(pin) returns a number representing the pin mode. GPIO.OUT corresponds to 1.
✗ Incorrect
The code sets pin 18 as an output pin using BCM numbering. The gpio_function returns 1 for output mode.
❓ Predict Output
intermediate2: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")
Attempts:
2 left
💡 Hint
The setup function expects a constant like GPIO.OUT, not a string.
✗ Incorrect
The GPIO.setup function requires the mode argument to be a constant like GPIO.OUT, not a string. Passing "OUT" causes a ValueError.
🔧 Debug
advanced3: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()
Attempts:
2 left
💡 Hint
Think about how fast the commands run and what you can actually see.
✗ Incorrect
The code sets the pin HIGH and then immediately LOW without any pause. The change happens too quickly to notice. Adding a delay between the two commands would make the toggle visible.
🧠 Conceptual
advanced2: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)?
Attempts:
2 left
💡 Hint
Think about how pins are identified on the Raspberry Pi.
✗ Incorrect
GPIO.BCM mode refers to the Broadcom chip's GPIO numbering, while GPIO.BOARD mode refers to the physical pin numbers on the Raspberry Pi's header.
🚀 Application
expert3: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)
Attempts:
2 left
💡 Hint
Consider the effect of the last two setup calls on pins 6 and 19.
✗ Incorrect
Initially, pins 5, 6, 13, and 19 are set as outputs. Then pins 6 and 19 are reconfigured as inputs. So only pins 5 and 13 remain outputs.