How To Connect A Button To Arduino

Table of contents:

How To Connect A Button To Arduino
How To Connect A Button To Arduino

Video: How To Connect A Button To Arduino

Video: How To Connect A Button To Arduino
Video: Arduino Turorial: How to use a button with Arduino Uno 2024, December
Anonim

Seems like it could be easier than connecting a button? Nevertheless, there are pitfalls here too. Let's figure it out.

Tact button
Tact button

It is necessary

  • - Arduino;
  • - tact button;
  • - 10 kOhm resistor;
  • - bread board;
  • - connecting wires.

Instructions

Step 1

The buttons are different, but they all perform the same function - they physically connect (or, conversely, break) conductors together to provide electrical contact. In the simplest case, this is the connection of two conductors; there are buttons that connect more conductors.

Some buttons, after pressing, leave the conductors connected (latched buttons), others - immediately after releasing, open the circuit (non-latching).

Also, buttons are divided into normally open and normally closed. The first, when pressed, close the circuit, the second open.

Now the type of buttons, which are called "tact buttons", has found widespread use. Bars are not from the word "tact", but rather from the word "tactile", tk. pressing is well felt with your fingers. These are buttons that, when pressed, close the electrical circuit, and when released, they open.

Different buttons and their circuit diagrams
Different buttons and their circuit diagrams

Step 2

The button is a very simple and useful invention that serves to better human-technology interaction. But, like everything in nature, it is not perfect. This is manifested in the fact that when you press the button and when you release it, a so-called. "bounce" ("bounce" in English). This is a multiple switching of the state of the button in a short period of time (on the order of several milliseconds) before it assumes a steady state. This undesirable phenomenon occurs at the moment of switching the button due to the elasticity of the materials of the button or due to micro sparks arising from electrical contact.

You can see the bounce of contacts with your own eyes using the Arduino, which we will do a little later.

Contact bounce at the push of a button
Contact bounce at the push of a button

Step 3

To connect a normally open clock button to the Arduino, you can do the simplest way: connect one free conductor of the button to power or ground, the other to the digital pin of the Arduino. But generally speaking, this is wrong. The fact is that at the moments when the button is not closed, electromagnetic interference will appear on the digital output of the Arduino, and because of this, false positives are possible.

To avoid pickup, the digital pin is usually connected through a large enough resistor (10 kΩ), either to ground or to the power supply. In the first case, this is called a "pull-up resistor circuit", in the second, a "pull-up resistor circuit". Let's take a look at each of them.

Diagram of connecting a button to Arduino directly
Diagram of connecting a button to Arduino directly

Step 4

First, we connect the button to the Arduino using a pull-up resistor circuit. To do this, connect one contact of the button to ground, and the other to digital output 2. Digital output 2 is also connected through a 10 kOhm resistor to the +5 V power supply.

Pull-up resistor circuit
Pull-up resistor circuit

Step 5

Let's write this sketch for handling button clicks and upload it to the Arduino.

The built-in LED on pin 13 is now constantly on until the button is pressed. When we press the button, it becomes LOW and the LED goes out.

Sketch for processing button clicks according to the scheme with a pull-up resistor
Sketch for processing button clicks according to the scheme with a pull-up resistor

Step 6

Now let's assemble the pull-down resistor circuit. Connect one contact of the button to the +5 V power supply, the second to digital output 2. Connect digital output 2 to ground through a 10 kΩ resistor.

We will not change the sketch.

Pull-down resistor circuit
Pull-down resistor circuit

Step 7

Now the LED is off until the button is pressed.

Recommended: