You want to blink an LED connected to GPIO pin 12 five times with 0.5 seconds on and 0.5 seconds off. Which code snippet correctly implements this?
hard📝 Application Q8 of 15
Raspberry Pi - LED and Button Projects
You want to blink an LED connected to GPIO pin 12 five times with 0.5 seconds on and 0.5 seconds off. Which code snippet correctly implements this?
Aimport RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)
for _ in range(5):
GPIO.output(12, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(12, GPIO.LOW)
time.sleep(0.5)
Cimport RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.IN)
for _ in range(5):
GPIO.output(12, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(12, GPIO.LOW)
time.sleep(0.5)
Dimport RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)
GPIO.output(12, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(12, GPIO.LOW)
time.sleep(0.5)
Step-by-Step Solution
Solution:
Step 1: Identify the need for repeated on/off cycles
Blinking 5 times requires a loop that turns LED on and off with delays.
Step 2: Check code correctness
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)
for _ in range(5):
GPIO.output(12, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(12, GPIO.LOW)
time.sleep(0.5) uses a for loop with GPIO.output HIGH and LOW and 0.5s sleep, matching requirements.
Final Answer:
Option A correctly blinks LED 5 times with 0.5s intervals -> Option A
Quick Check:
Loop with GPIO.HIGH/LOW and sleep = blink [OK]
Quick Trick:Use a loop with sleep to blink LED multiple times [OK]
Common Mistakes:
Setting pin as input instead of output
Missing loop for multiple blinks
Not importing time module
Master "LED and Button Projects" in Raspberry Pi
9 interactive learning modes - each teaches the same concept differently