0
0
Raspberry Piprogramming~20 mins

Multiple LED patterns in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Multiple LED Patterns
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 LED blinking code?

Consider this Python code controlling an LED connected to GPIO pin 17 on a Raspberry Pi. What will be the LED's behavior?

Raspberry Pi
import RPi.GPIO as GPIO
import time

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

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

GPIO.cleanup()
AThe LED stays off and never turns on.
BThe LED stays on continuously for 3 seconds.
CThe LED blinks on and off 6 times, each state lasting 0.5 seconds.
DThe LED blinks on and off 3 times, each state lasting 0.5 seconds.
Attempts:
2 left
💡 Hint

Look at the loop and the sleep durations to understand how many times the LED changes state.

Predict Output
intermediate
2:00remaining
What pattern does this LED sequence produce?

This code controls two LEDs connected to GPIO pins 17 and 27. What is the resulting LED pattern?

Raspberry Pi
import RPi.GPIO as GPIO
import time

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

for _ in range(2):
    GPIO.output(17, GPIO.HIGH)
    GPIO.output(27, GPIO.LOW)
    time.sleep(1)
    GPIO.output(17, GPIO.LOW)
    GPIO.output(27, GPIO.HIGH)
    time.sleep(1)

GPIO.cleanup()
ALED on pin 17 and LED on pin 27 alternate turning on every second, repeating twice.
BOnly the LED on pin 17 blinks twice; the other stays off.
CBoth LEDs turn on and off together twice with 1 second intervals.
DBoth LEDs stay off the entire time.
Attempts:
2 left
💡 Hint

Check which LED is on and off in each step inside the loop.

🔧 Debug
advanced
2:00remaining
Why does this LED pattern code raise an error?

Examine this code snippet intended to blink an LED connected to GPIO pin 22. Why does it raise an error?

Raspberry Pi
import RPi.GPIO as GPIO
import time

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

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

GPIO.cleanup()
ASyntaxError due to missing colon after the for loop declaration.
BRuntimeError because GPIO pin 22 is not set up correctly.
CTypeError because time.sleep() is called with a float instead of an integer.
DNameError because GPIO is not imported.
Attempts:
2 left
💡 Hint

Check the for loop syntax carefully.

🚀 Application
advanced
2:00remaining
How many LED blinks occur in this pattern?

This code runs a pattern on an LED connected to GPIO pin 5. How many times does the LED blink (turn on then off) before the program ends?

Raspberry Pi
import RPi.GPIO as GPIO
import time

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

for i in range(2):
    for j in range(3):
        GPIO.output(5, GPIO.HIGH)
        time.sleep(0.2)
        GPIO.output(5, GPIO.LOW)
        time.sleep(0.2)
    time.sleep(0.5)

GPIO.cleanup()
A5 blinks
B6 blinks
C2 blinks
D3 blinks
Attempts:
2 left
💡 Hint

Count the inner loop blinks and multiply by the outer loop iterations.

🧠 Conceptual
expert
2:00remaining
What is the main advantage of using PWM for LED brightness control on Raspberry Pi?

Pulse Width Modulation (PWM) is often used to control LED brightness. What is the key advantage of using PWM instead of just turning the LED on and off quickly?

APWM makes the LED blink faster so it appears to be a different color.
BPWM increases the voltage to the LED to make it brighter than normal.
CPWM allows smooth brightness control by varying the on/off ratio without changing voltage, saving power and reducing heat.
DPWM is used to change the LED's physical size by controlling current.
Attempts:
2 left
💡 Hint

Think about how brightness changes without damaging the LED or wasting energy.