0
0
Raspberry Piprogramming~20 mins

First GPIO program (LED blink) in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GPIO Blink 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 GPIO LED blink code?

Consider this Python code to blink an LED connected to GPIO pin 17 on a Raspberry Pi. What will it print to the console?

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)
    print("LED ON")
    time.sleep(0.5)
    GPIO.output(17, GPIO.LOW)
    print("LED OFF")
    time.sleep(0.5)

GPIO.cleanup()
ALED ON\nLED OFF\nLED ON\nLED OFF\nLED ON\nLED OFF
BLED ON\nLED ON\nLED ON\nLED OFF\nLED OFF\nLED OFF
CLED ON\nLED OFF\nLED ON\nLED ON\nLED OFF\nLED OFF
DLED OFF\nLED ON\nLED OFF\nLED ON\nLED OFF\nLED ON
Attempts:
2 left
💡 Hint

Each loop turns the LED ON then OFF once, printing messages accordingly.

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

In the code below, what does GPIO.setmode(GPIO.BCM) mean?

Raspberry Pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
AIt initializes the GPIO pins as input by default.
BIt sets pin numbering to use the physical pin numbers on the header.
CIt disables all GPIO pins to save power.
DIt sets pin numbering to use the Broadcom chip's GPIO numbers.
Attempts:
2 left
💡 Hint

There are two common ways to number pins: BCM and BOARD.

🔧 Debug
advanced
2:00remaining
What error will this GPIO LED blink code produce?

Look at this code snippet. What error will it raise when run on a Raspberry Pi?

Raspberry Pi
import RPi.GPIO as GPIO
import time

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

GPIO.output(17, GPIO.HIGH)
print("LED ON")
time.sleep(1)

GPIO.cleanup()

GPIO.output(17, GPIO.LOW)
print("LED OFF")
ARuntimeError: GPIO channel already in use
BRuntimeError: You must setup() the GPIO channel first
CRuntimeError: The GPIO channel has been cleaned up and cannot be used
DNo error, the code runs fine
Attempts:
2 left
💡 Hint

Consider what GPIO.cleanup() does before the last output call.

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

This code has a syntax error. Which option fixes it correctly?

for i in range(3)
    GPIO.output(17, GPIO.HIGH)
    time.sleep(0.5)
Raspberry Pi
for i in range(3)
    GPIO.output(17, GPIO.HIGH)
    time.sleep(0.5)
AReplace range(3) with range[3]
BAdd a colon after the for statement: for i in range(3):
CRemove the parentheses from range: for i in range 3
DIndent the for statement by 4 spaces
Attempts:
2 left
💡 Hint

Python for loops require a colon at the end of the header line.

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

Given this Raspberry Pi GPIO code, how many times will the LED turn ON and OFF?

Raspberry Pi
import RPi.GPIO as GPIO
import time

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

count = 0
while count < 5:
    GPIO.output(17, GPIO.HIGH)
    time.sleep(0.2)
    GPIO.output(17, GPIO.LOW)
    time.sleep(0.2)
    count += 1

GPIO.cleanup()
A5 times
B10 times
C1 time
D0 times
Attempts:
2 left
💡 Hint

Count how many times the loop runs and what happens each time.