Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

This question already has an answer here:

I was trying out a basic button tutorial on my new Arduino UNO. But for some reason, the Arduino was detecting the button as on (pressed) even when it was off (unpressed).

After some experimentation, I found that the digital pins were flickering between HIGH and LOW, even if there was just a loose wire plugged in with nothing at the other end. And the state of the button, etc. wasn't making any difference. I tried this with pins 5,6 and 7, and it's happening with all of them, even when the neighbouring pins are connected to the ground.

Is this behaviour normal or is there something wrong with my input pins?

Since I'm new to Arduino, there might also be something wrong with my testing code. Here's what I used...

const unsigned int LED_PIN = 12;
const unsigned int BUTTON_PIN = 7;
const unsigned int INDICATOR_PIN = 13;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT);
  digitalWrite(LED_PIN, LOW);
}

void loop() {
  const int BUTTON_STATE = digitalRead(BUTTON_PIN);

  if (BUTTON_STATE == HIGH) {
    digitalWrite(LED_PIN, HIGH);
    digitalWrite(INDICATOR_PIN, LOW);
  }
  else if (BUTTON_STATE == LOW) {
    digitalWrite(LED_PIN, LOW);
    digitalWrite(INDICATOR_PIN, HIGH);
  }
}

This code makes the lights always in opposite states: when one is on, the other is off. When I ran the code, both lights seemed on, which meant they were probably flickering, which meant that there was some interference in the input pin.

share|improve this question

marked as duplicate by sachleen Apr 24 at 1:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

up vote 4 down vote accepted

It sounds like you're seeing entirely normal behaviour. If you leave a digital input unconnected (or connected to an open button) then it's 'floating'. That means it can pick up interference from all sorts of things, and may appear to drift between high and low.

The usual way to resolve this problem is using a pull-up or pull-down resistor. This is usually a high-ohm resistor (e.g. 10,000 Ohm or more) which will gently pull the pin HIGH or LOW in the absence of any other signal. When another signal comes in (e.g. from a button being pushed), it should have a much stronger effect than the resistor, so that is what the input will see.

Your Uno has built-in pull-up resistors which you can use for this, although it will require a couple of changes. In your setup() function, you can enable the pull-up on your input pin like this:

pinMode(BUTTON_PIN, INPUT_PULLUP);

That will cause the input to appear HIGH when the button is not pushed. That means you'll need to wire the other end of your button to ground (negative) instead of the voltage supply (positive). If you've set it up correctly, the input should appear LOW when the button is pushed.

share|improve this answer
    
Thanks for the tip about the resistor! I also realized that my breadboard was separated in the middle, so my "ground" line wasn't actually connected to the Arduino's GND pin. –  Hippo Apr 27 at 5:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.