0
0
Raspberry Piprogramming~20 mins

Single LED control in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Single LED Control Master
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 Raspberry Pi LED control code?

Consider this Python code controlling an LED connected to GPIO pin 17 on a Raspberry Pi. What will be the state of the LED after running this code?

Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)

GPIO.output(17, GPIO.HIGH)
time.sleep(1)
GPIO.output(17, GPIO.LOW)
GPIO.cleanup()
AThe LED stays off the whole time.
BThe LED turns on for 1 second, then turns off.
CThe LED blinks continuously.
DThe code raises a runtime error.
Attempts:
2 left
💡 Hint

GPIO.HIGH turns the LED on, GPIO.LOW turns it off.

🧠 Conceptual
intermediate
1:30remaining
Which GPIO mode is used in this code snippet?

In Raspberry Pi Python code, what does GPIO.setmode(GPIO.BCM) mean?

AIt sets all pins to input mode.
BIt uses the physical pin numbering on the board.
CIt uses the Broadcom chip pin numbering scheme.
DIt disables all GPIO pins.
Attempts:
2 left
💡 Hint

BCM refers to the chip's internal numbering.

🔧 Debug
advanced
2:00remaining
What error does this code raise?

What error will this Raspberry Pi LED control code produce?

Raspberry Pi
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, 2)
GPIO.cleanup()
AValueError: invalid output value
BTypeError: an integer is required (got type str)
CRuntimeWarning: This channel is already in use
DNo error, LED turns on.
Attempts:
2 left
💡 Hint

GPIO.output expects GPIO.HIGH or GPIO.LOW (0 or 1), not 2.

📝 Syntax
advanced
1:30remaining
Which option fixes the syntax error in this code?

Fix the syntax error in this Raspberry Pi LED control code snippet:

GPIO.setup(17 GPIO.OUT)
AGPIO.setup(17, GPIO.OUT)
BGPIO.setup(17 GPIO.OUT)
CGPIO.setup(17: GPIO.OUT)
DGPIO.setup(17; GPIO.OUT)
Attempts:
2 left
💡 Hint

Function arguments must be separated by commas.

🚀 Application
expert
2:30remaining
How many times does the LED blink in this code?

Given this Raspberry Pi Python code, how many times will the LED blink?

Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)

for i in range(3):
    GPIO.output(17, GPIO.HIGH)
    time.sleep(0.5)
    GPIO.output(17, GPIO.LOW)
    time.sleep(0.5)

GPIO.cleanup()
A1 time
B6 times
C0 times
D3 times
Attempts:
2 left
💡 Hint

Each loop cycle turns the LED on and off once.