0
0
Raspberry Piprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GPIO Output Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output state of the LED after running this code?

Consider this Raspberry Pi Python code controlling an LED connected to GPIO pin 18.

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.HIGH)
state = GPIO.input(18)
print(state)

What will be printed?

Raspberry Pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.HIGH)
state = GPIO.input(18)
print(state)
A1
B0
CGPIO.HIGH
DNone
Attempts:
2 left
💡 Hint

GPIO.HIGH sets the pin to ON, so reading it back returns 1.

🧠 Conceptual
intermediate
1:30remaining
Which GPIO.output call will turn OFF the LED connected to pin 23?

You have an LED connected to GPIO pin 23. Which command will turn the LED OFF?

AGPIO.output(23, True)
BGPIO.output(23, GPIO.HIGH)
CGPIO.output(23, 1)
DGPIO.output(23, GPIO.LOW)
Attempts:
2 left
💡 Hint

LOW means OFF, HIGH means ON.

🔧 Debug
advanced
2:30remaining
Why does this code raise a RuntimeError?

Look at this code snippet:

import RPi.GPIO as GPIO
GPIO.output(17, GPIO.HIGH)

When run, it raises a RuntimeError: 'You must setup() the GPIO channel first'. Why?

Raspberry Pi
import RPi.GPIO as GPIO
GPIO.output(17, GPIO.HIGH)
ABecause GPIO.setup(17, GPIO.OUT) was not called before output()
BBecause GPIO.setmode() was not called before output()
CBecause GPIO.cleanup() was called before output()
DBecause GPIO.output() cannot be called with GPIO.HIGH
Attempts:
2 left
💡 Hint

You must prepare the pin as output before setting its value.

📝 Syntax
advanced
1:30remaining
Which option contains a syntax error in using GPIO.output?

Identify the code snippet that will cause a syntax error when trying to set GPIO pin 5 to LOW.

AGPIO.output(5, 0)
BGPIO.output(5 GPIO.LOW)
CGPIO.output(5, GPIO.LOW)
DGPIO.output(5, False)
Attempts:
2 left
💡 Hint

Check for missing commas or parentheses.

🚀 Application
expert
3:00remaining
How many times will the LED blink in this code?

Given this code to blink an LED connected to GPIO pin 12:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)
for i in range(5):
    GPIO.output(12, GPIO.HIGH)
    time.sleep(0.2)
    GPIO.output(12, GPIO.LOW)
    time.sleep(0.2)
GPIO.cleanup()

How many times does the LED turn ON (blink) before the program ends?

Raspberry Pi
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)
for i in range(5):
    GPIO.output(12, GPIO.HIGH)
    time.sleep(0.2)
    GPIO.output(12, GPIO.LOW)
    time.sleep(0.2)
GPIO.cleanup()
A1
B10
C5
D0
Attempts:
2 left
💡 Hint

Each loop cycle turns the LED ON once.