Button reads random HIGH and LOW: floating input and INPUT_PULLUP

A pin with nothing defining its level “floats” and reads random values. Give it a pull-up or pull-down resistor.

What the message means

An input pin is very sensitive. When the switch is open the pin is connected to nothing, so noise decides what it reads.

Fix it in this order

  1. Use pinMode(pin, INPUT_PULLUP) on boards that have internal pull-ups, and wire the button between the pin and ground.
  2. With INPUT_PULLUP the pin reads HIGH when released and LOW when pressed. Reverse your logic to match.
  3. Or add an external resistor of about 10 kohm to 3.3 V or 5 V (pull-up) or to ground (pull-down).
  4. On ESP32 GPIO 34, 35, 36 and 39 there are no internal pulls. Use an external resistor.
  5. Add a debounce delay of about 20 ms, or a small capacitor, so one press is not read as many.

Why it happens

An input pin with nothing connected to it has no defined voltage. The input circuit has very high impedance, so stray charge and electrical noise move its voltage around. A pull-up resistor holds the line at a known HIGH level while the switch is open, and the switch pulls it LOW when pressed. That gives the code a stable, predictable signal to read.

Mechanical contacts also bounce, opening and closing several times within a few milliseconds, so one press can register as many. Debouncing with a short delay, or by checking that the state stays the same for a set time, removes the extra edges. Some ESP32 pins have no internal pull resistors, so an external resistor is needed on those. Check the datasheet for the pin before choosing.

If you see this, do this

You seeDo this
Reads randomly with nothing pressedFloating pin. Add a pull resistor.
One press counts many timesDebounce.

Confirm the fix

  1. Set pinMode to INPUT_PULLUP and print digitalRead with the button released; the value should read HIGH every time.
  2. Press the button and confirm the printed value changes to LOW, not a random mix of both values.
  3. Watch the serial output during one press and count the transitions; a debounced switch prints a single change per press.

Common mistake: Assuming an unconnected input reads LOW by default. A floating pin has no defined level, so it returns random HIGH and LOW values that change with noise and nearby wiring.

input-only pins, wiring checker.

Last reviewed: November 2026. These are the usual causes and the order that finds them fastest. Test on your own board; clones and cables differ.