Sub topic 9
Floating inputs and INPUT_PULLUP
This chapter assumes you know what voltage, current, and resistance are, and that you can write a basic sketch with setup() and loop(). It also assumes you have read the chapter on digital pins and pinMode. Here you will learn why an unconnected input gives unreliable readings, how pull-up resistors fix that, and how to use INPUT_PULLUP on the Uno.
What a floating input is
A digital input pin on the ATmega328P (the Uno's microcontroller) has a very high input impedance. That means it draws almost no current. It only senses the voltage on it and decides whether that voltage counts as HIGH or LOW.
If nothing drives the pin to a defined voltage, the pin is called floating. It is not at 0 V and not at 5 V. It behaves like a tiny antenna and picks up:
- electric fields from your hand or nearby wires
- noise from the mains supply
- coupling from neighbouring pins that change state
The result is that digitalRead() returns unpredictable values. You may see it flip between HIGH and LOW with no input at all, or change when you move your hand near the board.
A simple push button wired from a pin to 5 V shows the problem. When the button is pressed, the pin is connected to 5 V and reads HIGH. When the button is released, the pin is connected to nothing. It floats, and the reading is random.
Pull-up and pull-down resistors
The fix is a resistor that gently pulls the pin to a known voltage when nothing else is driving it.
- A pull-up resistor connects the pin to 5 V. The idle state is HIGH.
- A pull-down resistor connects the pin to GND. The idle state is LOW.
The resistor must be large enough that it does not matter when the button closes. A typical value is 10 kΩ. When the button connects the pin to the opposite rail, the pin voltage is set by the button, and the resistor only carries a small current.
Take a pull-up with a 10 kΩ resistor and a button to GND:
- Button released: no current flows through the pin (its impedance is very high), so there is almost no voltage drop across the resistor. The pin sits at about 5 V and reads HIGH.
- Button pressed: the pin connects directly to GND and reads LOW. The current through the resistor is 5 V ÷ 10 000 Ω = 0.5 mA, which is harmless.
The resistor value is a trade-off. A smaller resistor gives a stronger pull and better noise resistance, but wastes more current when the button is pressed. A larger one saves current but is easier to disturb with noise.
The internal pull-up
The ATmega328P has a built-in pull-up resistor on every I/O pin, which you can switch on in software. Its value is roughly 20 kΩ to 50 kΩ, according to the chip's datasheet. You enable it with INPUT_PULLUP:
pinMode(2, INPUT_PULLUP);This removes the need for an external resistor. You then wire the button between the pin and GND. The wiring is:
- One leg of the button goes to digital pin 2.
- The other leg goes to GND.
Nothing goes to 5 V. The internal resistor supplies that connection inside the chip.
Inverted logic
Because the pull-up holds the pin HIGH when idle, the logic is inverted:
| Button state | Pin voltage | `digitalRead()` |
|---|---|---|
| Released | about 5 V | `HIGH` |
| Pressed | 0 V | `LOW` |
A pressed button reads LOW. This surprises many beginners, but it is the standard convention for buttons on Arduino boards. The usual way to keep your code readable is to give the test a name:
const int buttonPin = 2;
const int ledPin = 13;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop() {
bool pressed = (digitalRead(buttonPin) == LOW);
digitalWrite(ledPin, pressed ? HIGH : LOW);
}The variable pressed is true when the pin is LOW. The LED on pin 13 lights while the button is held, and no external resistor is used.
How the pull-up works internally
On the ATmega328P, each pin belongs to a port, and each port has three registers: DDRx (data direction), PORTx (output value), and PINx (input value). For an input pin, writing a 1 to the matching PORTx bit connects the internal pull-up. This is why older sketches write digitalWrite(pin, HIGH) right after pinMode(pin, INPUT). That trick still works on the Uno, but INPUT_PULLUP states the intention more clearly, so prefer it.
Common errors
Reading a button with no pull at all
pinMode(2, INPUT); // no pull-up, no external resistorIf the button goes only to GND or only to 5 V, the pin floats in one of the two states. The readings are random. Either use INPUT_PULLUP or add an external resistor.
Using INPUT_PULLUP and also wiring to 5 V
If you enable the internal pull-up and also connect the button between the pin and 5 V, pressing the button connects the pin to 5 V, which it already approximates. The pin never reaches LOW, and the button seems to do nothing. With INPUT_PULLUP, the button goes to GND.
Switching the mode too late
Pull-ups are enabled in setup(). If you read the pin before calling pinMode, the first readings can be wrong. Set the mode before any read.
Forgetting that outputs behave differently
The pull-up only applies when the pin is an input. If you later change the pin to OUTPUT, it drives HIGH or LOW directly, and the pull-up no longer matters. If you switch a pin from INPUT_PULLUP to OUTPUT, be aware that the output starts with whatever value is in the PORTx bit, which is HIGH after INPUT_PULLUP. Write the level you want before changing the mode if the exact start state is important.
Expecting a strong pull
At 20 kΩ to 50 kΩ, the internal pull-up is weak. It works for buttons and switches on short wires. It may not be enough for:
- long cables, which pick up more noise
- sensors with open-collector outputs that need a specific resistor value
- I2C buses, which commonly want 4.7 kΩ pull-ups for clean signal edges
In those cases, add an external resistor.
Button bounce
Real buttons do not change state cleanly. The metal contacts bounce for a few milliseconds, so one press produces several LOW-HIGH transitions. A digitalRead() in a fast loop sees all of them. If your code counts presses, the count will be too high.
A simple fix is a short wait after detecting a change:
const int buttonPin = 2;
int count = 0;
int lastState = HIGH;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
int state = digitalRead(buttonPin);
if (state != lastState) {
delay(20); // wait for the contacts to settle
state = digitalRead(buttonPin);
if (state == LOW && lastState == HIGH) {
count++;
Serial.println(count);
}
lastState = state;
}
}The sketch counts only the transition from HIGH to LOW, so holding the button down counts once. The delay(20) blocks the sketch for 20 ms, which is fine for a simple counter. Larger programs usually use millis() timing instead.
Choosing between internal and external pull-ups
| Situation | Better choice |
|---|---|
| Simple button or switch, short wires | `INPUT_PULLUP` |
| Saving parts and board space | `INPUT_PULLUP` |
| Noisy environment or long cable | External resistor, 1 kΩ to 10 kΩ |
| I2C bus | External 4.7 kΩ pull-ups (some boards include them) |
| Need a LOW idle state | External pull-down, because the Uno has no internal pull-down |
Analog pins as digital inputs
Pins A0 to A5 can also be used as digital pins, and INPUT_PULLUP works on them too. You can refer to them as A0, or by their digital numbers 14 to 19. This is handy when you run out of digital pins for buttons.
Exercise
A button is wired between digital pin 4 and GND. The sketch calls pinMode(4, INPUT) and reads the pin in loop(). The readings jump between HIGH and LOW even when nobody touches the button. What is wrong, and what is the smallest change that fixes it? What will digitalRead(4) return while the button is held down?
Show answer
The pin floats whenever the button is released, because nothing holds it at a defined voltage. Change the mode to pinMode(4, INPUT_PULLUP); so the internal pull-up holds the pin HIGH when the button is released. While the button is held down, the pin is connected to GND, so digitalRead(4) returns LOW.
Exercise
You enable INPUT_PULLUP on pin 7 and connect a 10 kΩ resistor from pin 7 to GND, intending it as a pull-down. Why is this a poor design, and roughly what voltage appears on the pin when no button is pressed?
Show answer
The internal pull-up (about 20 kΩ to 50 kΩ) and the 10 kΩ resistor form a voltage divider. The pin sits at roughly 5 V × 10 / (10 + 20 to 50), which is about 0.8 V to 1.7 V. That is close to or inside the undefined zone between LOW and HIGH, so readings are unreliable. Use either the internal pull-up with a button to GND, or an external pull-down with plain INPUT, not both.