Bird
Raised Fist0
Raspberry Piprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the GPIO.setup(pin, GPIO.OUT) command in a Raspberry Pi LED blink program?
easy
A. It cleans up the GPIO pins after use.
B. It reads the input from the LED.
C. It turns the LED on immediately.
D. It sets the specified pin as an output to control the LED.

Solution

  1. Step 1: Understand GPIO pin modes

    The Raspberry Pi GPIO pins can be set as input or output. To control an LED, the pin must send signals, so it needs to be an output.
  2. Step 2: Role of GPIO.setup

    The command GPIO.setup(pin, GPIO.OUT) configures the pin as an output pin, allowing the program to turn the LED on or off.
  3. Final Answer:

    It sets the specified pin as an output to control the LED. -> Option D
  4. Quick Check:

    GPIO.setup with GPIO.OUT means output pin [OK]
Hint: Remember: setup with GPIO.OUT means output pin [OK]
Common Mistakes:
  • Confusing input and output pin setup
  • Thinking setup turns LED on
  • Mixing cleanup with setup
2. Which of the following is the correct way to import the GPIO library and set the pin numbering mode for a Raspberry Pi LED blink program?
easy
A. import RPi.GPIO as GPIO\nGPIO.setupmode(GPIO.BCM)
B. import RPi.GPIO as GPIO\nGPIO.setmode(GPIO.BOARD)
C. import RPi.GPIO as GPIO\nGPIO.setmode(GPIO.OUT)
D. import gpiozero\ngpiozero.setmode(GPIO.BCM)

Solution

  1. Step 1: Importing the GPIO library

    The standard library to control GPIO pins on Raspberry Pi is RPi.GPIO, imported as GPIO.
  2. Step 2: Setting pin numbering mode

    We use GPIO.setmode() to choose pin numbering style. GPIO.BOARD uses physical pin numbers, which is common for beginners.
  3. Final Answer:

    import RPi.GPIO as GPIO\nGPIO.setmode(GPIO.BOARD) -> Option B
  4. Quick Check:

    Import RPi.GPIO and setmode BOARD [OK]
Hint: Use 'import RPi.GPIO as GPIO' and 'GPIO.setmode(GPIO.BOARD)' [OK]
Common Mistakes:
  • Using gpiozero instead of RPi.GPIO
  • Wrong method names like setupmode
  • Setting mode to GPIO.OUT instead of BOARD or BCM
3. What will be the output behavior of this code snippet controlling an LED on pin 11?
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
for i in range(3):
    GPIO.output(11, GPIO.HIGH)
    time.sleep(1)
    GPIO.output(11, GPIO.LOW)
    time.sleep(1)
GPIO.cleanup()
medium
A. The LED will blink on and off 3 times with 1 second intervals.
B. The LED will stay on continuously for 3 seconds.
C. The LED will blink rapidly without delay.
D. The program will raise an error due to missing setup.

Solution

  1. Step 1: Analyze the loop and GPIO output commands

    The loop runs 3 times. Each time, the LED pin is set HIGH (on), waits 1 second, then set LOW (off), waits 1 second.
  2. Step 2: Understand the LED blinking pattern

    This means the LED turns on for 1 second, off for 1 second, repeated 3 times, resulting in 3 blinks.
  3. Final Answer:

    The LED will blink on and off 3 times with 1 second intervals. -> Option A
  4. Quick Check:

    Loop with sleep controls blink count and duration [OK]
Hint: Loop with sleep(1) toggles LED on/off 3 times [OK]
Common Mistakes:
  • Thinking LED stays on continuously
  • Ignoring sleep delays causing rapid blinking
  • Assuming missing setup causes error here
4. Identify the error in this Raspberry Pi LED blink code:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.HIGH)
time.sleep(1)
GPIO.output(18, GPIO.LOW)
GPIO.cleanup()
medium
A. Pin 18 is not set up as output before use.
B. Missing import of time module.
C. GPIO.setmode(GPIO.BCM) conflicts with pin numbering 18 if BOARD mode expected.
D. GPIO.cleanup() should be called before output commands.

Solution

  1. Step 1: Check pin numbering mode and pin number

    The code uses GPIO.setmode(GPIO.BCM), which means pin numbers refer to Broadcom chip pins. Pin 18 is valid in BCM mode, but if the user expects physical pin numbering (BOARD), this causes confusion.
  2. Step 2: Understand the mismatch impact

    If the user wired the LED to physical pin 18 (BOARD), but code uses BCM mode, the LED won't work as expected. This is a common error mixing modes and pin numbers.
  3. Final Answer:

    GPIO.setmode(GPIO.BCM) conflicts with pin numbering 18 if BOARD mode expected. -> Option C
  4. Quick Check:

    Pin numbering mode must match wiring [OK]
Hint: Match setmode with pin numbering used [OK]
Common Mistakes:
  • Confusing BOARD and BCM pin numbers
  • Calling cleanup too early
  • Forgetting to import time module
5. You want to blink two LEDs connected to pins 7 and 11 alternately for 5 cycles using Raspberry Pi GPIO. Which code snippet correctly implements this behavior?
hard
A. import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT) GPIO.setup(11, GPIO.OUT) for _ in range(5): GPIO.output(7, GPIO.HIGH) GPIO.output(11, GPIO.LOW) time.sleep(0.5) GPIO.output(7, GPIO.LOW) GPIO.output(11, GPIO.HIGH) time.sleep(0.5) GPIO.cleanup()
B. import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT) for _ in range(5): GPIO.output(7, GPIO.HIGH) GPIO.output(11, GPIO.LOW) time.sleep(1) GPIO.cleanup()
C. import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT) GPIO.setup(11, GPIO.OUT) GPIO.output(7, GPIO.HIGH) GPIO.output(11, GPIO.HIGH) GPIO.cleanup()
D. import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.IN) GPIO.setup(11, GPIO.IN) for _ in range(5): GPIO.output(7, GPIO.HIGH) GPIO.output(11, GPIO.LOW) time.sleep(0.5) GPIO.cleanup()

Solution

  1. Step 1: Setup pins as output and initialize GPIO

    Both pins 7 and 11 are set as outputs using GPIO.setup(pin, GPIO.OUT). The mode is set to BOARD to match physical pin numbers.
  2. Step 2: Alternate LEDs in a loop with delays

    The loop runs 5 times. Inside each cycle, pin 7 is turned on and pin 11 off, then after 0.5 seconds, pin 7 is off and pin 11 on, creating alternate blinking.
  3. Step 3: Cleanup GPIO pins after use

    Calling GPIO.cleanup() resets the pins safely after blinking finishes.
  4. Final Answer:

    Code in option A correctly blinks two LEDs alternately for 5 cycles. -> Option A
  5. Quick Check:

    Alternate output with sleep and cleanup [OK]
Hint: Use two outputs toggled alternately with sleep [OK]
Common Mistakes:
  • Setting pins as input instead of output
  • Not toggling both pins alternately
  • Missing time delays causing no visible blink
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard