Bird
Raised Fist0
Raspberry Piprogramming~30 mins

LED toggle with button in Raspberry Pi - Mini Project: Build & Apply

Choose your learning style10 modes available
LED toggle with button
📖 Scenario: You have a Raspberry Pi connected to an LED and a button. You want to control the LED so that each time you press the button, the LED turns on if it was off, and turns off if it was on.
🎯 Goal: Build a Python program that reads the button press and toggles the LED state accordingly.
📋 What You'll Learn
Use the gpiozero library to control the LED and button.
Create an LED object connected to GPIO pin 17.
Create a Button object connected to GPIO pin 2.
Toggle the LED state each time the button is pressed.
Print LED is ON or LED is OFF after each toggle.
💡 Why This Matters
🌍 Real World
This project shows how to control physical devices like LEDs using buttons, which is common in home automation and interactive electronics.
💼 Career
Understanding GPIO control and event-driven programming is useful for embedded systems, IoT development, and hardware prototyping jobs.
Progress0 / 4 steps
1
Set up LED and Button objects
Import LED and Button from gpiozero. Create an LED object called led connected to GPIO pin 17. Create a Button object called button connected to GPIO pin 2.
Raspberry Pi
Hint

Use from gpiozero import LED, Button to import. Then create led = LED(17) and button = Button(2).

2
Create a function to toggle the LED
Define a function called toggle_led that toggles the led state. Inside the function, use led.toggle() to change the LED state. Then use an if statement to print LED is ON if led.is_lit is True, otherwise print LED is OFF.
Raspberry Pi
Hint

Define toggle_led() that calls led.toggle(). Use if led.is_lit: to check LED state and print the correct message.

3
Connect the button press to toggle the LED
Use button.when_pressed = toggle_led to call the toggle_led function each time the button is pressed.
Raspberry Pi
Hint

Assign toggle_led to button.when_pressed to run the function on button press.

4
Keep the program running to listen for button presses
Import pause from signal and call pause() at the end of the program to keep it running and listening for button presses.
Raspberry Pi
Hint

Use from signal import pause and call pause() to keep the program running.

Practice

(1/5)
1.

What is the main purpose of using a button in an LED toggle program on Raspberry Pi?

easy
A. To provide power to the LED
B. To increase the brightness of the LED
C. To detect user input and change the LED state
D. To reset the Raspberry Pi

Solution

  1. 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.
  2. 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).
  3. Final Answer:

    To detect user input and change the LED state -> Option C
  4. Quick Check:

    Button press controls LED toggle = B [OK]
Hint: Button input changes LED state on press [OK]
Common Mistakes:
  • Thinking button powers the LED
  • Confusing button with LED brightness control
  • Assuming button resets the device
2.

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?
easy
A. GPIO.setup(17, GPIO.OUT)
B. GPIO.setup(17, GPIO.IN)
C. GPIO.output(17, GPIO.OUT)
D. GPIO.input(17, GPIO.OUT)

Solution

  1. Step 1: Identify the function to configure pin mode

    GPIO.setup(pin_number, mode) is used to set a pin as input or output.
  2. Step 2: Set pin 17 as output

    Use GPIO.setup(17, GPIO.OUT) to configure pin 17 as an output for the LED.
  3. Final Answer:

    GPIO.setup(17, GPIO.OUT) -> Option A
  4. Quick Check:

    GPIO.setup with GPIO.OUT sets output pin = A [OK]
Hint: Use GPIO.setup(pin, GPIO.OUT) for output pins [OK]
Common Mistakes:
  • Using GPIO.setup with GPIO.IN for output pin
  • Confusing GPIO.output with GPIO.setup
  • Using GPIO.input for output pin setup
3.

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.

medium
A. LED blinks continuously without button press
B. LED toggles on and off each time the button is pressed
C. LED stays always off
D. LED stays always on

Solution

  1. Step 1: Understand button input reading

    The button is set with a pull-up resistor, so pressing it makes input False (pressed).
  2. 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.
  3. Final Answer:

    LED toggles on and off each time the button is pressed -> Option B
  4. Quick Check:

    Button press flips LED state = D [OK]
Hint: Button press flips LED state with delay to avoid bounce [OK]
Common Mistakes:
  • Ignoring pull-up resistor effect on button input
  • Missing delay causing multiple toggles
  • Assuming LED always on or off
4.

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?

medium
A. Missing pull-up or pull-down resistor setup for the button input
B. LED pin is not set as output
C. time.sleep() is too long
D. led_on variable is not initialized

Solution

  1. 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.
  2. Step 2: Understand effect of missing resistor

    Without pull-up/down, the input pin may float, causing random toggles of the LED.
  3. Final Answer:

    Missing pull-up or pull-down resistor setup for the button input -> Option A
  4. Quick Check:

    Button input needs pull-up/down resistor = A [OK]
Hint: Always set pull-up/down for button input pins [OK]
Common Mistakes:
  • Forgetting pull-up/down resistor causes unstable input
  • Assuming LED pin setup is wrong
  • Thinking sleep time is the issue
5.

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?

hard
A. Remove the pull-up resistor from the button pin
B. Increase the delay after toggling LED to 5 seconds
C. Use a while loop inside the main loop to wait until button is released
D. Add a variable to track previous button state and toggle LED only on state change from released to pressed

Solution

  1. Step 1: Understand button press and hold behavior

    Holding the button causes the input to stay low, triggering multiple toggles if not tracked.
  2. 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.
  3. Final Answer:

    Add a variable to track previous button state and toggle LED only on state change from released to pressed -> Option D
  4. Quick Check:

    Track button state changes to toggle once = C [OK]
Hint: Track previous button state to toggle once per press [OK]
Common Mistakes:
  • Using long delays instead of state tracking
  • Removing pull-up resistor causing unstable input
  • Busy waiting inside loops causing CPU load
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard