All lessons
🔘Components 6 min read

Buttons

How the Arduino knows you pressed something — plus the two classic beginner traps.

In 10 seconds

  • Buttons connect two wires while pressed.
  • Use INPUT_PULLUP so the pin never floats.
  • With a pull-up, pressed reads LOW.

A push button is the simplest input there is. Inside the little plastic square is a springy metal strip. Press it and the strip touches two contacts, joining them together; let go and the spring pulls it back apart. That is the entire mechanism. Everything interesting happens in how you connect it and how you read it.

The four-legged mystery

Most tactile buttons have four legs, which puzzles everyone at first. There are not four separate connections — the legs are joined in pairs, and pressing the button connects the two pairs together. The pairs run across the button, not along it. If your button seems permanently pressed, rotate it 90 degrees on the breadboard and try again.

Floating pins, the first trap

Suppose you connect one side of the button to pin 2 and the other side to 5V. Press it and pin 2 sees 5 volts — HIGH — which is correct. But what does pin 2 see when the button is not pressed? Nothing at all. The pin is connected to empty air, and an unconnected pin is called floating. It picks up electrical noise from the room and flickers randomly between HIGH and LOW. Your program will think the button is being tapped by a ghost.

The fix is a pull-down or pull-up resistor: a 10kΩ resistor that gently holds the pin at a known level whenever the button is open. And the Arduino has one built in, so most of the time you can skip the extra part entirely:

  • Write pinMode(2, INPUT_PULLUP); in setup.
  • Wire the button between pin 2 and GND.
  • The pin now reads HIGH normally, and LOW when pressed.

That backwards feeling — pressed means LOW — surprises everybody once, and then becomes second nature.

Bouncing, the second trap

Metal contacts do not close cleanly. In the first few milliseconds of a press they chatter, touching and separating several times. Your Arduino is fast enough to see every one of those, so a single press can register as five. This is called bounce, and it is why your dice sometimes rolls three times.

The simplest cure is to wait a moment after detecting a change:

  • Detect that the button went from HIGH to LOW.
  • Wait about 30 milliseconds for the chatter to settle.
  • Do the action once, then wait until the button is released before looking again.

Press versus hold

There is a big difference between "the button is down right now" and "the button just went down". Games and menus almost always want the second one, which means remembering what the button looked like last time round the loop and comparing. That single idea — storing the previous state — unlocks double-clicks, long presses and everything else.

Quick quiz: Buttons

3 questions to check it stuck.

Take the quiz

Next lesson

🛰️ Sensors

Keep learning

Ready to try it for real?

Reading is good. Wiring is better. Pick a project and put this to work.

Explore projects