Bird
Raised Fist0
Raspberry Piprogramming~20 mins

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

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

Practice

(1/5)
1. What is the main purpose of setting a GPIO pin as output when controlling a single LED on a Raspberry Pi?
easy
A. To send signals that turn the LED on or off
B. To read the LED's brightness level
C. To connect the LED to the internet
D. To power the Raspberry Pi itself

Solution

  1. Step 1: Understand GPIO pin modes

    GPIO pins can be set as input or output. Output pins send signals to devices like LEDs.
  2. Step 2: Purpose of output mode for LED

    Setting a pin as output allows the Raspberry Pi to send HIGH or LOW signals to turn the LED on or off.
  3. Final Answer:

    To send signals that turn the LED on or off -> Option A
  4. Quick Check:

    Output mode controls LED state [OK]
Hint: Output pins send signals to control devices like LEDs [OK]
Common Mistakes:
  • Confusing input with output mode
  • Thinking GPIO controls power supply
  • Assuming GPIO reads LED brightness
2. Which of the following is the correct way to import the GPIO library and set pin 18 as output in Python for Raspberry Pi?
easy
A. import RPi.GPIO as GPIO\nGPIO.setup(18, GPIO.OUT)
B. import RPi.GPIO as GPIO\nGPIO.setup(18, GPIO.IN)
C. import gpiozero\nGPIO.setup(18, GPIO.OUT)
D. import RPi.GPIO as GPIO\nGPIO.output(18, GPIO.OUT)

Solution

  1. Step 1: Import the correct library

    Use import RPi.GPIO as GPIO to access GPIO functions.
  2. Step 2: Set pin 18 as output

    Use GPIO.setup(18, GPIO.OUT) to configure pin 18 for output.
  3. Final Answer:

    import RPi.GPIO as GPIO\nGPIO.setup(18, GPIO.OUT) -> Option A
  4. Quick Check:

    GPIO setup output = correct syntax [OK]
Hint: Use GPIO.setup(pin, GPIO.OUT) to set output pin [OK]
Common Mistakes:
  • Using GPIO.IN instead of GPIO.OUT
  • Calling GPIO.output instead of GPIO.setup for pin mode
  • Importing wrong library like gpiozero
3. What will be the output on the LED when running this Python code on 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(2)
GPIO.output(17, GPIO.LOW)
GPIO.cleanup()
medium
A. LED stays off for 2 seconds, then turns on
B. LED blinks rapidly for 2 seconds
C. LED turns on for 2 seconds, then turns off
D. Code causes an error and LED does not light

Solution

  1. Step 1: Analyze GPIO output commands

    The code sets pin 17 HIGH, turning LED on, waits 2 seconds, then sets pin 17 LOW, turning LED off.
  2. Step 2: Understand timing and cleanup

    The LED stays on during time.sleep(2), then turns off before cleanup.
  3. Final Answer:

    LED turns on for 2 seconds, then turns off -> Option C
  4. Quick Check:

    GPIO.HIGH = LED on, GPIO.LOW = LED off [OK]
Hint: HIGH turns LED on, LOW turns it off [OK]
Common Mistakes:
  • Confusing HIGH and LOW signals
  • Ignoring the sleep delay effect
  • Forgetting GPIO.cleanup() importance
4. Identify the error in this Raspberry Pi LED control code:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
GPIO.output(12, HIGH)
GPIO.cleanup()
medium
A. GPIO.cleanup() should be called before output
B. Missing import for time module
C. Pin 12 cannot be used as output
D. Incorrect use of GPIO.HIGH without prefix

Solution

  1. Step 1: Check GPIO output command

    The code uses HIGH without GPIO. prefix, causing a NameError.
  2. Step 2: Correct usage of HIGH signal

    It should be GPIO.output(12, GPIO.HIGH) to properly reference the constant.
  3. Final Answer:

    Incorrect use of GPIO.HIGH without prefix -> Option D
  4. Quick Check:

    Use GPIO.HIGH, not just HIGH [OK]
Hint: Always prefix HIGH with GPIO. [OK]
Common Mistakes:
  • Using HIGH instead of GPIO.HIGH
  • Calling cleanup before output
  • Confusing BOARD and BCM modes
5. You want to write a Python program on Raspberry Pi that turns an LED on for 1 second, off for 1 second, and repeats this 5 times. Which code snippet correctly implements this behavior?
hard
A. import RPi.GPIO as GPIO\nGPIO.setmode(GPIO.BCM)\nGPIO.setup(21, GPIO.OUT)\nGPIO.output(21, GPIO.HIGH)\ntime.sleep(5)\nGPIO.output(21, GPIO.LOW)\nGPIO.cleanup()
B. import RPi.GPIO as GPIO\nimport time\nGPIO.setmode(GPIO.BCM)\nGPIO.setup(21, GPIO.OUT)\nfor i in range(5):\n GPIO.output(21, GPIO.HIGH)\n time.sleep(1)\n GPIO.output(21, GPIO.LOW)\n time.sleep(1)\nGPIO.cleanup()
C. import RPi.GPIO as GPIO\nimport time\nGPIO.setmode(GPIO.BCM)\nGPIO.setup(21, GPIO.OUT)\nfor i in range(5):\n GPIO.output(21, GPIO.LOW)\n time.sleep(1)\n GPIO.output(21, GPIO.HIGH)\n time.sleep(1)\nGPIO.cleanup()
D. import RPi.GPIO as GPIO\nimport time\nGPIO.setmode(GPIO.BCM)\nGPIO.setup(21, GPIO.OUT)\nGPIO.output(21, GPIO.HIGH)\ntime.sleep(1)\nGPIO.output(21, GPIO.LOW)\ntime.sleep(1)

Solution

  1. Step 1: Understand the loop for repetition

    The code uses for i in range(5): to repeat the on/off cycle 5 times.
  2. Step 2: Correct on/off timing inside loop

    Inside the loop, the LED is turned on (GPIO.HIGH) for 1 second, then off (GPIO.LOW) for 1 second.
  3. Final Answer:

    Code snippet C correctly repeats LED on/off 5 times with 1 second intervals -> Option B
  4. Quick Check:

    Loop with GPIO.HIGH then GPIO.LOW and sleep [OK]
Hint: Use a loop with GPIO.HIGH, sleep, GPIO.LOW, sleep [OK]
Common Mistakes:
  • Swapping HIGH and LOW timing order
  • Missing import of time module
  • Not using a loop for repetition
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