Go Down

Topic: Push-button Debouncing question (Read 106 times) previous topic - next topic


Hi all
I have a simple circuit: a push-button with a pull-down 10 kohm resistor connected to an LED.
I'm using an Arduino UNO. The loop has a freq of 100Hz.
Every time I push the button I would like to change the state of the LED.
Not every single time I can change its state.
I know that I have to do some kind of debouncing (software or hardware).
I've tried to look at the signal with the scope but it isn't clear to me why I have to do the debouncing.
I have connected the yellow probe through a yellow cable to the pull down resistor
I have read that signals >3V are considered HIGH while signals <1.5V are considered LOW by Arduino.
In the signal on the scope I don't see what causes the problem to che change of the state of the LED.

Code: [Select]

// Turn on the LED while the button is pressed and
// keep it on while it's released
// Turn off the LED after the button is pressed again.

const int LED=2;  // LED connected to digital pin 2
const int BUTTON=3;  // button connected to digital pin 3
int buttonState=0;  // buttonState stores the value of the button state
int LEDstate;  //LEDstate stores the value of the LED state

const int LOOP=4; // Needed to check the loop speed
int loopIndicator=0;  // Needed to check the loop speed

void setup() {
  pinMode(LED,OUTPUT);  //Set the digital pin as output
  pinMode(BUTTON,INPUT);  //Set the digital pin as input
  pinMode(LOOP,OUTPUT);  // Needed to check the loop speed

void loop() {
  buttonState=digitalRead(BUTTON);  // read input value and store it

    LEDstate = !LEDstate;
  // Check whether the LED state is HIGH
    digitalWrite(LED,HIGH); // Turns the LED on
  digitalWrite(LED,LOW);  // Turns the LED off
  digitalWrite(LOOP,loopIndicator);  // Needed to check the loop speed
  loopIndicator=!loopIndicator;  // Needed to check the loop speed



Every time I push the button I would like to change the state of the LED.
Then look at the example code in the Arduino's IDE called "state change" and that will show you how to do it correctly. It uses when a input pin's state changes not what you have appeared to do which is use the current state of the input pin.


Feb 29, 2020, 07:08 pm Last Edit: Feb 29, 2020, 07:10 pm by PerryBebbington
What GM said.

Code: [Select]
    LEDstate = !LEDstate;

Every time you go round loop, which is every 10ms, you change the state of buttonState, so if you keep the button pressed for more than 10ms you will keep changing buttonState until you let go of the button. 10ms is no time at all for a button press, time some button presses with your 'scope and you will see. I have managed to get 20ms of bounce with a (pretty crap) button, let alone being pressed for more than 10ms.

BTW, and I know you are addressing this elsewhere, you have awful ringing on that button. I don't know why it's so bad. Have you any capacitors you can put across the power supply on the breadboard?

Go Up