Home / Learn Embedded C / Reading a Push Button

Reading a Push Button

Detecting a button press reliably using GPIO input.

The short versionDetecting a button press reliably using GPIO input.

Theory in plain words

Definition

Reading a push button means configuring its pin as input (usually with a pull-up), then checking GPIO_READ against the "pressed" level — often LOW, if wired active-low with a pull-up.

Why you need this

Buttons are the most common physical user-input device in embedded projects, and getting the read polarity (active-high vs active-low) right is a frequent early source of "my button does nothing" bugs.

How it works

A button that connects the pin to ground when pressed, combined with an internal/external pull-up, reads HIGH when unpressed and LOW when pressed — the opposite of what beginners often expect.

Where you'll see this

  • User input handling
  • Mode-switching controls
  • Simple HMI (human-machine interface)

Try it

Reading an active-low button and reporting its stateC: edit and run

This sandbox's simulated GPIO_READ returns a fixed test value — on real hardware this would reflect the physical button state at read time.

Careful:Always double-check whether your button wiring is active-high or active-low before writing the condition — testing the physical behavior (print both states) is faster than guessing.

Try it: checking two buttons for a combo press

Detecting whether two buttons are held at the same timeC: edit and run

Reading several buttons is just repeating the single-button read pattern per pin and combining the results with logical AND/OR.

Test what you just read

1. With a button wired to ground and a pull-up resistor, what does GPIO_READ return when the button is PRESSED?