0
0
Iot-protocolsProgramBeginner · 2 min read

Raspberry Pi Program to Control LED with Button

Use the gpiozero library to control an LED with a button on Raspberry Pi by creating LED and Button objects and linking the button press to turn the LED on or off, e.g., button.when_pressed = led.on and button.when_released = led.off.
📋

Examples

InputPress the button once
OutputLED turns on
InputRelease the button after pressing
OutputLED turns off
InputDo not press the button
OutputLED stays off
🧠

How to Think About It

To control an LED with a button on Raspberry Pi, connect the LED and button to GPIO pins. Use a program to watch the button state. When the button is pressed, turn the LED on; when released, turn it off. This uses simple input-output control with the GPIO pins.
📐

Algorithm

1
Set up the LED pin as output and the button pin as input.
2
Wait for the button to be pressed.
3
When the button is pressed, turn the LED on.
4
When the button is released, turn the LED off.
5
Keep the program running to listen for button events.
💻

Code

python
from gpiozero import LED, Button
from signal import pause

led = LED(17)  # Connect LED to GPIO17
button = Button(2)  # Connect button to GPIO2

button.when_pressed = led.on
button.when_released = led.off

print('Press the button to control the LED')
pause()
Output
Press the button to control the LED
🔍

Dry Run

Let's trace pressing and releasing the button through the code

1

Program starts

LED connected to GPIO17 is off; button connected to GPIO2 is waiting for press.

2

Button pressed

button.when_pressed triggers led.on(), LED turns on.

3

Button released

button.when_released triggers led.off(), LED turns off.

EventLED State
Program startOff
Button pressedOn
Button releasedOff
💡

Why This Works

Step 1: Setup GPIO pins

The LED and Button objects link to specific GPIO pins to control hardware.

Step 2: Button press event

When the button is pressed, the program runs led.on() to light the LED.

Step 3: Button release event

When the button is released, the program runs led.off() to turn the LED off.

🔄

Alternative Approaches

Using RPi.GPIO library with polling
python
import RPi.GPIO as GPIO
import time

LED_PIN = 17
BUTTON_PIN = 2

GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)

try:
    while True:
        if GPIO.input(BUTTON_PIN) == GPIO.LOW:
            GPIO.output(LED_PIN, GPIO.HIGH)
        else:
            GPIO.output(LED_PIN, GPIO.LOW)
        time.sleep(0.1)
except KeyboardInterrupt:
    GPIO.cleanup()
This method uses manual checking (polling) of button state in a loop, which is less efficient but shows basic GPIO control.
Using gpiozero with hold_time for long press
python
from gpiozero import LED, Button
from signal import pause

led = LED(17)
button = Button(2, hold_time=2)

button.when_held = led.toggle

print('Hold button for 2 seconds to toggle LED')
pause()
This approach toggles the LED on a long button hold, showing more advanced event handling.

Complexity: O(1) time, O(1) space

Time Complexity

The program reacts to button events without loops that depend on input size, so it runs in constant time.

Space Complexity

Only a few variables for GPIO pins and objects are used, so space is constant.

Which Approach is Fastest?

Using gpiozero event handlers is more efficient than polling loops because it waits for hardware events.

ApproachTimeSpaceBest For
gpiozero event handlersO(1)O(1)Simple, efficient event-driven control
RPi.GPIO polling loopO(1)O(1)Basic control, easy to understand but less efficient
gpiozero hold_time toggleO(1)O(1)Advanced button event handling with long press
💡
Use the gpiozero library for simple and clean Raspberry Pi GPIO control with buttons and LEDs.
⚠️
Forgetting to set the button pin with a pull-up or pull-down resistor can cause unreliable button readings.