digitalRead and the floating input
digitalRead on a pin with nothing connected does not return LOW. It returns whatever the pin has drifted to, and a fingertip on the header hole turns that into a stream of noise. The cure is to give every input something that holds it.
An input takes almost nothing
pinMode(2, INPUT) switches the pin's driver off and connects it only to the
chip's input circuit. That circuit draws at most a microamp or so, which is what
makes an input useful: it can read a weak signal without disturbing it. It is
also the problem. With nothing connected, almost nothing is enough to move the
pin, and nothing is holding it anywhere.
A pin in that state is called floating. It is the state every pin is in after a reset, until the sketch says otherwise.
What digitalRead sees
The chip calls a pin HIGH above 0.6 × 5 V = 3 V and LOW below 0.3 × 5 V = 1.5 V. Between the two it gives an answer but does not promise which. A floating pin can sit anywhere, and wander.
Put a finger on the D2 hole and the reading goes to noise. Your body picks up hum from the mains wiring around it, and the pin picks it up from you, swinging through both lines many times a second. Every 50 ms the sketch catches it at a different point, and the Plotter fills with 0s and 1s.
Take the finger away and the pin may settle on 1 or 0 for seconds, then change with no visible cause. Neither answer is about anything.
The fix is to hold the pin
An input needs something that sets its voltage when nothing else does: a resistor to 5 V (a pull-up) or to ground (a pull-down). It is weak enough that a switch or a sensor can overrule it, and strong enough that a finger cannot.
The ATmega328P has a pull-up built into every pin, and switching it on is one
word in pinMode. It has no pull-down. A switch to 5 V needs a resistor to
ground of your own.
Pins a sketch does not use can be left floating. They read nonsense, and nothing reads them, so no harm is done.
The code
Reads D2 every 50 ms, prints the result and copies it to the pin-13 lamp. Upload it, open the Serial Plotter at 9600 baud, and touch the D2 header hole with a fingertip.
// Floating_D2.ino — prints what D2 reads with nothing holding it
// 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: none. Touch the D2 header hole with a fingertip.
// D2 is an INPUT with no pull-up: the noise is the lesson.
const byte PIN = 2;
const byte LAMP = 13;
void setup() {
pinMode(PIN, INPUT); // undriven, nothing holding it
pinMode(LAMP, OUTPUT);
Serial.begin(9600);
}
void loop() {
int v = digitalRead(PIN);
digitalWrite(LAMP, v); // the lamp shows the same answer
Serial.println(v); // open the Serial Plotter to see it
delay(50);
}Nothing is wired, and D2 is an INPUT with no pull-up on purpose. The noise is the lesson. The next article adds one word to setup() and it stops.
When it does not work
A floating pin can hold a charge for a long time, and how much your body picks up depends on the room, your shoes and the cable. Hold a fingertip on the hole for a few seconds, then wave your hand just above the board. Any change with no wire attached is the point.
The same floating pin. A button that only joins the pin to ground leaves it floating while it is not pressed. Use pinMode(pin, INPUT_PULLUP), or a resistor of your own to 5 V or to ground.
That is not this sketch: it drives D13 fully HIGH or LOW. On the UNO, L can glow part-way when D13 itself is a floating input, because the LM358 follower copies whatever voltage it finds. Give D13 a pinMode and it stops. On the Nano the lamp hangs on the pin, and a floating pin cannot feed it enough to light it.
One word in pinMode, a resistor inside the chip, and a reading that means something.
INPUT_PULLUP →Edit this page — content/books/atmega328p/the-floating-input.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.