Tutorial 3- Button Input
In this tutorial, we will learn how to connect a button to the Raspberry Pi to control an LED.
Introduction
Push buttons are one of the most common ways for a user to provide input. In this tutorial, we’ll learn how to use a button with the Raspberry Pi 5 board. We’ll write a Micropython program that reads the button’s state and prints a message when it is pressed.
Components Needed
Component | Quantity |
---|---|
Raspberry Pi 5 | 1 |
Breadboard | 1 |
Wires | Several |
LED | 1 |
300 - 1K Ohm Resistor | 1 |
Fritzing Diagram
Connect the Button and LED to your Raspberry Pi as shown in the following diagram.
Code
from gpiozero import LED, Button
from time import sleep
# Initialize an LED connected to GPIO pin 17 and a button connected to GPIO pin 18
led = LED(17)
button = Button(18)
# Keep track of the LED state
led_status = False
print("Press the button to toggle the LED. Press Ctrl+C to exit.")
while True:
if button.is_pressed: # Check if the button is pressed
led_status = not led_status # Toggle the LED status
if led_status:
led.on() # Turn the LED on
print('LED ON')
else:
led.off() # Turn the LED off
print('LED OFF')
sleep(0.5) # Debounce delay to prevent multiple toggles in a short time
Code Explanation
Importing Libraries
from gpiozero import LED, Button
from time import sleep
from gpiozero import LED, Button: This line imports the LED and Button classes from the gpiozero library, which simplifies working with GPIO pins on the Raspberry Pi.
from time import sleep: This imports the sleep function from the time module, which is used to pause the program for a specified number of seconds.
Initialization
led = LED(17)
button = Button(18)
led = LED(17): This creates an instance of the LED class, associating it with GPIO pin 17. This pin will control the LED.
button = Button(18): This creates an instance of the Button class, associating it with GPIO pin 18. This pin will read the button’s state (pressed or not).
LED State Tracking
led_status = False
led_status = False: This initializes a boolean variable that keeps track of the LED’s current state (on or off). It starts as False, indicating the LED is initially off.
User Instructions
print("Press the button to toggle the LED. Press Ctrl+C to exit.")
This line prints a message to the console, informing the user how to interact with the program and how to exit it.
Main Loop
while True:
This starts an infinite loop that will continuously run until the program is manually stopped. It allows the program to keep checking the button’s state.
Button Press Check
if button.is_pressed: # Check if the button is pressed
if button.is_pressed: This line checks if the button is currently pressed. If it is, the following block of code will execute.
Toggle LED Status
led_status = not led_status # Toggle the LED status
led_status = not led_status: This line toggles the led_status variable. If it was False (LED off), it becomes True (LED on), and vice versa.
Control LED Based on Status
if led_status:
led.on() # Turn the LED on
print('LED ON')
else:
led.off() # Turn the LED off
print('LED OFF')
if led_status: This checks if led_status is True. If True, it calls led.on() to turn the LED on and prints “LED ON”. If False, it calls led.off() to turn the LED off and prints “LED OFF”.
Debounce Delay
sleep(0.5) # Debounce delay to prevent multiple toggles in a short time
sleep(0.5): This line pauses the program for 0.5 seconds. This delay helps to prevent “bouncing,” where the button may register multiple presses from a single press due to mechanical vibrations. It ensures that the LED toggles only once per button press.
Conclusion
This code sets up a simple program to control an LED with a button using the GPIO pins on a Raspberry Pi. When the button is pressed, the LED toggles between on and off states, and the current state is printed to the console. The program runs in a loop, continuously checking for button presses until it is stopped.