Introduction: Push-button Tutorial

About: Dude!

Push-button Tutorial

Well, a tutorial to connect a button to Arduino?

Alright, it seems to be very easy, but connecting a button to Arduino is full of hidden knowledgements, so here i’m to share it.

This tutorial explans a little about i/o pins of Ardunio, the usage of pull-up/pull-down resistors and programming (C programming skills).


Step 1: I/O Pin

- I/O Pin

Using an Arduino UNO for this tutorial, this board has several I/O pins that work as digital or analog input/output.

Let’s use the pin 11 for reading purposes and the pin 12 for output, just to write an output to LED. It’s possible to use the led already conneted to the board, pin 13.

Step 2: Push-buttonn and Pull-up/Pull-down Resistor.

- Push-button and Pull-up Resistor:

As you can see in the picture above, the push-button has 4 legs, but you just use two of them. Usually, we think, “all i have to do is connect it to VCC, or 5V, and then press it”. That’s a great idea, actually it’ll work, the software will read HIGH as long as you keep pressing it, but what if you release it? What’d happen?

That’s the point of Pull-up/Pull-down resistor, when you release the push-button, the system doesn’t know what is connected to input pin, so the pin is floating, it’s exposed to noises and the software will read HIGH and LOW non-uniformly. To fix that problem, it’s necessary a pull-up or pull-down resistor. The pull-up resistor is connected to 5V and then connected to the first button’s leg. The 11 pin goes between the resistor and the button’s leg. This way, when the button is open, the input pin is HIGH, but when it’s pressed, it goes to LOW.

The picture uploaded shows the pull-up resistor schematic developed using Fritzing.

Step 3: Push-button and Pull-up/Pull-down Resistor.

- Push-button and Pull-down Resistor:

On the other hand, if you want to use the pull-down resistor (recommended in this exemple), it will make the inverse of pull-up resistor. The 5V is connected to the first button’s leg and the second leg is conected to resistor and then connected to the GND. The 11 pin goes between the resistor and the button’s leg.

The picture uploaded shows the pull-down resistor schematic developed using Fritzing.

Step 4: Assembly, Materials and Code.

Assembly

As you can see in the picture, the assembly is very simple, but if you want to change some i/o pins, you must change the code too.

The resistor value is chosen considering the internal resistance of microcontroller, so I recommend, if you wanna know more about it, this article: Pull-Up Resistors.

The configuration of the i/o pins is described in the code.

Materials

Material needed for this tutorial are:

1 Arduino UNO, NANO...

1 Breadboard;

1 LED;

1 220Ω Resistor;

1 10kΩ Resistor;

1 Push-button;

Jumper wires.

Code

When the push-button is pressed, the LED turns on. When the push-button is unpressed, the LED turns off.

This code was made to be used with the pull-down resistor. But you can use the pull-up resistor.

#define button 11
#define led 12 boolean val = 0;
void setup()
{
  pinMode(led,OUTPUT); 
  pinMode(button,INPUT);
}
void loop()
{  
  val = digitalRead(button);     
  if (val == HIGH) 
  {
    digitalWrite (led, HIGH); 
  }
  else
  {
    digitalWrite(led, LOW);
  }
}

In the code below, there's a variable that saves the button's state, so the LED will turn on and turn off conforme it changes.

#define led 12
#define button 11 boolean x=0; boolean y=0; boolean mode = 0; void setup() { pinMode(led, OUTPUT); pinMode(button, INPUT); } void loop() { x = digitalRead(button); if ((x == HIGH) && (y == LOW)) { mode = 1 - mode; delay(20); } y = x; if (mode == 1) { digitalWrite(led, HIGH); } else { digitalWrite(led, LOW); } }

Step 5: Attachments

Attachments:

- Arduino Sketch (.ino);

- Fritzing Simulation (.fzz);

- Proteus: ISIS Schematic Capture Simulation (.DSN); (Arduino library required)

- Pictures.