Skip to content

Reading a button

Floating inputs, pull-up resistors, and why a single press can register as several.

Explanation

A pin can also be an input. Your program reads it and gets a 1 or a 0. The usual wiring is a button between the pin and ground, so a pressed button reads 0. The question is what the pin reads when the button is released.

An input that is connected to nothing is called floating. It has no defined level and picks up electrical noise: the wire acts as a small antenna, and touching the board changes the reading. A floating input reads random 0s and 1s. This is the most common surprise for beginners.

The solution is a pull-up resistor: a resistor from the pin to the supply, usually 10 kOhm. When the button is released, the resistor holds the pin high and it reads 1. When the button is pressed, it connects the pin to ground and the pin reads 0. The resistor limits the current that flows while the button is pressed. Nearly every MCU has an internal pull-up that you can enable in software, so the pull-up is often one line of configuration rather than a separate part.

Note that the logic is inverted: pressed reads 0. This is normal and is called active low. LEDs are sometimes wired active low too, with the LED between the supply and the pin, so that writing 0 turns the LED on. Read the schematic first, then write the code to match.

Mechanical buttons also do not close cleanly. The metal contacts bounce open and closed for a few milliseconds before they settle, so a program that counts edges can see 3, 5 or 10 presses for a single push. This is called contact bounce. The usual fix is in software: after the first change, ignore the pin for 20 to 50 ms, or require the same reading several times in a row before accepting it.

Key points

  • An unconnected input floats and reads random values.
  • A pull-up, often internal, holds a released button high, so pressed reads 0. This is called active low, and it is normal.
  • Match the code to the schematic: a 0 can mean on.
  • Buttons bounce for a few milliseconds. Debounce in software with a short ignore window.

Check your understanding

4 questions on this lesson. An explanation follows each answer.

Sign in to answer the questions. Your answers count towards your level.

Sign in

You can read the lesson and the project without an account.

Project

Button counter with debounce

What you need

  • Your board, the breadboard, and an LED with its resistor from lesson 2.
  • A push button.

Steps

  1. 1Wire the button between a pin and ground. Enable the internal pull-up for that pin in code. Print or blink the raw reading, and confirm that it reads 1 when released and 0 when pressed.
  2. 2Disable the pull-up and touch the board. Observe that the reading becomes random. Then enable the pull-up again.
  3. 3Count the presses without any debounce and show the count, by serial print if your board supports it, or by blinking the LED that many times. Press slowly and note that the count sometimes increases by more than one.
  4. 4Add a 30 ms debounce and check that each press is counted exactly once.

Done when

Pressing the button 20 times gives a count of exactly 20, and you have seen a floating input give random readings.