INPUT_PULLUP
pinMode(2, INPUT_PULLUP) switches on a 20 to 50 kΩ resistor inside the chip that holds D2 at 5 V, so it reads HIGH until something pulls it to ground. The reading is upside down: LOW means the wire is on. One jumper wire shows it.
One word, one resistor
pinMode(2, INPUT_PULLUP);That line makes D2 an input, as INPUT does, and also connects a resistor
inside the chip from the pin to the 5 V rail. The datasheet gives it as 20 to
50 kΩ. With nothing else on the pin, no current flows through it, so there is
no voltage across it, and the pin sits at 5 V: a steady HIGH that a fingertip
cannot move.
The wire wins
Put a jumper wire from the 2 hole to a GND hole and the pin is joined straight to ground. The wire has next to no resistance and the pull-up has tens of thousands of ohms, so the wire wins: the pin is at 0 V and reads LOW. The whole 5 V now sits across the pull-up, which lets through 5 V / 50 kΩ = 0.1 mA to 5 V / 20 kΩ = 0.25 mA. That small current is why a pull-up is so large. It is a short circuit too weak to matter.
The order matters. Upload the sketch first, so D2 is already an input with its pull-up on, then plug the wire. A wire from GND to a pin set as OUTPUT and written HIGH joins the 5 V rail to ground through the pin's driver, and that is a short that can damage it.
Not D13 on a Nano
Any digital pin can do this on the UNO, D13 included. On the Nano, D13 is the
exception: its lamp and R4, 5.1 kΩ, hang from the pin to ground. With the
pull-up on, current runs in from 5 V through the pull-up and out through R4 and
the lamp, and the pin settles where the two balance. By the arithmetic, with a
20 to 50 kΩ pull-up and a red LED, that is somewhere around 2 V, and the lamp
glows faintly. 2 V is between the 1.5 V the chip promises to read as LOW and
the 3 V it promises to read as HIGH, so digitalRead(13) can return either. On
a Nano, put a button or a wire on any other pin.
The reading is upside down
With a pull-up, nothing connected reads HIGH and the switch closed reads LOW. The sketch says so in one line:
bool touching = digitalRead(PIN) == LOW;Naming the state, rather than testing HIGH and LOW throughout, keeps the
inversion in one place. Buttons wired this way, from the pin to ground with the
pull-up on, are the usual arrangement on an Arduino. It needs no resistor at all.
Where the GND holes are
Every GND hole is the same ground, and any of them will do. The UNO has three: one on the top strip, between AREF and 13, and two on the power strip, between 5V and VIN.
The code
Reads D2 with its pull-up on, lights the pin-13 lamp while the reading is LOW and prints which state it is in. Upload it first, then put one jumper wire from the 2 hole to any GND hole, and take it out again.
// Pullup_On_D2.ino — lamp 13 lights while a wire joins D2 to GND
// Tools > Board: Arduino Uno, or Arduino Nano with
// Processor ATmega328P (Old Bootloader if the upload says
// "not in sync")
// Tools > Port: the CH340 port
// Wiring: one jumper wire from the 2 hole to a GND hole.
// D2 is INPUT_PULLUP, so the wire is safe.
const byte PIN = 2;
const byte LAMP = 13;
void setup() {
pinMode(PIN, INPUT_PULLUP); // 20-50 kohm to 5 V inside the chip
pinMode(LAMP, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Inverted: LOW means the wire is touching GND.
bool touching = digitalRead(PIN) == LOW;
digitalWrite(LAMP, touching ? HIGH : LOW);
Serial.println(touching ? "wire on GND: LOW" : "no wire: HIGH");
delay(100);
}D2 is INPUT_PULLUP before you touch the wire, so the wire joins a weak pull-up to ground, a fraction of a milliamp. Never put a wire from GND, 5V or 3V3 on a pin a sketch has set as OUTPUT: that is a short.
When it does not work
With a pull-up the logic is inverted: no wire reads HIGH, a wire to GND reads LOW. The sketch handles it with digitalRead(PIN) == LOW. Forgetting the inversion is the most common pull-up bug, and it looks like a wiring fault.
Check the wire is on 2 and on a GND. On the UNO's top strip GND sits between AREF and 13, and on the Nano GND is the hole right beside D2, so it is easy to land one off. A loose jumper end can also miss the contact; a paper clip pressed into both holes works as a test.
Check setup() says INPUT_PULLUP, not INPUT, and that it is the same pin number as the digitalRead. A long wire run past a motor can also pick up enough to beat a 20 to 50 kΩ pull-up; a stronger 4.7 or 10 kΩ resistor of your own fixes that.
That is contact bounce: metal meeting metal makes and breaks several times before it settles. This sketch only prints every 100 ms, so it mostly hides it. The next article counts the bounces and ignores them.
One touch of a wire is several edges, and a millis() lockout counts it once.
Bounce, and how to ignore it →Edit this page — content/books/atmega328p/input-pullup.mdx
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.