ATmega328P01+5 V10 kΩbuttonbutton10 kΩ1pinMode(2, INPUT);2pinMode(2, INPUT_PULLUP);3int v = digitalRead(2);4digitalWrite(13, v == LOW);
Back: the chip (built-in pull-up, read lamp), the 5 V to GND column and a voltmeterFront: the four lines of Arduino codeMoving dots: current flowing from 5 V to GNDPin 2 of an Arduino Uno R3 with a button to ground. Joined to nothing, the pin floats and reads at random. A 10 kΩ pull-up to 5 V holds it HIGH; a press pulls it LOW while 0.5 mA flows through the resistor and button. INPUT_PULLUP uses the chip's own 20-50 kΩ pull-up; a pull-down is the mirror image, where a press reads HIGH.

A floating pin

  1. A button joins pin 2 to ground (GND, 0 volts). With the button open, the pin is connected to nothing at all. We call it floating, and digitalRead returns 0 or 1 at random.

The pull-up resistor

Built in, and flipped

What happens

;; step 1/9 · floating
board     Arduino Uno R3 (ATmega328P, 5 V)
wiring    pin 2 -> button -> GND
line 1    pinMode(2, INPUT);
open      pin joined to nothing: floating
read      0, 1, 1, 0, ... at random

The figure shows the voltage the pin itself sees. A real multimeter has an input resistance of millions of ohms, so touching a floating pin with it pulls the pin a little and changes the reading. 1 µA is the datasheet's worst case for input leakage (at 5.5 V). A real pin usually leaks far less, so with 10 kΩ the open pin sits within 0.01 V of 5 V.