Pushbutton: how to use this simple element with Arduino

button

Un pushbutton is a button that allows to interrupt or send an electronic signal. With this simple element combined with other elements you can create projects for a multitude of applications. The use of this type of pushbuttons is very common when it comes to Arduino projects. And by combining several of these buttons you can create a somewhat more complex keyboard, although there are already programmable keyboards for these uses ...

By the way, you should not confuse the pushbutton with a switch. They are totally different things. The difference is that the switch or switch is activated or deactivated with each press that is made on it. Whereas the push button will only stay in one state while pressure is being exerted on it. I have commented that it can send or interrupt, that is because there are two fundamental types of buttons.

push button symbol

Exist NO or normally open pushbuttons and NC or normally closed pushbuttons. This will also sound to you from the relays. And yes, it is exactly the same operation. When you have an NC, it will let current pass through its terminals and it only interrupts while you are pressing it. On the other hand, the NA does not let current pass when pressure is not exerted on it and will only let it pass when you press it.

Knowing that, is almost everything you need to know about a push button to start your connection and programming using Arduino. The truth is that it is such a simple element that there is not much more to say about this type of pushbuttons.

Push Button Integration with Arduino

circuit with Arduino

La connecting a pushbutton to make it interact with Arduino could not be simpler. An example is the diagram that you can see on these lines. That would be all it takes to start experimenting. But of course, with that scheme you can do little. It would be necessary for you to put a little imagination to decide what that button is going to control. In fact, if you read hwlibre.es frequently you will have already seen some articles where we have used push buttons ...

Ways to connect it

pull-up and pull-down

One thing that you should know is the issue of anti-bounce and how to connect these pushbuttons. First we go to the way to connect them, which you already know can be with the pull-up and pull-down resistors:

  • Pull-Up- With this resistor setting, when the pushbutton is pressed, the microcontroller or Arduino can see or read a zero on that pin. That is, it interprets it as a LOW signal.
  • Pull-down: In this case it is the opposite, you can read or receive a 1 or HIGH signal through the connected pin.

Do not confuse it with the NC or NA, which is something else different as we have seen previously. This is independent of the other ...

Anti-Bounce

The pushbuttons have a rebound effect when pressed. That is, when it is pressed or released there is a fluctuation in the signal that passes through its contacts and could cause it to go from a HIGT state to LOW or vice versa without really wanting that to happen. That can produce an unwanted effect on the Arduino and make it do strange things, like activate an element when we really wanted to turn it off with the push button, etc. That's because the Arduino interprets the bounces as if it had been pressed more than once ...

That negative effect it has a solution. For this, a small capacitor must be implemented in the anti-bounce circuit (hardware method) or software (modifying the source code), whether a pull-up or pull-down configuration has been used or if it is NC or NO. In all these cases, the solution must be implemented to avoid these rebounds.

For example, pull-up and pull-down circuits with the anti-bounce capacitor they would look something like this:

rebounder

While the software method It could be seen in this code snippet:

if (digitalRead (button) == LOW) // Check if the button is pressed
{
pressed = 1; // The variable changes value
}
if (digitalRead (button) == HIGH && pressed == 1)
{
// Perform the desired action
pressed = 0; // The variable returns to its original value
}

Simple project example

anti-bounce with push button and Arduino

Once we have learned the topic of the ways to connect our pushbutton and the anti-rebound circuit, we are going to see a practical example to control an LED with the pushbutton. The scheme is equally simple as you can see.

Once connected correctly, the next thing is to write the code in Arduino IDE to program your panel and start experimenting with the buttons. A simple code example to control our circuit would be the following:

// Example of sketch to control the button
int pin = 2;
int state;
pulsating int = 0;
void setup ()

{
pinMode (2, INPUT); // To read the pulse by making that pin input

pinMode (13, OUTPUT); // For the LED

Serial.begin (9600);
}
void loop ()

{
if (digitalRead (2) == HIGH)

{

pin = 2;

antiBounce (); // Call to the anti-bounce function

}
}
// Software anti-bounce function
void anti-bounce ()

{
while (digitalRead (pin) == LOW);
state = digitalRead (13);
digitalWrite (13,! state);
while (digitalRead (pin) == HIGH);

}


5 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Marcelo Castillo placeholder image said

    Cool!!! Thank you very much, I have been building a CNC and paradoxically the buttons have been the hardest thing for me to tune.

  2.   Liliana said

    Hi! I consult as a novice, in the connection with GND… ..the black wire should not come out of the negative line, which is located above the one shown in diagram 2?

  3.   John said

    Excellent explanation .. a couple of years ago I did a car ignition project and the truth is I could never make a correct keystroke. For the ignition .. I am going to try this method. I thank you very much for this great help

  4.   omar romero rincon said

    Hello, I am doing a project with three buttons and 5 LEDs with the following sequence.
    1 push button sends a signal to 2 LEDs, which I have called 1 and 2.
    the second pushbotton sends signal to 3 LEDs, called 2,3 and 4.
    my third pushbotton sends a signal to another 3 LEDs, called 3,4, 5 and XNUMX.

    I have managed to do that sequence, I only have one problem, when pressing 2 buttons, it sends a false signal to the LED that should stay on causing it to blink intermittently, I have controlled it by putting a delay (of 2 seconds, which is what I need so that the LEDs stay on and then turn off. then my question is how can I put the millis function into my program, I don't understand how millis works, I want to know if you can help me by making an example for 3 buttons using millis in each of them , I need millis to be able to press the buttons at any time without delaying the arduino.

    1.    Isaac said

      Hello Omar,
      I recommend you see our Arduino tutorial:
      https://www.hwlibre.com/programacion-en-arduino/
      And you can also see our article on millis ():
      https://www.hwlibre.com/millis-arduino/
      A greeting.