Skip to content
Sign in
Theme

Reading a button

Floating inputs, pull-up resistors and why one press can look like ten.

Read this first

The same pin can be an input. Your program reads it and gets a 1 or a 0. The obvious wiring is a button between the pin and ground: pressed reads 0. The catch is what the pin reads when the button is not pressed.

An input connected to nothing is called floating. It has no defined level, so it picks up whatever is around: 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 number one beginner surprise.

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

Notice the logic is inverted: pressed is 0. That is completely normal and it 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 it on. Read the schematic, then write the code to match.

The second surprise: a mechanical button does not close cleanly. The metal contacts bounce open and closed for a few milliseconds before they settle, so a program that counts edges sees 3, 5 or 10 presses for one push. This is called contact bounce, and 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.

To remember

  • An unconnected input floats and reads random values.
  • A pull-up (often internal) holds a released button high, so pressed reads 0. That is active low and it is normal.
  • Wire 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 what you read

4 questions from the question bank on the ideas above. Each one comes with an explanation after you answer.

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

Sign in

The lesson text and the project are free to read without an account.

Build this

Button counter with debounce

You need

  • Your board, breadboard and one LED with its resistor from lesson 2.
  • One push button.

Steps

  1. Wire 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 it reads 1 released and 0 pressed.
  2. Disable the pull-up, and touch the board. Watch the reading go random. Enable it again.
  3. Count the presses without any debounce and show the count (serial print if your board has it, or blink the LED that many times). Press slowly and see counts jump by more than one.
  4. Add a 30 ms debounce and check that every press counts exactly once.

Done when

Twenty presses give a count of exactly twenty, and you saw the floating input misbehave with your own eyes.