We use an LED toggle with a button to turn an LED on or off by pressing a button. It helps us control lights or signals easily.
LED toggle with button in Raspberry Pi
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) LED_PIN = 18 BUTTON_PIN = 17 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) time.sleep(0.3) # debounce delay
GPIO.BCM means we use the Broadcom pin numbering on the Raspberry Pi.
Pull-up resistor means the button input reads HIGH when not pressed, LOW when pressed.
GPIO.setup(LED_PIN, GPIO.OUT) GPIO.output(LED_PIN, True) # Turn LED on GPIO.output(LED_PIN, False) # Turn LED off
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) button_state = GPIO.input(BUTTON_PIN) if button_state == GPIO.LOW: print('Button pressed')
This program waits for you to press the button. Each press switches the LED on or off. It also prints the LED state to the screen. When you stop the program, it cleans up the pins safely.
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) LED_PIN = 18 BUTTON_PIN = 17 GPIO.setup(LED_PIN, GPIO.OUT) GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) led_state = False print('Press the button to toggle the LED. Press Ctrl+C to exit.') try: 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 now {'ON' if led_state else 'OFF'}") time.sleep(0.3) # debounce delay except KeyboardInterrupt: print('\nExiting program.') finally: GPIO.output(LED_PIN, False) GPIO.cleanup()
Use a small delay after detecting a button press to avoid reading the same press multiple times (debounce).
Always clean up GPIO pins at the end to avoid warnings or errors next time you run the program.
You can control an LED by reading a button press on Raspberry Pi.
Use GPIO pins with proper setup for input (button) and output (LED).
Toggle the LED state each time the button is pressed, with a small delay to avoid errors.
Practice
What is the main purpose of using a button in an LED toggle program on Raspberry Pi?
Solution
Step 1: Understand the role of the button
The button acts as an input device that the user can press to send a signal to the Raspberry Pi.Step 2: Connect button press to LED behavior
Each time the button is pressed, the program toggles the LED state (turns it on if off, or off if on).Final Answer:
To detect user input and change the LED state -> Option CQuick Check:
Button press controls LED toggle = B [OK]
- Thinking button powers the LED
- Confusing button with LED brightness control
- Assuming button resets the device
Which of the following is the correct way to set up a GPIO pin 17 as an output for the LED in Python using RPi.GPIO?
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
# What goes here?Solution
Step 1: Identify the function to configure pin mode
GPIO.setup(pin_number, mode) is used to set a pin as input or output.Step 2: Set pin 17 as output
Use GPIO.setup(17, GPIO.OUT) to configure pin 17 as an output for the LED.Final Answer:
GPIO.setup(17, GPIO.OUT) -> Option AQuick Check:
GPIO.setup with GPIO.OUT sets output pin = A [OK]
- Using GPIO.setup with GPIO.IN for output pin
- Confusing GPIO.output with GPIO.setup
- Using GPIO.input for output pin setup
What will be the output behavior of this code snippet controlling an LED and button on Raspberry Pi?
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
LED_PIN = 18
BUTTON_PIN = 23
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 == False:
led_state = not led_state
GPIO.output(LED_PIN, led_state)
time.sleep(0.3)Choose the correct description of the LED behavior.
Solution
Step 1: Understand button input reading
The button is set with a pull-up resistor, so pressing it makes input False (pressed).Step 2: Toggle LED state on button press
When button_state is False, led_state flips True/False and sets LED output accordingly, then waits 0.3 seconds to avoid bouncing.Final Answer:
LED toggles on and off each time the button is pressed -> Option BQuick Check:
Button press flips LED state = D [OK]
- Ignoring pull-up resistor effect on button input
- Missing delay causing multiple toggles
- Assuming LED always on or off
Identify the error in this Raspberry Pi LED toggle code snippet:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
LED_PIN = 21
BUTTON_PIN = 20
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.setup(BUTTON_PIN, GPIO.IN)
led_on = False
while True:
if GPIO.input(BUTTON_PIN) == False:
led_on = not led_on
GPIO.output(LED_PIN, led_on)
time.sleep(0.1)What is the main problem?
Solution
Step 1: Check button input setup
The button pin is set as input but lacks pull-up or pull-down resistor configuration, which can cause unstable readings.Step 2: Understand effect of missing resistor
Without pull-up/down, the input pin may float, causing random toggles of the LED.Final Answer:
Missing pull-up or pull-down resistor setup for the button input -> Option AQuick Check:
Button input needs pull-up/down resistor = A [OK]
- Forgetting pull-up/down resistor causes unstable input
- Assuming LED pin setup is wrong
- Thinking sleep time is the issue
You want to modify your Raspberry Pi LED toggle program so the LED only toggles when the button is pressed and released (not multiple toggles if held down). Which approach is best?
Solution
Step 1: Understand button press and hold behavior
Holding the button causes the input to stay low, triggering multiple toggles if not tracked.Step 2: Use previous state tracking
By saving the last button state, the program can detect the transition from released to pressed and toggle LED only once per press.Final Answer:
Add a variable to track previous button state and toggle LED only on state change from released to pressed -> Option DQuick Check:
Track button state changes to toggle once = C [OK]
- Using long delays instead of state tracking
- Removing pull-up resistor causing unstable input
- Busy waiting inside loops causing CPU load
Ready to prove it?
Pass the test and this pattern moves unknown
