Introduction: Arduino Button With No Resistor

About: Inventor, Author, Head of Innovation, Entrepreneur, Forbes 30 Under 30, 10 Outstanding Young Persons of the World

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