loading

It is simple to connect a button to the Arduino. You need the button, some wires, and a resistor. But what if we no longer need the resistor and want to still be able to use the button with no false readings?

The resistor is mandatory for proper operation of a button, and everybody will insist on using it. However, there is a little secret embedded in each Arduino pin. Each pin already has a pull-up resistor that we can enable with just one small change in our code.

For this instructable we will need just two components:

  • An Arduino board connected to a computer via USB
  • A push button
This instructable and many more can be found in my Arduino Development Cookbook available here. :D

Step 1: How to Connect Them

Simply connect the Arduino GND to a terminal on the button and connect a digital pin to the other button terminal. In this example we used pin 12. Check the schematic and "breadboard" implementation.

For most buttons with standard through-hole terminals, we can directly input the pins into the terminals on the Arduino.

Step 2: Code

The following code will read if the button has been pressed and will control the built-in LED:

// Declare the pins for the Button and the LED<br>int buttonPin = 12;
int LED = 13;

void setup() {
   // Define pin #12 as input and activate the internal pull-up resistor
   pinMode(buttonPin, INPUT_PULLUP);
   // Define pin #13 as output, for the LED
   pinMode(LED, OUTPUT);
}

void loop(){
   // Read the value of the input. It can either be 1 or 0
   int buttonValue = digitalRead(buttonPin);
   if (buttonValue == LOW){
      // If button pushed, turn LED on
      digitalWrite(LED,HIGH);
   } else {
      // Otherwise, turn the LED off
      digitalWrite(LED, LOW);
   }
}
If the button is connected to a different pin, change the buttonPin value to the value of the pin that has been used.

Step 3: Code Breakdown

The code takes the value from the button. If the button is pressed, it will start the built-in LED. Otherwise it will turn it off.

Here, we declare the pin to which the button is connected as pin 12, and the built-in LED as pin 13:

int buttonPin = 12;<br>int LED = 13;

In the setup() function, we set the button pin as a digital input and we activate the internal pull-up resistor using the INPUT_PULLUP macro. The LED pin is declared as an output:

void setup() {<br>   pinMode(buttonPin, INPUT_PULLUP); 
   pinMode(LED, OUTPUT);
}

In the loop() function, we continuously read the value of the button using the digitalRead() function, and we store it in a newly declared variable called buttonValue:

int buttonValue = digitalRead(buttonPin);

Lastly, depending on the button state, we initiate another action. In this case, we just light up the LED or turn it off:

if (buttonValue == LOW){<br>   // If button pushed, turn LED on
   digitalWrite(LED,HIGH);
} else {
   // Otherwise, turn the LED off
   digitalWrite(LED, LOW);
}

Step 4: More About Buttons

It is easy to connect a button to the Arduino without any resistors. What if we need more buttons?

Each button requires its own digital pin and resistor. The Arduino already has one pull-up resistor in each digital and analog pin, so in the end, all that is needed is one pin for each individual button. The other terminal of the buttons is tied together to GND.

More topics regarding buttons such as button debouncing, connecting 1000 buttons on one pin or button multiplexing can be found in my Arduino Development Cookbook available here. :D
<p>great tutorial </p><p>You Saved my day</p>
<p>Wow! Game changer!</p><p>thanks a ton \m/</p>
<p>As a question for making my own instructables better, how did you get the code to be in the boxes like that?</p>
<p>Thanks for the instructable. It really helped me a lot. I don't have to dig through my bin of resistors every time i want to test a button. Also it saves some space. Thank you for the help!</p>
Thanks for the ible. I am learning and really appreciate good information.
Good stuff, I have used this very useful feature to its maximum on my pong watch, only that uses a PIC. <br>https://www.instructables.com/id/Pong-Watch<br>I love using the least components possible in my designs. It's funny though, I never think of pointing things out like that to people as I assume they have read the device data sheet. It's kinda common knowledge, but I guess Arduino has brought embedded electronics to the new budding hobbiests. :-)

About This Instructable

16,411views

192favorites

License:

Bio: Inventor, Author, Senior R&D Engineer, Entrepreneur, Forbes 30 Under 30
More by cornelam:Arduino Servo Motors Arduino Distance Sensors Arduino Stepper Motors 
Add instructable to: