Button Operation Principal
A push button functions by connecting or disconnecting a circuit. When pressed, the circuit closes; when released, it opens. To read this state via a Genarel Purpose Input/Output (GPIO) pin, the pin must not be left floating, as ambient electrical noise could cause erratic readings. To prevent this, a pull-up or pull-down resistor is used to maintain a stable voltage level.
Pull-Up and Pull-Down Resistors
With a pull-up resistor configuration, the GPIO pin reads HIGH when the button is not pressed. Pressing the button connects the pin to ground, causing a LOW reading (a falling edge). Conversely, a pull-down resistor keeps the pin LOW when idle, and pressing the button connects it to a voltage source, resulting in a HIGH reading (a rising edge).
Wiring Configuration
Connect an LED to GPIO pin 11 (physical pin 11). Connect two push buttons to GPIO pins 13 and 15. This example uses the Raspberry Pi's internal pull-up resistors, so external resistors are not necessary. The desired behavior is:
- Pressing the first button (on pin 13) turns the LED on.
- Pressing the second button (on pin 15) turns the LED off.
Code Example 1: On/Off Control
import RPi.GPIO as GPIO
import time
# Use physical pin numbering
GPIO.setmode(GPIO.BOARD)
# Configure LED pin as output
led_output = 11
GPIO.setup(led_output, GPIO.OUT)
# Configure button pins as input with internal pull-up resistors
button_on = 13
button_off = 15
GPIO.setup(button_on, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(button_off, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Initialize LED to off
GPIO.output(led_output, GPIO.HIGH)
try:
while True:
# Check if 'on' button is pressed (LOW signal)
if GPIO.input(button_on) == GPIO.LOW:
GPIO.output(led_output, GPIO.LOW) # Turn LED on
# Check if 'off' button is pressed (LOW signal)
if GPIO.input(button_off) == GPIO.LOW:
GPIO.output(led_output, GPIO.HIGH) # Turn LED off
time.sleep(0.01) # Debounce delay
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()
Code Example 2: Momentary Control and Exit
This example configures the first button to turn the LED on only while pressed. The second button cleans up resources and exits the program.
import RPi.GPIO as GPIO
import time
import sys
GPIO.setmode(GPIO.BOARD)
led_pin = 11
GPIO.setup(led_pin, GPIO.OUT)
GPIO.output(led_pin, GPIO.HIGH) # Start with LED off
btn_momentary = 13
btn_exit = 15
GPIO.setup(btn_momentary, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(btn_exit, GPIO.IN, pull_up_down=GPIO.PUD_UP)
try:
while True:
# Momentary control: LED on when pressed, off when released
if GPIO.input(btn_momentary) == GPIO.LOW:
GPIO.output(led_pin, GPIO.LOW)
else:
GPIO.output(led_pin, GPIO.HIGH)
# Exit button: cleanup and terminate
if GPIO.input(btn_exit) == GPIO.LOW:
GPIO.cleanup()
sys.exit(0)
time.sleep(0.01)
except Exception as err:
print(err)
finally:
GPIO.cleanup()
A toggle function, where one button togggles the LED state, requires a boolean flag to track the current state. This can be implemented by modifying the polling loop.