CalculateeLearning paths
Embedded Systems8–10 min read

GPIO, Pull-up Resistors and Button Debouncing

Read buttons reliably and understand floating inputs, active-low signals and switch bounce.

Mental model

A floating input is like a question with nobody assigned to answer. A pull-up provides the default answer; pressing a switch deliberately overrides it.

3.3 V10 kΩGPIO inputreleased = 1pressed = 0
The pull-up defines HIGH while the switch is open; pressing it creates a controlled path to ground.
UNDERSTAND THE BEHAVIOR
01

An input pin observes a voltage rather than supplying a defined logic value. If disconnected, capacitance, leakage and nearby electrical noise can change that voltage. A pull-up resistor weakly connects the input to its logic supply. A switch to ground then creates two defined conditions: released is high, pressed is low. This is called an active-low input.

02

The resistor limits current when the switch closes. With a 3.3 V supply and 10 kΩ pull-up, the closed switch carries approximately 0.33 mA. A direct wire from supply to ground would instead make a short circuit. Internal pull-ups are convenient but their resistance and availability depend on the chip; consult its documentation when leakage or power matters.

03

Mechanical contacts may open and close repeatedly during one press. Reading every edge as a new press can increment a counter several times. Debouncing accepts a new state only after it remains stable for a chosen interval. A value such as 20 ms is a starting point to measure and tune, not a universal property of every switch.

04

Separate the debounced level from the press event. A level stays true while held; an event occurs once when the stable level changes to pressed. This matters for menus and counters. Keep polling without long delays so other tasks can run, and verify that voltage on the input remains inside the device's allowed range.

Why it matters

Read buttons reliably and understand floating inputs, active-low signals and switch bounce.

Core ideas

  • INPUT_PULLUP normally makes a grounded button active-low.
  • Debounce the physical contact before counting presses.
  • A level and an edge are different kinds of information.
Key equationsClosed-switch pull-up current ≈ Vcc / R3.3 V / 10 kΩ = 0.33 mA
EXAMPLE

Work through a practical example

1Connect a button between the selected input and ground; use the board's supported internal pull-up.

2Record the time whenever the raw input changes.

3Accept the input after 20 ms without another change.

4Increment the application counter only on the accepted high-to-low transition.

Common mistake

Counting every low reading as a new press, or leaving the input unconnected without a pull resistor.

RememberINPUT_PULLUP normally makes a grounded button active-low.

Practice and check your understanding

Try each question before opening the worked answer.

1. With a pull-up and a switch to ground, which level means pressed?

Show answer and reasoning

LOW. The switch pulls the pin toward ground. Software should express pressed as digitalRead(pin) == LOW.

2. A button remains pressed for 2 seconds. How many press events should it create?

Show answer and reasoning

One, unless repeated actions while held are explicitly part of the design.

Continue learning

Technical references and further reading

References are for further study, not endorsements. Report an error or suggest an improvement.

Progress stays on this browser only. Clearing site data removes it; it does not sync across devices.