🔘 Connecting button to Arduino

When you are just starting to get acquainted with Arduino, then your first scheme will most likely be connecting the LED to the board and flashing it at the frequency you specify in the program code.
arduino-and-tact-switch-connection-4

When you are just starting to get acquainted with Arduino, then your first scheme will most likely be connecting the LED to the board and flashing it at the frequency you specify in the program code. What to do if you want to turn on / off the LED (or in other words, to apply the maximum 5 volts from the board) when mechanically pressed, as in a switch in our homes?

The usual clock button that will help you get acquainted with this basic process will come to the rescue, so this article is more suitable for the smallest. But first things first.

The clock button is the simplest device that allows a person to directly influence the electrical circuit. Pressing it causes the circuit to close or open, respectively.

For the convenience of connecting a button to a circuit, for example, consisting of an LED, it is usually advantageous to use a small prototype board. And for the connection, you must use the so-called tightening resistor. What is it for?

The fact is that the button allows you to control two logical values ​​of the digital output on the Arduino board, namely: a logical zero and a logical one.

If you connect the button directly without a resistor, then when you press it to open the circuit and turn off the LED, the voltage may still occur, and the LED will turn on / off randomly (otherwise it is also called button bounce).

This is because noise is generated around the conductors and, due to their reason, a new electric field can arise. When a resistor is connected, any random and extra current will go to ground.

It is desirable to take the resistor value to 10 kΩ, and also do not forget that the current to the LED goes through a separate 220 Ohm resistor

A diagram of the connection of the button to the board, including the tightening resistor, is shown below:

Outwardly, it will look like this:

Now we connect the Arduino board to the computer via a USB cable, set all the necessary settings for it (port number, board name) and download the following code:

int button = 2;
int led = 8; 
void setup() {
pinMode(led, OUTPUT); 
pinMode(button, INPUT);      
}
void loop(){ 
if (digitalRead(button) == HIGH)                   
{
digitalWrite(led, HIGH);
}
else {
digitalWrite(led, LOW);
}
}

Now we have figured out why and how to connect a regular clock button to Arduino.

You can also connect several buttons in one circuit, i.e., turn on several LEDs by pressing one button or make sure that a particular LED corresponds to a specific button. Moreover, it is not necessary to use only an LED, instead of it, there can be a relay module.

Buttons can be connected not only to digital but also to analog outputs. This is very convenient in some schemes. The only difference is that

void setup() {
pinMode(8, OUTPUT); 
pinMode(A1, INPUT); 
}
void loop()
{
if (analogRead(A1) > 300)
{
digitalWrite(13, HIGH); 
}
if (analogRead(A1) < 300)
{
digitalWrite(13, LOW); 
}
}

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest