Debouncing A Button
Electricity is too fast! What are noisy buttons, and why do we have to account for them?
In a previous article (Hello IoT (RPIO)), you may remember we had to define the button state has to be stable for 500ms before we take action.
Debouncing a button is required because when a push-button switch is pressed or released, it does not change immediately between states. Actually, mechanical vibrations can introduce noise. We have to account for this in our code by waiting for the button to stabilize.
This diagram illustrates this concept:
When I first read about this, I could not believe this would be an issue; how could this be noticeable? I remove that part of the code to see what would happen. I started getting weird results. So I decided to put the oscilloscope to use and see if I would understand what is happening. Reading about something and seeing something working is a big difference!
To make the issue more obvious, I replaced the switch for a pair of cables I manually joined, simulating a faulty button. As you can see below, this issue is very real, and we need to account for it!
If we had a different project, where the LED stays ON only while holding down the button, then we would not worry because it would be too fast for your eyes.
Please leave a comment
I reserve the right to delete any inappropriate comments. All comments for all pages can be viewed and searched online here.
Opinions expressed in this blog are solely my own and do not express the views or opinions of my employer, wife, son, friends, boss, or anybody else! 😀
To edit or delete your comment:
- Click the "Comments" link
- Find your comment on this GitHub page and click the 3 dots in the top-right
- Click "Edit" or "Delete". Editing or adding a comment from the GitHub page also gives you a nicer editor.