0

I have 5V -> button -> 10K resistor -> PIN2 setup with Arduino UNO.

My code is very simple:

int count = 0;

void setup() {
  pinMode(2, INPUT);
  Serial.begin(9600); 
}

void loop() {
  int state = digitalRead(2);
  Serial.println(state);
  if (state == HIGH) {
    count++;
    Serial.print("Hi");
    Serial.println(count);
  }
  delay(1000); 
}

If the button is pressed print "HiX", but what I get is very strange. When I press the button only once I get this:

0
0
0
0
0
1
Hi1
1
Hi2
1
Hi3
1
Hi4
1
Hi5
1
Hi6
1
Hi7
1
Hi8
1
Hi9
1
Hi10
0

The button press is registered 10 times.

What am I doing wrong ?

migrated from electronics.stackexchange.com Mar 21 '17 at 19:19

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • Nothing, button contacts "BOUNCE" on off for a while when activated. YOU need to either add a de-bounce circuit, or configure your code to ignore then signal for 50mS or whatever. Though I am assuming that code is even right... – Trevor Mar 18 '17 at 19:01
  • You pressed it once, but you must have held it for 10 seconds. You got a "Hi" for every iteration through the loop. If you only want one message per press, then you need to remember the previous state of the switch in a variable, and then compare that to the current state. – Dave Tweed Mar 18 '17 at 19:09
  • Use one of the "bounce" libraries for Arduino or write something similar, see: github.com/thomasfredericks/Bounce2/wiki – Dejvid_no1 Mar 18 '17 at 19:10
  • it is is really "bouncing" issue, why does it last 10 seconds ? edit: To be clear, I press the button for less than a second. – gotha Mar 18 '17 at 19:10
  • 1
    As Yasu said in his answer, your input is floating when the button is not pressed – Dejvid_no1 Mar 18 '17 at 19:52
6

I can't comment that's why I'm writing this as an answer but,

Are you doing exactly this this to wire the button?

https://www.arduino.cc/en/uploads/Tutorial/button.png I'd like to see your schematic but from what you describe there's a pull-down connection missing

  • 1
    seems like you are right, my schematic was not correct, but I still needed some debouncing code to make it work smoother. thanks – gotha Mar 18 '17 at 20:16
1

there could be some stray capacitance that's holding the charge.

generally for cases like that, put the buttons active low + pull-up to avoid cases like yours.

0

Try out this code and schematic. It may solve your problem of debouncing.

schematic

const int buttonPin = 2; // Push Button at Pin 2

int buttonState = 0;
int lastButtonState = 0;

int counter = 0;

void setup()
{
  pinMode(buttonPin, INPUT_PULLUP); // Push Button with pull-up
  Serial.begin(9600);
}

void loop()
{
  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState)
  {
    if (buttonState == HIGH)
    {
      counter++;
      Serial.println("Hi !");
      Serial.print("Number of button pushes:  ");
      Serial.println(counter);
    }
    else 
    {
      Serial.println("Button off");
    }
    delay(50); // Delay a little bit to avoid bouncing
  }
  lastButtonState = buttonState;
}
0

I think it's not a problem of debouncing... There is a delay(1000) in the loop so after 1 second, the switch is not bouncing anymore. But switching to VCC is the problem. Connect the switch to GND and use the internal pullup resistors (pinMode(2, PULLUP). The switch then pulls it down to ground. Now it is floating and probably still HIGH after opening the switch....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy