3. Controlling GPIO Input with a Tactile Switch¶
This guide explains how to use a tactile switch (push button) with the Raspberry Pi's GPIO pins. We'll use a C++ program that utilizes the Linux GPIO v2 character device interface to detect button presses.

Required Hardware¶
- Raspberry Pi (any model)
- Tactile switch/push button (1)
- Jumper wires
- Breadboard (optional, but recommended)
Hardware Setup¶
This guide uses GPIO pin 22 for the tact switch connection. Connect the components as follows:
- Connect one leg of the tact switch to GPIO 22
- Connect the other leg of the tact switch to GND (ground)
graph LR
subgraph RPi["Raspberry Pi"]
GPIO22["GPIO 22 (with internal pull-up)"]
GND["GND"]
end
GPIO22 --- Switch["Tactile Switch"]
Switch --- GND
style Switch fill:#4a90e2
How it Works¶
When you press the button, it connects GPIO 22 to GND, creating a LOW signal. The program detects this state change and responds accordingly.
The code uses internal pull-up resistors, so no external resistors are required. When the button is not pressed, the pin reads HIGH; when pressed, it reads LOW (active-low configuration).
Code Explanation¶
This program uses the Linux GPIO v2 Character Device API to monitor a tact switch:
Key Program Elements¶
-
GPIO Line Configuration:
- The program configures GPIO pin 22 as an input with a pull-up resistor
- The pull-up resistor ensures the pin reads HIGH when the button is not pressed
-
Button State Detection:
- The program polls the GPIO pin at 100ms intervals
- It uses active-LOW logic: 0 (LOW) means button pressed, 1 (HIGH) means button released
-
GPIO v2 API:
- Uses the newer GPIO v2 API, which provides better features compared to the older interface
- Includes proper error handling and resource cleanup
-
Debouncing Considerations:
- The 100ms polling interval provides basic debouncing
- For a production application, more sophisticated debouncing might be needed
Compiling and Running¶
-
Save the code to a file, e.g.,
gpio_button.cpp -
Compile the program:
-
Run the program with root privileges (needed for GPIO access):
-
The program will wait until you press the button, then exit.
How It Works in Detail¶
-
Internal Pull-up Resistor: The program enables an internal pull-up resistor on GPIO 22, which keeps the pin HIGH when the button is not pressed.
-
Ground Connection: When the button is pressed, it connects GPIO 22 to ground, pulling the pin LOW.
-
Detection Logic: The program constantly checks the pin value and detects when it goes LOW.
-
Polling Approach: This program uses polling rather than interrupts. While not as efficient, it's simpler to implement.
Advanced Considerations¶
-
Interrupt-based Implementation: For better efficiency, consider using GPIO interrupts instead of polling.
-
Debouncing: Mechanical switches often "bounce," causing multiple rapid transitions. Consider implementing a more robust debouncing algorithm.
-
Multiple Buttons: The GPIO v2 API supports monitoring multiple lines simultaneously. You can extend this example to handle several buttons.
-
Error Handling: The program includes basic error handling. In a production application, you might want more comprehensive error management.
Troubleshooting¶
-
Permission Issues: If you get "Failed to open GPIO chip device," ensure you're running the program with sudo.
-
Button Not Detected: Check your wiring and ensure the button connects to the correct GPIO pin and ground.
-
Inconsistent Behavior: If the button press detection is inconsistent, implement a better debouncing solution.