Push Button with Raspberry Pi
Interfacing Pushbuttons with Raspberry Pi for Digital Input Detection
Interfacing Pushbuttons with Raspberry Pi for Digital Input Detection demonstrates how pushbuttons can be connected to a Raspberry Pi board and their input states detected using Python programming.
A pushbutton is a momentary mechanical switch used to establish or interrupt an electrical connection when pressed.
In this experiment, two pushbuttons are connected to GPIO 7 and GPIO 8 of the Raspberry Pi. The Raspberry Pi detects which pushbutton is pressed and displays the corresponding message on the Python terminal.
Pushbuttons → Raspberry Pi GPIO Pins → Input Detection → Terminal Output
Theory
A pushbutton is a mechanical switch that is used to provide digital input to an electronic system.
A pushbutton generally has two operating conditions:
- Pressed: The switch establishes an electrical connection.
- Released: The switch returns to its normal open or closed condition, depending on its configuration.
The Raspberry Pi uses its General Purpose Input/Output (GPIO) pins to detect the state of external pushbuttons.
GPIO pins can be configured as digital inputs using the GPIO.IN configuration.
The GPIO.input() function is used to read the digital state of a GPIO pin.
- HIGH (1): Indicates a HIGH input signal.
- LOW (0): Indicates a LOW input signal.
In this experiment, two pushbuttons are connected to GPIO 7 and GPIO 8.
The Raspberry Pi continuously monitors both GPIO pins and displays the corresponding pushbutton status on the terminal.
Pushbutton Pressed → GPIO Input HIGH → Button Detection
Pushbutton Pins
A pushbutton generally has two electrical contacts that establish a connection when pressed.
| Pin | Connection | Function |
|---|---|---|
| Terminal 1 | Raspberry Pi GPIO input | Digital input signal |
| Terminal 2 | 3.3V | HIGH signal when pressed |
For this experiment, the pushbuttons are connected using external pull-down resistors.
When a pushbutton is pressed, the GPIO input receives 3.3V (HIGH). When released, the pull-down resistor maintains the GPIO input at LOW.
Pushbutton Input Detection
The Raspberry Pi provides GPIO pins that can be configured as digital inputs.
The GPIO.setup() function configures the selected GPIO pin as an input.
The GPIO.input() function reads the state of the selected GPIO pin.
The syntax is:
GPIO.setup(pin, GPIO.IN) GPIO.input(pin)
Where:
| Function / Parameter | Description |
|---|---|
| GPIO.setup() | Configures the selected GPIO pin |
| GPIO.IN | Configures the pin as a digital input |
| GPIO.input(pin) | Reads the digital state of the selected GPIO pin |
| True / HIGH | Indicates a HIGH input signal |
| False / LOW | Indicates a LOW input signal |
Applications
Pushbuttons are commonly used in:
- Electronic control panels
- Home automation systems
- Industrial control systems
- Digital switching applications
- User input interfaces
- Security and alarm systems
- Embedded system applications
Connections
In this experiment, two pushbuttons are connected to GPIO 7 and GPIO 8 of the Raspberry Pi.
| Device | Terminal | Raspberry Pi | Function |
|---|---|---|---|
| Pushbutton 1 | Terminal 1 | GPIO 7 (BCM) | Digital input 1 |
| Pushbutton 1 | Terminal 2 | 3.3V | HIGH signal |
| Pushbutton 2 | Terminal 1 | GPIO 8 (BCM) | Digital input 2 |
| Pushbutton 2 | Terminal 2 | 3.3V | HIGH signal |
| Pull-down Resistor 1 | One terminal | GPIO 7 | Input stabilization |
| Pull-down Resistor 1 | Other terminal | GND | LOW reference |
| Pull-down Resistor 2 | One terminal | GPIO 8 | Input stabilization |
| Pull-down Resistor 2 | Other terminal | GND | LOW reference |
GPIO Pin Details:
| Parameter | Pushbutton 1 | Pushbutton 2 |
|---|---|---|
| GPIO Numbering | BCM | BCM |
| GPIO Pin | GPIO 7 | GPIO 8 |
| Physical Pin | Pin 26 | Pin 24 |
Note: Use a separate 10K ohm pull-down resistor for each pushbutton. Connect one side of each resistor to GND and the corresponding GPIO input. Connect the pushbutton between 3.3V and the GPIO input. Never apply 5V to Raspberry Pi GPIO pins.
Algorithm
The following algorithm is used to detect two pushbuttons using Raspberry Pi:
- Start the Raspberry Pi.
- Open the Python programming environment or terminal.
- Import the RPi.GPIO and time libraries.
- Declare two variables to assign GPIO 7 and GPIO 8 for the pushbuttons.
- Configure GPIO numbering using BCM mode.
- Disable GPIO warnings.
- Configure both GPIO pins as INPUT.
- Start an infinite while loop.
- Read the input state of the first pushbutton using GPIO.input().
- If GPIO 7 is HIGH, display "Push button 1 is pressed".
- Otherwise, check the input state of the second pushbutton.
- If GPIO 8 is HIGH, display "Push button 2 is pressed".
- If both pushbuttons are LOW, display "Push button is not pressed".
- Provide a delay of 1 second.
- Repeat the process continuously.
The overall operation is:
Read GPIO 7 → Check Button 1 → Read GPIO 8 → Check Button 2 → Display Status → Repeat
Working
The Raspberry Pi continuously monitors the digital input states of GPIO 7 and GPIO 8.
The working sequence is:
- The RPi.GPIO library is imported to access the GPIO pins.
- The time library is imported to provide time delays.
- GPIO numbering is configured using BCM mode.
- GPIO 7 and GPIO 8 are configured as INPUT pins.
- The program enters an infinite while loop.
- The input state of GPIO 7 is checked.
- If GPIO 7 is HIGH, the message "push button 1 is pressed" is displayed.
- If GPIO 7 is LOW, the program checks GPIO 8.
- If GPIO 8 is HIGH, the message "push button 2 is pressed" is displayed.
- If both inputs are LOW, the message "push button is not pressed" is displayed.
- A delay of 1 second is provided.
- The process repeats continuously.
Pushbutton 1 → GPIO 7 → Input Detection
Pushbutton 2 → GPIO 8 → Input Detection
Observation
Pushbutton Input Detection Program
The following Python program is used to detect two pushbuttons connected to GPIO 7 and GPIO 8 of the Raspberry Pi.
import RPi.GPIO as GPIO
import time
pushbuttonPin = 7
pushbuttonPin2 = 8
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(pushbuttonPin, GPIO.IN)
GPIO.setup(pushbuttonPin2, GPIO.IN)
while True:
if GPIO.input(pushbuttonPin) == True:
print("push button 1 is pressed")
time.sleep(1)
elif GPIO.input(pushbuttonPin2) == True:
print("push button 2 is pressed")
time.sleep(1)
else:
print("push button is not pressed")
time.sleep(1)
GPIO.cleanup()
Program Explanation
- The RPi.GPIO library is imported to access the Raspberry Pi GPIO pins.
- The time library is imported to provide delays.
- The variable pushbuttonPin is assigned GPIO 7 for the first pushbutton.
- The variable pushbuttonPin2 is assigned GPIO 8 for the second pushbutton.
- GPIO.setmode(GPIO.BCM) selects BCM GPIO numbering.
- GPIO.setwarnings(False) disables GPIO warning messages.
- GPIO.setup() configures both GPIO pins as INPUT pins.
- The while True loop continuously monitors the pushbutton states.
- GPIO.input(pushbuttonPin) reads the state of the first pushbutton.
- If the first pushbutton is pressed, the message "push button 1 is pressed" is displayed.
- The elif condition checks whether the second pushbutton is pressed.
- If the second pushbutton is pressed, the message "push button 2 is pressed" is displayed.
- If neither pushbutton is pressed, the else condition displays "push button is not pressed".
- time.sleep(1) provides a delay of 1 second between successive readings.
- GPIO.cleanup() releases the GPIO resources when the program exits. It is normally reached after the loop is interrupted.
Observation Table
| Step | Pushbutton 1 (GPIO 7) | Pushbutton 2 (GPIO 8) | Terminal Output |
|---|---|---|---|
| 1 | Pressed (HIGH) | Released (LOW) | Push button 1 is pressed |
| 2 | Released (LOW) | Pressed (HIGH) | Push button 2 is pressed |
| 3 | Released (LOW) | Released (LOW) | Push button is not pressed |
| 4 | Pressed (HIGH) | Pressed (HIGH) | Push button 1 is pressed |
Note: Since the program uses if-elif conditions, when both pushbuttons are pressed simultaneously, only the first pushbutton message is displayed.
Result
The Raspberry Pi successfully interfaces with two pushbuttons using BCM GPIO 7 and GPIO 8. The input states of both pushbuttons are continuously monitored, and the corresponding messages are displayed on the Python terminal. Thus, digital input detection using pushbuttons is successfully implemented using Raspberry Pi GPIO pins and Python programming.