class="site">Protocol ExplorerGuidesDesigned to ScalePricing App
Protocol ExplorerGuidesLearn ArduinoPricing App

Why Does My Arduino Button Read Random Values?

A button that reads random values is almost never broken. The pin is floating, and a single resistor, built in or external, fixes it. If one press then counts several times, the fix is a short debounce.

By Protocol Explorer · Updated

Why an unconnected input reads random values

When a button is open, a pin wired only to the button is connected to nothing. A digital input draws almost no current, so it does not settle at 0 V or 5 V; it drifts with electrical noise from your hand, nearby wires and the mains. digitalRead() then returns HIGH and LOW more or less at random. This is called a floating input, and it is not a faulty button.

The fix is to give the pin a default voltage with a resistor, so the button only has to change it.

Fix 1: use the built-in pull-up (fewest parts)

Every Uno R3 digital pin has an internal pull-up resistor of roughly 20–50 kΩ. Turn it on with INPUT_PULLUP and wire the button between the pin and GND. The pin now reads HIGH when the button is released and LOW when it is pressed, so the logic is inverted.

const int BUTTON_PIN = 2;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(115200);
}

void loop() {
  bool pressed = digitalRead(BUTTON_PIN) == LOW;  // LOW means pressed
  Serial.println(pressed ? "pressed" : "released");
  delay(100);
}

Open the Serial Monitor and set it to 115200 baud to match Serial.begin(). A mismatched baud rate shows garbage characters, which is a separate problem that is easy to confuse with a bad input.

Fix 2: an external 10 kΩ pull-down resistor

If you want pressed to read HIGH, connect one side of the button to 5 V and the other side to the input pin, then add a 10 kΩ resistor from the input pin to GND. Use plain pinMode(pin, INPUT). The resistor holds the pin at 0 V until the button connects it to 5 V. This is the wiring many starter-kit diagrams use.

Check how the four-leg button is connected

A standard tactile button has four legs connected internally in two pairs. If you wire two legs that are already joined, the input reads “pressed” all the time. Place the button across the breadboard’s centre channel and use two diagonally opposite legs; a diagonal pair is always switched by the button.

One press counts several times: contact bounce

Once the input is stable, you may see one press register two, three or more times. The metal contacts inside the button bounce for a few milliseconds as they close. A sketch that counts every change from HIGH to LOW sees each bounce as a new press.

Debounce by accepting a change only after the reading has stayed the same for a short time. Arduino’s own Debounce example uses 50 ms, and it uses millis() rather than delay(), so the rest of the sketch keeps running:

const int BUTTON_PIN = 2;
const unsigned long DEBOUNCE_MS = 50;

int stableState = HIGH;
int lastReading = HIGH;
unsigned long lastChange = 0;
int presses = 0;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(115200);
}

void loop() {
  int reading = digitalRead(BUTTON_PIN);
  if (reading != lastReading) {
    lastChange = millis();
    lastReading = reading;
  }
  if (millis() - lastChange > DEBOUNCE_MS && reading != stableState) {
    stableState = reading;
    if (stableState == LOW) {
      presses++;
      Serial.println(presses);
    }
  }
}

A quick diagnosis table

  • Reads random HIGH and LOW with nothing touched: floating input. Add INPUT_PULLUP or a pull-down resistor.
  • Always reads pressed: the button legs used are internally joined, or the pull-up wiring goes to 5 V instead of GND.
  • Never changes: the button is not in the circuit, the pin number is wrong, or the wire lands one hole over on the header.
  • One press counts several times: contact bounce. Add a debounce.
  • Serial Monitor shows strange symbols: the monitor’s baud rate does not match Serial.begin().

Practise it in the browser

Lesson 3 of Learn Arduino wires a push button to a 3D Uno R3 and prints its state to the Serial Monitor. Lesson 9 extends the same idea to eight buttons in a small piano, where clean, debounced input matters.

Official references

Related guides