Sub topic 4
Why an unconnected input reads random values
This chapter assumes you can write a basic sketch with setup() and loop(). It also assumes you know Ohm's law, can wire a breadboard, and have used pinMode, digitalRead, and Serial. You should also know that HIGH and LOW are the two levels a digital pin reports.
By the end you will know what a floating input is, why it happens, how to recognize it, and why a button wired the obvious way produces one.
What "floating" means
A pin set with pinMode(pin, INPUT) is not connected to anything inside the chip that sets its voltage. It only measures the voltage that something outside the chip applies to it. If nothing external drives it to a definite level, the pin is floating. Its voltage is whatever the surroundings push it to, and digitalRead reports a result that has no meaning.
Floating is not a fault in the chip. It is a wiring state that you created by leaving a conductor connected to nothing.
Why the pin picks up noise
An input pin on an ATmega328P (the chip on an Uno) is the gate of a CMOS transistor. A gate draws almost no current. The datasheet gives a worst-case input leakage of about 1 µA, and in practice it is far lower. Its input resistance is therefore enormous, in the range of hundreds of megaohms to gigaohms. The pin and the wire attached to it also have a small capacitance to everything nearby, on the order of picofarads.
Together this makes a very weak node. Three effects act on it:
- Capacitive coupling. Mains wiring, a phone charger, a fluorescent light, or your own body radiates 50 or 60 Hz fields. A loose wire acts as a small antenna. Because almost no current is needed to move the voltage, a tiny induced current is enough.
- Stored charge. The pin's capacitance can hold a charge. After you touch the wire, or after the pin was last driven, it may stay at one level for seconds and then drift. This is why a floating pin sometimes looks stable and then changes.
- Crosstalk. A neighboring wire that switches, such as a PWM output or a serial line, couples into the floating wire.
None of these sources has a defined level, so the result is unpredictable.
The undefined zone between the thresholds
The chip does not decide HIGH or LOW at exactly half of the supply voltage. At a 5 V supply, the ATmega328P datasheet specifies:
| Level | Condition | Voltage at Vcc = 5 V |
|---|---|---|
| Guaranteed LOW | below 0.3 × Vcc | below about 1.5 V |
| Undefined | between the two limits | about 1.5 V to 3.0 V |
| Guaranteed HIGH | above 0.6 × Vcc | above about 3.0 V |
A floating pin can sit anywhere in the middle band. In that band the input buffer may report either value, and it may flip back and forth with tiny changes. It also wastes a little power, because both transistors in the buffer conduct partly at the same time. The exact numbers differ on other boards (a 3.3 V board has different absolute limits), but the three zones exist on all of them.
Seeing it for yourself
Upload this sketch to an Uno or a compatible board. Connect a single jumper wire to pin 2 and leave its other end in the air.
const int probePin = 2;
void setup() {
Serial.begin(9600);
pinMode(probePin, INPUT);
}
void loop() {
int highCount = 0;
for (int i = 0; i < 100; i++) {
if (digitalRead(probePin) == HIGH) {
highCount++;
}
delay(1);
}
Serial.print("HIGH in last 100 samples: ");
Serial.println(highCount);
}Open the Serial Monitor at 9600 baud. Each line reports how many of 100 readings, taken 1 ms apart, were HIGH. A stable input would print 0 or 100 every time. A floating one often prints values in between, and the numbers change when you do these things:
- Move your hand close to the wire without touching it.
- Touch the bare end of the wire.
- Move the wire near a power cable or a charger.
- Touch the wire to the 5 V pin, then to GND, and then pull it away.
Step 4 is informative. After you remove the wire, the reading may hold the last level for a while before it drifts. That is the stored charge from the list above. Results depend on the board, the wire length, and the room, so expect variation. The point of the experiment is that the readings are not repeatable.
How a button creates a floating input
The common beginner circuit is a push button between pin 2 and 5 V, with nothing else connected. Consider what the pin sees in each state:
- Button pressed. The contacts connect pin 2 to 5 V. The pin reads
HIGH, and the level is firmly driven. - Button released. The contacts are open, so pin 2 connects to nothing. The pin floats.
A button is only a switch. When it opens, it breaks the connection but does not supply a LOW. Half of the button's states are therefore undefined, and the sketch sees random presses.
Here is the sketch that goes with that wiring. It has the bug built into the wiring, not into the code.
const int buttonPin = 2;
const int ledPin = 13;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
if (digitalRead(buttonPin) == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}The code is correct. With the button wired only to 5 V, the LED flickers or lights when you wave a hand near the board. Reading the code will not show the bug. You have to look at the circuit.
Symptoms checklist
Suspect a floating input when you see any of these:
- The LED or other output changes when you touch or approach the breadboard, the wire, or the board.
- The reading is stable until you move a nearby wire, plug in a cable, or switch something on.
- The "released" state is wrong in an irregular way, sometimes reading as pressed with nobody touching it.
- The behavior changes between two identical boards, between USB power and a battery, or between a desk and a different room.
- The Serial Monitor shows bursts of changes at roughly mains frequency. Sampling an open pin quickly can show a pattern near 50 or 60 transitions per second.
A floating input produces a repeatable pattern only in the sense that it responds to its environment. If your bug moves when your hand moves, check for floating first.
Common mistakes that cause floating inputs
These are the errors you are most likely to meet while building a button circuit:
- One-sided wiring. The button connects to 5 V or to GND, but not to both a source and a reference. Every button needs a defined level in both states.
- Wrong legs on a four-legged button. A typical tactile switch has four legs arranged as two connected pairs. If you place it so two legs on the same side connect to your wires, the button is permanently shorted or never connects. Rotate it by 90 degrees and test with a multimeter in continuity mode.
- Loose jumper or half-seated pin. The breadboard contact looks fine but does not connect. The pin then floats exactly as if no wire existed.
- Wrong pin number in code. You wire pin 3 and read pin 2. Pin 2 is unconnected, so it floats.
- A pin you forgot about. You declared a pin as an input in code, then removed the wiring from the circuit.
- Reading with an
analogReadon an open analog pin. The same effect applies. An open analog input shows values that wander across the range.
What a fix must achieve
Any correct fix gives the pin a default level that a weak connection supplies when the button is open. The button then overrides that default when pressed. The weak connection is a resistor, typically 10 kΩ. The resistor passes only a small current to the pin, because the pin's input resistance is huge and takes almost nothing. When the button closes, it connects the pin to the opposite level, and the resistor limits the current that flows from one level to the other.
There are two directions for the default:
- A pull-down resistor connects the pin to GND. The default is
LOW, and the button brings the pin to 5 V. - A pull-up resistor connects the pin to 5 V. The default is
HIGH, and the button brings the pin to GND.
The Arduino also has a pull-up resistor built into each pin, which you enable with INPUT_PULLUP. It removes the need for an external part. The following chapters cover each option in turn.
Unused pins
Unused pins can float too. On a normal Uno this is harmless to the board, though a floating pin wastes a little current. In battery-powered designs, it adds up. A common habit is to give every unused pin a defined state. You can either set it as an output, or set it as INPUT_PULLUP so that it has a known level and draws very little. For example:
void setup() {
for (int pin = 2; pin <= 12; pin++) {
pinMode(pin, INPUT_PULLUP);
}
}
void loop() {
}This loop skips pin 13 because it drives the built-in LED on an Uno, and it skips pins 0 and 1 because they carry the USB serial connection. Do not run it on pins that you use for other purposes.
Exercise
A student wires a push button between pin 2 and 5 V, sets pin 2 with pinMode(2, INPUT), and prints digitalRead(2) to the Serial Monitor. Pressing the button prints 1 as expected. With the button released, the monitor prints a mix of 0 and 1, and the mix changes when the student moves a hand near the wire. Explain the cause, and say what is missing from the circuit.
Show answer
When the button is released, its contacts are open, so pin 2 is connected to nothing and floats. The pin's input resistance is extremely high, so noise from nearby fields and stored charge sets its voltage, and it can land in the undefined zone between about 1.5 V and 3.0 V or cross either threshold. The circuit lacks a resistor that gives the pin a default level when the button is open, either a pull-down resistor from the pin to GND, or a pull-up arrangement that uses the other side of the button. Without it, only the pressed state is defined.