Button bouncing is another common problem that everyone faces when designing electronic circuits. This phenomenon is caused due to mechanical properties of buttons.In here I am concerned with the push button which is a type of button used often in electronic projects.
When we push (close) a switch it sends an electrical current to a microcontroller input pin. Then it is identified by that microcontroller and responds accordingly. When we push a push button it sends a sort of a pulse signal to the microcontroller as given below.

However, although switching on or off a switch may seem instant, real switches, buttons take some time to make the contact. so the real case is quite different from the ideal case. since a real push button is consists of a few mechanical contacts, when you press a button these metal contacts will touch several times before it settles down to a stable position (on or off). The following figure shows what happens when you push the button and releases it.

An oscilloscope graph of pushing a push button
As you can see there are several spikes before the switch or button settles to an “on” position. Although this whole thing occurs within few milliseconds, it is a quite a large time period with respect to a microcontroller. The result will be that the microcontroller will count these spikes as button presses (false positives). for example, if you program your Arduino to count the number of button presses, the program will output an incorrect result due to button bouncing.
There are mainly two methods which can be utilised to prevent button bouncing. They are,
- Hardware debouncing
- Software debouncing
This article deals with hardware debouncing methods. I highly recommend you to read it as it will give you a complete understanding. In this article, I would like to focus on software debouncing as it feels the easiest way to overcome bouncing.
As I have mentioned earlier, bouncing occurs due to button contacts making several contacts before it reaches to its desired position (state). So all we need to do is to tell the microcontroller to ignore the signals coming from the button for a certain amount of time. since we know these bounces take less than 1ms, we can program the controller to start accepting input signals after about 5ms. In this way, the microcontroller will not see those undesired spikes hence it will give us correct readings.
In this project, I am using my push button in 3 different ways.
- Toggle a LED using a push button.
- Count the number of button presses to control a LED.
- Measuring time between pushing and releasing a push button.
All these have a practical aspect since we use these functionalities in actual designs. For examples, we often use the same button to switch on/off electronic devices.Moreover, the same button reacts differently depending on the duration it was pressed. All these are possible due to the correct implementation of button debouncing techniques.
Toggle a LED using a push button
In this exercise, a push button is used to turn on and turn off a LED. although the task is simple, implementing correct functionality is difficult due to button bouncing. As a result, I used a software debounce technique to eliminate button bouncing.
The following picture shows the circuit I used to do the exercise.

The circuit to demonstrate button debouncing

I was able to perform all three exercise just using this circuit.
Initialization of the circuit was done as follows.

Initialization of program and board
In this piece of code the variables, “lastbtnstate” and “currentbtnstate” are used to store the status of LED at the moment and one before. “LEDstate” is used to toggle the LED (turn it on or off).

Button debounce Function
In this function, it returns a boolean value as the state of the button. Firstly, it records the current state of the button (btnstatenow). Secondly, it compares the current state of the button with the previous state (previous) to identify whether it is being pressed or released. if those states are not the same, then the function enters to a delay. This delay avoids button debouncing which would last for about 1ms.You can play with delay interval but I recommend using something like 5-10 ms will do the job. After that delay, the function once again reads the button state and returns its value.
The code I used for loop() is as follows,

In this function, it passes the previous state of the button to button debounce function and stores the current state of the button (High or Low) in the currentbtnstate variable. After that, I used if condition to detect the button press. If statement toggles the variable (LEDstate) that controls the state of LED (on or off). Finally, the button status is updated.
Count the number of button presses to control a LED.
Next task is to count the number of button presses that can be used to trigger something else. In this exercise, I light up a LED for 1 second and turn it off after the program detects 10 button counts.
In this, the main goal is to count the correct number of button presses. It is impossible to do this without using debouncing as the microcontroller counts unwanted button bounces as legit button presses hence turning on the LED at a random point.
Initialization is done as follows,

In this program, the variable count is used to store the number of button presses. You can use the same function that was used in the previous exercise to debounce button presses. So I just copy and pasted it to this program.
The Following figure shows the main function of the program.

Similar in task 1, at the beginning the previous button state is passed to button debouncing function and the current button state is stored. Then it is used to detect a button press using if condition. if the program detects a button press, it increases the counter variable by one and toggles the button state. then another if condition detects the button release and assign Low state to current button state. If the counter hits 10, then the program turns on LED for 1 second and turns it off and resets the counter to 0. Using debouncing guarantees only legitimate button presses are being detected.
Measuring time between pushing and releasing a push button
This functionality is another useful thing about push buttons as it allows us to execute different things depending on the duration of a push button being pressed. In order to do this, I used a built-in function in Arduino called millis(). This returns the number of milliseconds since the board started to run the program.
The concept is very simple. First I programmed the microcontroller to store a time measurement when the button is pressed. This is our starting time. Then I store another time measurement inside another variable when the button is released. Finally, I take the difference in these two measurements and divide it by 1000 to convert it into seconds.
The following picture shows the initialization code.

As usual, you can use the same button debouncing function in this instance as well.
The following picture shows the main function in this program.

You can download all three exercises using this link.Feel free to use them and change them.
I hope to see you all soon with another project.






















