Bird
0
0

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📝 Application Q15 of 15
Raspberry Pi - Platform
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?
Aimport 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()
Bimport 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()
Cimport 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()
Dimport 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()
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes