Arduino: Zero to First Project · EPISODE 3 · 2:45

A Button (digitalRead + INPUT_PULLUP)

Make an LED obey your finger! How a push button works inside, how to wire it to an Arduino (just 2 wires), why a "floating" pin reads random junk, and how INPUT_PULLUP fixes it — plus your first if / else, in under 3 minutes.

// CHAPTERS · TAP TO JUMP
// IN THIS EPISODE

What you'll learn

  • A tactile push button has 4 legs, joined inside in 2 pairs. Pressing it makes a metal dome bridge the two sides.
  • Wiring: one side of the button to pin 2, the other side to GND. That's it — no resistor needed (see below).
  • digitalRead(2) gives one of two answers: HIGH or LOW.
  • With the button not pressed, the pin is connected to nothing — it "floats" and reads random HIGH / LOW, like a tiny antenna. Your light flickers for no reason.
  • Fix: a pull-up resistor from the pin to 5 V holds it HIGH; pressing the button pulls it to ground → LOW. It's tens of thousands of ohms (≈ 20–50 kΩ inside the chip), so only a tiny current flows (5 V ÷ 50,000 Ω ≈ 0.1 mA).
  • The Arduino has it built in: pinMode(2, INPUT_PULLUP); → not pressed = HIGH, pressed = LOW.
  • The code:
    if (digitalRead(2) == LOW) { digitalWrite(LED_BUILTIN, HIGH); } else { digitalWrite(LED_BUILTIN, LOW); }
  • == compares, = sets. loop() checks the button thousands of times a second, so the light reacts instantly.
// 30-SECOND SHORTS ON THIS

Quick refreshers