0
0
Raspberry Piprogramming~20 mins

LED toggle with button in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Raspberry Pi LED Toggle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when the button is pressed once?

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?

ANo output
BLED is OFF
CLED is ON
D
LED is ON
LED is OFF
Attempts:
2 left
💡 Hint

Think about the initial state of the LED and what toggling means.

🧠 Conceptual
intermediate
1:30remaining
Why is there a delay after detecting the button press?

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?

ATo wait for the LED to physically turn on
BTo keep the LED on for 0.3 seconds
CTo reduce CPU usage by slowing the loop
DTo debounce the button and avoid multiple toggles from one press
Attempts:
2 left
💡 Hint

Think about what happens when a mechanical button is pressed and released quickly.

🔧 Debug
advanced
2:00remaining
What error will this code produce?

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?

ARuntimeError: No pull-up or pull-down resistor configured for BUTTON_PIN
BSyntaxError due to missing colon
CTypeError when toggling led_state
DNo error, code runs fine
Attempts:
2 left
💡 Hint

Think about the button input setup and electrical noise.

📝 Syntax
advanced
1:30remaining
Which option fixes the syntax error in this code?

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)
AAdd colon after while True: (while True:)
BAdd parentheses to if statement (if (GPIO.input(BUTTON_PIN) == GPIO.LOW):)
CRemove the print statement
DChange GPIO.setup to GPIO.setup(LED_PIN, GPIO.OUT, pull_up_down=GPIO.PUD_UP)
Attempts:
2 left
💡 Hint

Check the line that starts the while loop.

🚀 Application
expert
2:30remaining
How many times will the LED toggle if the button is held down for 2 seconds?

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?

A1 time
B7 times
C5 times
D10 times
Attempts:
2 left
💡 Hint

Calculate how many toggle cycles fit in 2 seconds considering the delays.