Bird
0
0

What will be the visible behavior of this Python code controlling LEDs on GPIO pins 12, 16, and 20?

medium📝 Predict Output Q5 of 15
Raspberry Pi - LED and Button Projects
What will be the visible behavior of this Python code controlling LEDs on GPIO pins 12, 16, and 20?
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
led_pins = [12, 16, 20]
for pin in led_pins:
    GPIO.setup(pin, GPIO.OUT)
for pin in led_pins:
    GPIO.output(pin, GPIO.HIGH)
    time.sleep(0.4)
    GPIO.output(pin, GPIO.LOW)
AEach LED turns on one by one for 0.4 seconds, then turns off before the next LED lights
BAll LEDs turn on simultaneously for 0.4 seconds, then all turn off together
CLEDs blink randomly without a pattern
DOnly the last LED stays on continuously
Step-by-Step Solution
Solution:
  1. Step 1: Setup LEDs

    All pins are set as outputs.
  2. Step 2: Loop through LEDs

    Each LED is turned on, delayed 0.4 seconds, then turned off before moving to next.
  3. Final Answer:

    Each LED turns on one by one for 0.4 seconds, then turns off before the next LED lights -> Option A
  4. Quick Check:

    Sequential on/off with delay matches Each LED turns on one by one for 0.4 seconds, then turns off before the next LED lights [OK]
Quick Trick: LEDs light sequentially with delay [OK]
Common Mistakes:
  • Assuming all LEDs light simultaneously
  • Thinking LEDs blink randomly
  • Believing last LED stays on only

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes