How to Read Push Button in Embedded C: Simple Guide
To read a push button in
embedded C, configure the button pin as input, then read its state using if statements checking the pin's input register. The button press is detected when the input reads low or high depending on the wiring.Syntax
To read a push button in embedded C, you typically:
- Set the button pin as input.
- Read the pin's input register.
- Use conditional checks to detect button press.
Example syntax:
if (BUTTON_PIN == PRESSED_STATE) {
// Button is pressed
}Here, BUTTON_PIN is the input register bit, and PRESSED_STATE is usually 0 or 1 depending on wiring.
c
if (BUTTON_PIN == PRESSED_STATE) { // Button is pressed }
Example
This example shows how to configure a push button connected to pin P1.0 as input and read its state in a loop. It assumes the button connects the pin to ground when pressed (active low).
c
#include <stdint.h> #include <stdbool.h> #define BUTTON_PIN (*(volatile uint8_t *)0x20) // Example input register address #define BUTTON_BIT 0 #define BUTTON_PRESSED 0 // Active low button int main(void) { // Configure P1.0 as input (hardware specific, example only) // Assume DDR register at 0x21, clear bit 0 for input volatile uint8_t *DDR = (volatile uint8_t *)0x21; volatile uint8_t *PORT = (volatile uint8_t *)0x22; // Added PORT definition for pull-up *DDR &= ~(1 << BUTTON_BIT); *PORT |= (1 << BUTTON_BIT); // Enable pull-up resistor while (1) { if (((BUTTON_PIN >> BUTTON_BIT) & 1) == BUTTON_PRESSED) { // Button pressed // Insert code to handle press } else { // Button not pressed } } return 0; }
Common Pitfalls
Common mistakes when reading push buttons include:
- Not configuring the pin as input, causing wrong readings.
- Ignoring button debounce, which causes multiple false triggers.
- Assuming wrong logic level for pressed state (active low vs active high).
- Not enabling internal pull-up resistors if needed, leading to floating inputs.
Example of wrong and right approach:
c
// Wrong: Not setting pin as input // Reading pin without configuration if (BUTTON_PIN == 0) { // May not work correctly } // Right: Configure pin as input and use pull-up *DDR &= ~(1 << BUTTON_BIT); // Set input *PORT |= (1 << BUTTON_BIT); // Enable pull-up if (((BUTTON_PIN >> BUTTON_BIT) & 1) == 0) { // Button pressed (active low) }
Quick Reference
- Configure pin as input: Clear DDR bit for the button pin.
- Enable pull-up resistor: Set PORT bit if hardware supports it.
- Read pin state: Check input register bit.
- Detect press: Compare pin state to pressed logic level (usually 0 for active low).
- Debounce: Add delay or software filtering to avoid false triggers.
Key Takeaways
Always configure the button pin as input before reading its state.
Know if your button is active low or active high to check the correct logic level.
Enable internal pull-up resistors if your hardware supports it to avoid floating inputs.
Implement debounce logic to prevent multiple false button presses.
Read the input register bit and compare it to the pressed state to detect button presses.