Sub topic 4
Why an Unconnected Input Reads Random Values
A digital input pin does not create a signal by itself. It measures the voltage present on the pin and classifies that voltage as either HIGH or LOW. If nothing connects the pin to a known voltage, the pin is floating.
This chapter assumes that you know the basic Arduino sketch structure, voltage and resistance, breadboard wiring, and how digitalWrite controls an LED.
A floating input has no defined voltage
Set a pin to INPUT when the Arduino should measure an external signal:
pinMode(buttonPin, INPUT);In this mode, the pin has very high electrical resistance. It draws only a tiny current from the circuit being measured. That is useful because the Arduino should not strongly affect the signal.
The same property causes a problem when the pin is unconnected. There is no strong path to either 5 V or GND. The pin's voltage can be changed by small unwanted effects:
- Electrical noise from nearby wires, motors, and digital signals
- Capacitively coupled signals from your hand or nearby objects
- Leakage current inside the microcontroller
- Charge left on the pin from an earlier signal
- Long jumper wires acting like small antennas
The voltage may move above and below the input threshold. Each call to digitalRead can then return a different value.
| Pin condition | Voltage source | Typical reading |
|---|---|---|
| Connected to GND | Strong, defined path | `LOW` |
| Connected to 5 V | Strong, defined path | `HIGH` |
| Unconnected | No defined path | Unpredictable |
The readings are not truly random in the mathematical sense. They depend on the surrounding electrical conditions, the previous voltage on the pin, and the exact timing of each measurement. However, random-looking behavior is the expected symptom.
What the Arduino sees
The Arduino compares the input voltage with logic thresholds. A voltage near GND is interpreted as LOW, and a voltage near the supply voltage is interpreted as HIGH. The region between those levels is not a reliable logic state.
A floating pin can spend time in that uncertain region. Tiny changes can push it across a threshold:
This sketch lets you observe the effect:
const int inputPin = 2;
void setup() {
Serial.begin(9600);
pinMode(inputPin, INPUT);
}
void loop() {
Serial.println(digitalRead(inputPin));
delay(100);
}With no wire connected to pin 2, the Serial Monitor may show changing zeroes and ones. The exact pattern can change when you touch the board, move a jumper wire, or switch another circuit.
A floating input can also make an output appear broken. For example, code that lights an LED whenever the input is HIGH may cause the LED to flicker even though nobody presses a button. The software is correctly reporting an unstable electrical signal.
How to recognize the problem
Check for these signs:
- The input changes when you touch the wire or nearby circuit.
- The result changes when the USB cable or power supply moves.
- The LED flickers without a deliberate button action.
- A multimeter shows a changing or intermediate voltage.
- The circuit works differently on different days or with different wire layouts.
The fix is to provide a weak but definite connection to a known voltage. A pull resistor is a resistor that establishes the default voltage while still allowing a button or other signal to override it. A pull-down connects the input weakly to GND. A pull-up connects it weakly to the positive supply. The next chapters compare those arrangements and show how to use the built-in pull-up.
Exercise
Why can an INPUT pin return changing values when no wire is connected to it?
Show answer
The pin has no defined path to 5 V or GND, so noise, leakage current, stored charge, and nearby electric fields can move its voltage across the logic threshold.