Project tutorial
You've Been Using a Button Wrong this Whole Time

You've Been Using a Button Wrong this Whole Time © GPL3+

This tutorial will show you the easiest and best way to use a pushbutton with your Arduino board!

  • 3,370 views
  • 1 comment
  • 4 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
×1
Omron b3f 1000 image 75px
SparkFun Pushbutton switch 12mm
You'll need this!
×1
09590 01
LED (generic)
×1
12002 04
Breadboard (generic)
×1
Mfr 25fbf52 221r sml
Resistor 221 ohm
Since I am using a red LED I will actually be using a 220 ohm resistor (Red-Red-Brown-Gold). Though, if you are using a different color LED you may want to make sure you have the right resistor for it.
×1
11026 02
Jumper wires (generic)
×1

Apps and online services

Ide web
Arduino IDE
Ide web
Arduino Web Editor
You can use either one of these.

About this project

As you probably know the most common way to connect a button to Arduino is to do something like this:

Now, this isn't wrong to do it this way, but there is an easier and better way that you may not know about!

So as you can see in the picture there are three wires and one resistor connected to the button. One wire for Ground, one wire for 5V, and one wire that connects to a digital pin. Also a 100 ohm resistor. Though, we can do this same thing with only 2 connections and no resistor will be needed! The explanation is as follows.

Explanation:

So, the way/reason we can connect a button to Arduino with only 2 connections and NO resistor is actually quite simple. Basically, each digital pin has an internal pull-up resistor that we can access through software. These pull-up resistors have values that range normally from 20K - 50K ohms. To do this through software all we have to do is set the pinMode() of a digital pin to INPUT_PULLUP. Doing this "reverses" the way an input pin works, where HIGH would mean off and LOW means on. And when we give a pin the mode INPUT_PULLUP the other end of the component is supposed to be connected to Ground. Therefore, using Arduino's internal pull-up resistors allows us to not have to use an external resistor and using this has some other nice advantages.

Trying it out

Now that we have learned about Arduino's internal pull-up resistors and how it works, now's the time to apply it to our application. In this example we are going to use an LED along with our push button to see it in action. Let's look at the circuit.

The Circuit

As you can see from the Breadboard diagram below we are using one wire to connect one pin of the button to ground, and another wire connected to digital pin 12. See how simple that is! Now on to the LED. The anode (longer lead) is connected to digital pin 13, and the cathode (shorter lead) is connected to a resistor that is going to ground. Now on to the software part of this project.

The Code

You can see the code by just clicking on the section named Code in the Table of Contents. You've probably noticed that this sketch looks very similar to the built-in Button example in the Arduino IDE. And you're right, it is very similar to the Button example but it has a few small changes. One difference is that I don't use the variable buttonState, but you can if you wish. Now we come to the most important difference, the difference that makes this work. The difference is that instead of setting the mode of the button to INPUT, I set it to INPUT_PULLUP. I have already explained why so in the Explanation section. There's one last change we have to make.

As I talked about earlier the INPUT_PULLUP works basically in the opposite manner than the INPUT mode. Meaning that HIGH actually means the component is off and LOW means the component is on. So, when the button is not being pressed the state of the button will remain HIGH, but when the button is pressed the state will change to LOW.  So in our sketch we have to say if the button reads LOW turn the LED on, and if the button reads HIGH turn the LED off. Now try it out, when you press the button the LED should turn on and when you don't press the button it should remain off. If not double check over the circuit and the code.

So I hope from this tutorial that you've learned something new, and can apply it in your future projects.

-SMM2

Code

CodeArduino
const int led = 13;
const int button = 12;

void setup() {
  // put your setup code here, to run once:
pinMode(led, OUTPUT);
pinMode(button, INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:
if (digitalRead(button) == LOW) {
  digitalWrite(led, HIGH);
}
if (digitalRead(button) == HIGH) {
  digitalWrite(led, LOW);
}
}
Arduino Web Editor link to the code

Schematics

Breadboard Diagram
Make the connections as shown.
Led button bb
Schematic Diagram
Led button schem

Comments

Similar projects you might like

Fade Lights and Push Button

Project tutorial by Phuong Vo

  • 2,157 views
  • 2 comments
  • 8 respects

What to Wear? Twitter Button

Project showcase by Arduino “having11” Guy

  • 2,762 views
  • 0 comments
  • 6 respects

Stopwatch and Lap Time Counter

Project showcase by LAGSILVA

  • 13,889 views
  • 5 comments
  • 24 respects

Simple Programmable Robotic Arm

Project showcase by Ryan Chan

  • 49,015 views
  • 63 comments
  • 145 respects

Arduino Simple "Camera Slider" Electronics

Project tutorial by Ian Cumming

  • 6,474 views
  • 3 comments
  • 18 respects

RGB LED Button Controller

Project showcase by Re

  • 7,945 views
  • 1 comment
  • 12 respects
Add projectSign up / Login