Whenever a push-button connected with Arduino is pressed, it may not immediately make a complete connection. Consequently, it may make contact on one side – then both – and then the other side – until it finally settles down. This making and breaking contact is called bouncing. certainly, it produces unwanted results during the execution of the program It is not a manufacturing defect of the button but is implicit in most physical switches. Bouncing happens in a matter of milliseconds and your microcontroller which is working at high speed read these transition as multiple inputs. To prevent Bouncing we use the button debounce, which means checking the inputs twice in a very short period as soon as the change is detected. We will also use the millis function to keep track of time.


Why we need Push Button to debounce?

When you press a push-button it produces vibrations on contact as shown in the figure below. These vibrations are recorded by the controller (like Arduino) as multiple ON/OFF signals due to its very high speed. As a result, the desired output will get altered every time you press the button until you fix the problem using button debounce.

Diagram Arduino debounce push button
Figure 1: Arduino debounce push button | Vibrations caused when button is pressed



While making the connections make sure to use a pull-down resistor (Read FAQ’s at last for more info) because directly connecting two points of a switch to the circuit will leave the input pin in floating condition and the circuit may not work as desired. Alternatively, you can also use built-in pull-up resistors of 20K by setting pinMode as INPUT_PULLUP.

Arduino debounce button
Arduino debounce button

Figure 2: Push Button connections with arduino


Arduino Debounce Button

/*UNCIA ROBOTICS | www.unciarobotics.com
  PROGRAM: Arduino button debounce
  Read the signal generated by the push button
  and toggle LED on each press

  13    LED
  2     Switch

const int buttonPin = 2;     // pushbutton pin
const int ledPin = 13;       // LED pin

int ledState = HIGH;         // current state of LED
int buttonState;             // current state of button
int lastButtonState = LOW;   // previous state of button

unsigned long lastDebounceTime = 0; // last toggle
unsigned long debounceDelay = 50;   // debounce time

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, ledState);

void loop() {
  int reading = digitalRead(buttonPin); //read

  if (reading != lastButtonState) // If the switch changed
    lastDebounceTime = millis();  // reset the debouncing
  //check if debounce time > 50ms
  if ((millis() - lastDebounceTime) > debounceDelay)
  { if (reading != buttonState)
    { buttonState = reading;
      if (buttonState == HIGH)
        ledState = !ledState;

  digitalWrite(ledPin, ledState);
  lastButtonState = reading;       // save the reading



const is an abbreviation for the word constant. It’s a Variable qualifier which makes any variable read-only. You can use it like any other variable but you cannot change its value in the program. It’s a good practice to name your pins using a constant qualifier.


Description: Returns the number of milliseconds passed since the execution of the program. The number will overflow and reset after 50 Days.

Syntax: time = millis( );

Returns: milliseconds passed since the program started | Allowed data type: unsigned long

Notes and Warnings:

  • Always use unsigned long as data type while using mills() otherwise it may produce errors.


Description: Configure specific pin to behave either as input or output.

Syntax: pinMode (pin,MODE);

  • pin: the Arduino pin number to set the mode of.

Returns: Nothing

Notes and Warnings:

  • It is only applicable to digital pins.


Description: Write a HIGH or LOW value to a digital pin.

Syntax: digitalWrite (pin,VALUE);

  • pin: the Arduino pin number.
  • value: HIGH or LOW

Returns: Nothing

Notes and Warnings:

  • It is very important to set pinMode() as OUTPUT first before using digitalWrite() function on that pin.
  • If you do not set the pinMode() to OUTPUT, and connect an LED to a pin, when calling digitalWrite(HIGH), the LED may appear dim.
  • You can use analog Input pins (A0, A1, … etc) as digital pins. With the exception of Arduino Nano, Pro Mini, and Mini’s A6 and A7 pins.


Description: Reads digital values from digital pins.

Syntax: digitalRead (pin);

  • pin: the Arduino pin number.

Returns: HIGH or LOW

Notes and Warnings:

  • digitalRead function returns random values (HIGH/LOW) if the input pin is floating (not connected).
  • Analog pins (A0, A1, …etc) can be used as digital pins on Arduino boards except Arduino Nano and Mini pro.
  • Avoid using pin 13 as an input pin. because of the LED and resistor which are already attached to it.


Why we prefer millis() function instead of delay()?

In general, you must have seen that wherever we want the program to wait we put a delay() in between. But it might not be a good practice because it stops execution of the program for n milliseconds. Everything freezes, no input will be received no output will be sent.

Consequently, if in the meantime something happens and the sensors attached to the system detect interesting variations. Nothing will happen!. More knowledgeable programmers usually avoid the use of delay() for the timing of events longer than 10’s of milliseconds unless the Arduino sketch is very simple.

Certain things do go on while delay() function if running however. Things like Interrupts, Serial Communication recorded at RX pin, PWM (analogWrite) and pin states are still maintained.

We should never use the delay() function?

This option which may seem harmful is, on the contrary, extremely important in various scenarios.

  • If we consider that Arduino performs about 16 million operations per second, it would be complex to read the output of a command on a screen if it is updated at that frequency. In this case, the delay between one warning and another is used, waiting at least 500ms (half a second).
  • Another case is reading a sensor. Think of a light or temperature sensor; they are two sensors with rather high inertia and for this, it would be useless to interrogate them 16 million times in a second but it is enough 1 time per second or less.

In conclusion, we can say that sometimes where the program is compact we can use delay function also.

arduino debounce button

Still, having doubts?

Ask your questions in comment section below or Contact us.

Leave a Reply