Switch bouncing and debouncing with Arduino

(click to enlarge)

This experiment is similar to the previous one. The components and the circuit are the same as that of this experiment of the Pull down resistor. In that experiment we used a momentary switch to momentarily turn on the LED. Whereas in today's experiment we will turn on the LED and keep it on until the button is pressed again. Hence it will work as a Toggle switch instead of a momentary switch. We can achieve this effect by making changes in the code that we upload on the Arduino's board.

We are going to to use the example program in Arduino's IDE
File - Examples - Digital - Debounce

But the name is strange!, it is not toggle or something similar. It is Debounce. What is de-bounce ? What is it that is bouncing in this circuit that we need to de-bounce? Let us try to understand that below.


  • Arduino Uno's board with USB cable
  • one breadboard small
  • one Tact Switch
  • one LED
  • Resistors 220 Ohms, 10 K Ohms, one each.
  • Jumper wires (Male-Male)
You can find all the components needed for  this experiment in Any Arduino starter kit. If you do not have one, you can find one on Amazon.in from the link in the right column.


You will need the Arduino's programming software for this experiment. You can download and install it from this link if you haven't done it already.


The wiring diagram for this experiment is shown below. 

LED Toggle switch 

(click to enlarge)

This is the same circuit that we used in the previous experiment. Please see this article if you haven't read it already.  After connecting the components, open and upload this program from examples in Arduino's IDE

File - Examples - Digital - Debounce

After uploading the program you can turn the LED on/off just like a normal switch button in your home.

But where does debounce figure in this ?  For that we will have to understand what bouncing is with respect to a switch. And we will have to use a code that does not include the debouncing code in it. You can read this simple toggle code on this link.

You can also download it from this link Toggle.ino  which you can upload directly on Arduino's board.

This code is written to toggle the LED on/off with each press. You will be able to see the bouncing effect each time the switch is pressed.

Bouncing and Debouncing

Bouncing occurs in almost all the mechanical switches, including this one that we are using in this experiment.  The parts of the switch are shown in the above picture. This is SPST - single pole single throw, normally open switch. Which closes momentarily as long as you keep the button pressed. There are metal pins at the bottom, and there is a circular metal disk below the button, which is pushed down when we press the button, this disk comes in contact with the upper portion of the pins, and thus the circuit is completed.

As the mechanical property of a metal, when two metal objects collide, there is a vibration created in both of the pieces. You can notice it when you strike two spoons together, or struck a plate with a spoon. Same thing happens here. But in a tiny scale. The force applied to push the button causes the metallic disk in the switch to vibrate a little. This vibration lasts for a few milli seconds, but this vibration is enough to cause an effect of continuously pressing and releasing the button in rapid succession. The button actually toggle several times during a few milli seconds each time it is pressed. This is called the Contact Bouncing or the Switch Bouncing. This will create unexpected effect on the circuit. As in this circuit. The microcontroller is fast enough to detect the toggle and execute the code accordingly several times in a few milli seconds. The LED will turn/off rapidly but it will not be detectable to our eyes. What we can detect though, is when the switch contact stabilizes, then instead on toggling on/off as we would expect it would have toggled the LED dozens of times, and randomly so sometimes the LED will remain ON or OFF instead of changing state.

This is the effect of the contact bouncing or the switch bouncing. There are different ways to rectify this problem. One is called hardware de-bouncing and another is software de-bouncing. As we are using a code to run the circuit, we can employ the code to de-bounce.

It is a simple strategy. The bouncing occurs for a few milli seconds. Therefore we detect the first change in the state of the switch within 50 milliseconds, and then add another 50 milli seconds to make the micro controller wait to settle the bouncing the contacts and then execute the further code.

The code in the Arduino;s IDE is one such code in which debouncing is implemented. You will have to study the debouncing code thoroughly as you will have to implement this code in your future experiments in Arduino.


We learned how to use the Tact switch/ Push button/ momentary switch to use as a toggle switch with help of companion code. We also learned how the switch or contact bouncing affects a circuit and how to overcome or avoid the bouncing by adding de-bouncing code in the program.

Popular posts from this blog

Introduction to Scratch Programming for ICSE Class 5

Hour of Code - Frozen with Anna and Elsa

Programming for kids - Course 4 # Artist for loops