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()
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:
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.
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.
Step 3: Cleanup GPIO pins after use
Calling GPIO.cleanup() resets the pins safely after blinking finishes.
Final Answer:
Code in option A correctly blinks two LEDs alternately for 5 cycles. -> Option A
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
Master "Platform" in Raspberry Pi
9 interactive learning modes - each teaches the same concept differently