Bird
0
0

What will be the output behavior of this Python code controlling two LEDs on GPIO pins 18 and 23?

medium📝 Predict Output Q4 of 15
Raspberry Pi - LED and Button Projects
What will be the output behavior of this Python code controlling two LEDs on GPIO pins 18 and 23?
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.output(18, GPIO.HIGH)
time.sleep(1)
GPIO.output(23, GPIO.HIGH)
time.sleep(1)
GPIO.output(18, GPIO.LOW)
time.sleep(1)
GPIO.output(23, GPIO.LOW)
ALED on pin 18 turns on, then LED on pin 23 turns on, then both turn off one by one.
BBoth LEDs turn on at the same time and then turn off together.
COnly LED on pin 23 turns on and off, pin 18 stays off.
DCode will cause an error due to missing GPIO.cleanup() call.
Step-by-Step Solution
Solution:
  1. Step 1: Trace LED pin 18 behavior

    Pin 18 is set HIGH first, turning its LED on, then after 1 second, pin 23 LED turns on.
  2. Step 2: Trace LED pin 23 behavior and turning off

    Pin 23 LED turns on after pin 18, then pin 18 LED turns off, then pin 23 LED turns off, each with 1-second delay.
  3. Final Answer:

    LED on pin 18 turns on, then LED on pin 23 turns on, then both turn off one by one. -> Option A
  4. Quick Check:

    Sequential LED on/off = LED on pin 18 turns on, then LED on pin 23 turns on, then both turn off one by one. [OK]
Quick Trick: Follow code line by line to track LED states [OK]
Common Mistakes:
  • Assuming LEDs turn on simultaneously
  • Ignoring sleep delays
  • Thinking missing cleanup causes runtime error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes