How To Get Rid Of Contact Bounce When Connecting A Button To Arduino

Table of contents:

How To Get Rid Of Contact Bounce When Connecting A Button To Arduino
How To Get Rid Of Contact Bounce When Connecting A Button To Arduino

Video: How To Get Rid Of Contact Bounce When Connecting A Button To Arduino

Video: How To Get Rid Of Contact Bounce When Connecting A Button To Arduino
Video: Arduino Button Debounce Tutorial 2024, May
Anonim

We have already looked at connecting a button to the Arduino and touched on the issue of "bouncing" contacts. This is a very annoying phenomenon that causes repeated button presses and complicates programmatically handling button clicks. Let's talk about how to get rid of contact bounce.

Contact bounce effect
Contact bounce effect

Necessary

  • - Arduino;
  • - tact button;
  • - 10 kOhm resistor;
  • - Light-emitting diode;
  • - connecting wires.

Instructions

Step 1

Contact bounce is a common phenomenon in mechanical switches, pushbuttons, toggle switches and relays. Due to the fact that contacts are usually made of metals and alloys that have elasticity, when physically closed, they do not immediately establish a reliable connection. Within a short period of time, the contacts close several times and repel each other. As a result, the electric current takes on a steady-state value not instantly, but after a series of ups and downs. The duration of this transient effect depends on the contact material, size and design. The illustration shows a typical oscillogram when the contacts of the tact button are closed. It can be seen that the time from the moment of switching to the steady state is several milliseconds. This is called "bounce".

This effect is not noticeable in electrical circuits to control lighting, motors, or other inertial sensors and devices. But in circuits where there is a fast reading and processing of information (where the frequencies are of the same order as the "bounce" pulses, or higher), this is a problem. In particular, the Arduino UNO, which operates at 16 MHz, is excellent at catching contact bounce by accepting a sequence of ones and zeros instead of a single 0 to 1 switch.

Contact bounce when pressing a button
Contact bounce when pressing a button

Step 2

Let's see how contact bounce affects the correct operation of the circuit. Let's connect the clock button to the Arduino using a pull-down resistor circuit. By pressing the button, we will light the LED and leave it on until the button is pressed again. For clarity, we connect an external LED to digital pin 13, although the built-in one can be dispensed with.

Connecting a button to an Arduino using a pull-up resistor circuit
Connecting a button to an Arduino using a pull-up resistor circuit

Step 3

To accomplish this task, the first thing that comes to mind:

- remember the previous state of the button;

- compare with the current state;

- if the state has changed, then we change the state of the LED.

Let's write such a sketch and load it into the Arduino memory.

When the circuit is turned on, the effect of contact bouncing is immediately visible. It manifests itself in the fact that the LED does not light up immediately after pressing the button, or lights up and then goes out, or does not turn off immediately after pressing the button, but remains on. In general, the circuit does not work stably. And if for a task with turning on the LED this is not so critical, then for other, more serious tasks, it is simply unacceptable.

Sketch of processing button pressing without taking into account contact bounce
Sketch of processing button pressing without taking into account contact bounce

Step 4

We will try to fix the situation. We know that contact bounce occurs within a few milliseconds after the contacts are closed. Let's wait, say, 5ms after changing the state of the button. This time for a person is almost an instant, and pressing a button by a person usually takes much longer - several tens of milliseconds. And Arduino works great with such short periods of time, and these 5ms will allow it to cut off the bounce of contacts from pressing a button.

In this sketch, we will declare the debounce () procedure ("bounce" in English is just "bounce", the prefix "de" means the reverse process), to the input of which we supply the previous state of the button. If a button press lasts more than 5 msec, then it is really a press.

By detecting the press, we change the state of the LED.

Upload the sketch to the Arduino board. Everything is much better now! The button works without fail, when pressed, the LED changes state, as we wanted.

Sketch of processing a button press, taking into account contact bounce
Sketch of processing a button press, taking into account contact bounce

Step 5

Similar functionality is provided by special libraries such as the Bounce2 library. You can download it from the link in the "Sources" section or on the website https://github.com/thomasfredericks/Bounce2. To install the library, place it in the libraries directory of the Arduino development environment and restart the IDE.

The "Bounce2" library contains the following methods:

Bounce () - initialization of the "Bounce" object;

void interval (ms) - sets the delay time in milliseconds;

void attach (pin number) - sets the pin to which the button is connected;

int update () - updates the object and returns true if the pin state has changed, and false otherwise;

int read () - reads the new state of the pin.

Let's rewrite our sketch using the library. You can also remember and compare the past state of the button with the current one, but let's simplify the algorithm. When the button is pressed, we will count the presses, and each odd press will turn on the LED, and each even press will turn it off. This sketch looks concise, easy to read and easy to use.

Recommended: