You want to blink an LED connected to GPIO 22 every second using gpiozero. Which code correctly achieves this?
hard📝 Application Q8 of 15
Raspberry Pi - gpiozero Library
You want to blink an LED connected to GPIO 22 every second using gpiozero. Which code correctly achieves this?
Afrom gpiozero import LED
led = LED(22)
while True:
led.on()
sleep(1)
led.off()
sleep(1)
Bfrom gpiozero import LED
led = LED(22)
led.blink(on_time=1, off_time=1)
Cfrom gpiozero import LED
led = LED(22)
led.blink(1, 1, n=0)
Dfrom gpiozero import LED
led = LED(22)
led.blink(1)
Step-by-Step Solution
Solution:
Step 1: Understand blinking methods
gpiozero's LED class has a blink() method with on_time and off_time parameters.
Step 2: Analyze options
from gpiozero import LED
led = LED(22)
while True:
led.on()
sleep(1)
led.off()
sleep(1) uses manual loop but lacks import for sleep, causing NameError. from gpiozero import LED
led = LED(22)
led.blink(1) calls blink with one argument (sets off_time=0.5s mismatch). from gpiozero import LED
led = LED(22)
led.blink(on_time=1, off_time=1) correctly uses blink(on_time=1, off_time=1). from gpiozero import LED
led = LED(22)
led.blink(1, 1, n=0) sets n=0 causing no continuous blinking.
Final Answer:
from gpiozero import LED
led = LED(22)
led.blink(on_time=1, off_time=1) -> Option B
Quick Check:
Use blink(on_time, off_time) = D [OK]
Quick Trick:Use led.blink(on_time=1, off_time=1) to blink LED [OK]
Common Mistakes:
Using manual loops instead of blink()
Passing wrong number of arguments to blink()
Adding invalid extra arguments
Master "gpiozero Library" in Raspberry Pi
9 interactive learning modes - each teaches the same concept differently