JavaScript For IoT

Diary of a JavaScript developer on the magical world of IoT and electronics. Join me as we share knowledge.
Let's learn together!

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:

Noise Diagram

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 help me keep this blog alive and free of ads
Support me with a small contribution by Paypal or, if you prefer, buy me a coffee.

Please leave a comment

Comments are powered by Utterances. A GitHub account is required to comment. Comments are moderated. Be respectful. No swearing or inflammatory language. No spam.

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:

  1. Click the "Comments" link
  2. Find your comment on this GitHub page and click the 3 dots in the top-right
  3. Click "Edit" or "Delete". Editing or adding a comment from the GitHub page also gives you a nicer editor.