Bird
0
0

What will be the output of this Python code controlling LEDs on Raspberry Pi?

medium📝 Predict Output Q13 of 15
Raspberry Pi - LED and Button Projects
What will be the output of this Python code controlling LEDs on Raspberry Pi?
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup([17, 18], GPIO.OUT)

for pin in [17, 18]:
    GPIO.output(pin, GPIO.HIGH)
    time.sleep(0.5)
    GPIO.output(pin, GPIO.LOW)

GPIO.cleanup()
ALED on pin 17 turns on for 0.5s, then off; then LED on pin 18 turns on for 0.5s, then off
BBoth LEDs turn on and off together for 0.5s
COnly LED on pin 18 blinks twice
DSyntax error due to GPIO.output usage
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the for loop behavior

    The loop turns on pin 17 LED, waits 0.5 seconds, then turns it off, then repeats for pin 18.
  2. Step 2: Understand timing and output

    LEDs blink one after another, not together, each for 0.5 seconds.
  3. Final Answer:

    LED on pin 17 turns on for 0.5s, then off; then LED on pin 18 turns on for 0.5s, then off -> Option A
  4. Quick Check:

    Sequential LED blinking = LED on pin 17 turns on for 0.5s, then off; then LED on pin 18 turns on for 0.5s, then off [OK]
Quick Trick: Check loop order and sleep timing carefully [OK]
Common Mistakes:
  • Assuming LEDs blink simultaneously
  • Confusing pin numbers
  • Thinking code has syntax errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes