Consider this Python code running on a Raspberry Pi. It toggles an LED on or off each time a button is pressed.
import RPi.GPIO as GPIO
import time
LED_PIN = 18
BUTTON_PIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
led_state = False
while True:
button_state = GPIO.input(BUTTON_PIN)
if button_state == GPIO.LOW:
led_state = not led_state
GPIO.output(LED_PIN, led_state)
print(f"LED is {'ON' if led_state else 'OFF'}")
time.sleep(0.3) # debounce delay
time.sleep(0.1)What will be printed after pressing the button once?
Think about the initial state of the LED and what toggling means.
The LED starts off (False). Pressing the button toggles it to True, so the output is "LED is ON".
In the LED toggle code, there is a time.sleep(0.3) after toggling the LED when the button is pressed. Why is this delay important?
Think about what happens when a mechanical button is pressed and released quickly.
Mechanical buttons can cause multiple rapid signals (bounces) when pressed. The delay helps ignore these extra signals so the LED toggles only once per press.
Look at this code snippet for toggling an LED with a button on Raspberry Pi:
import RPi.GPIO as GPIO
import time
LED_PIN = 18
BUTTON_PIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.setup(BUTTON_PIN, GPIO.IN)
led_state = False
while True:
if GPIO.input(BUTTON_PIN) == GPIO.LOW:
led_state = not led_state
GPIO.output(LED_PIN, led_state)
print(f"LED is {'ON' if led_state else 'OFF'}")
time.sleep(0.3)
time.sleep(0.1)What error or problem will this code cause when running?
Think about the button input setup and electrical noise.
The button pin is set as input but no pull-up or pull-down resistor is configured. This can cause a RuntimeError or unstable input readings on Raspberry Pi.
Find the option that fixes the syntax error in this LED toggle code snippet:
import RPi.GPIO as GPIO
import time
LED_PIN = 18
BUTTON_PIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
led_state = False
while True
if GPIO.input(BUTTON_PIN) == GPIO.LOW:
led_state = not led_state
GPIO.output(LED_PIN, led_state)
print(f"LED is {'ON' if led_state else 'OFF'}")
time.sleep(0.3)
time.sleep(0.1)Check the line that starts the while loop.
The while loop is missing a colon at the end of the line. Adding it fixes the syntax error.
Given this code snippet:
import RPi.GPIO as GPIO
import time
LED_PIN = 18
BUTTON_PIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
led_state = False
start_time = time.time()
while time.time() - start_time < 2:
if GPIO.input(BUTTON_PIN) == GPIO.LOW:
led_state = not led_state
GPIO.output(LED_PIN, led_state)
print(f"LED is {'ON' if led_state else 'OFF'}")
time.sleep(0.3) # debounce delay
time.sleep(0.1)If the button is pressed and held down continuously for 2 seconds, how many times will the LED toggle?
Calculate how many toggle cycles fit in 2 seconds considering the delays.
The loop runs for 2 seconds. Each toggle cycle takes about 0.3 + 0.1 = 0.4 seconds. 2 / 0.4 = 5 cycles approximately, but the first toggle happens immediately, so total toggles are about 7.