Push buttons can be used to control LEDs, relays, motors, and pretty much anything else you can think of.

In this article, we will learn how to connect and program a push button on the Arduino. We will also learn about floating pins, pull up and pull down resistors, the digitalRead() function, and the Arduino’s internal pull up resistor. After reading this article, you’ll be able to add push buttons to any project.

Watch the video for this tutorial here:

The 3-in-1 Smart Car and IOT Learning Kit from SunFounder has everything you need to learn how to master the Arduino. It includes all of the parts, wiring diagrams, code, and step-by-step instructions for 58 different robotics and internet of things projects that are super fun to build!

Introduction to Push Buttons

Push buttons are available in a variety of different formats:

Different Types of Push Buttons

The push button we will use in this article is also known as a tactile switch or momentary push button:

Tactile Push Button.jpg

The pins on each side of the button have electrical contacts inside the button housing. The button itself has an electrically conductive piece of metal attached to it. When the button is pressed, the circuit is closed between the pins on each side and current is allowed to flow between them:

Push Button Diagram - Pressed vs Not Pressed

Example Project

To demonstrate how to control devices with a push button, let’s build a circuit that turns on an LED when the button is pressed. The LED is just an example, you can use this circuit to control any device powered by a 5 volt signal.

These are the parts needed to build the project:

Follow this diagram to connect the circuit:

LED Push Button No Pull Down Resistor.png

The current limiting resistor value can be anything from 200 Ohms to 1K Ohms.

One side of the push button is connected to 5 volts, and the other side is connected to pin 7. When the button is pressed, current will flow to pin 7 making it go high. We will use the digitalRead() function to detect when that happens. Then we will use the digitalWrite() function to set pin 11 high, making the LED light up.

How to Program a Push Button on the Arduino

Once you have the circuit connected, upload this code to the Arduino:

int buttonPin = 7;
int ledPin = 11;

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  int buttonState = digitalRead(buttonPin);
  digitalWrite(ledPin, buttonState);
}

At the top of the sketch we declare two pin variables. The buttonPin variable will hold the pin number of the Arduino pin connected to the button (pin 7). The ledPin variable will hold the Arduino pin number connected to the LED.

In the setup() section, we use the pinMode() function to set buttonPin as an input. Then we set the ledPin as an output.

In the loop() section, we declare an int variable called buttonState and set it equal to digitalRead(buttonPin). When the button is not pressed, the voltage at the buttonPin will be low, so the digitalRead() function will return a low value. The low value is stored in the buttonState variable. When the button is pressed, the voltage at the buttonPin will be high, so a high value will be stored in the buttonState variable.

We use the digitalWrite() function to send a voltage signal to the LED. The pin we want to send the voltage to is the ledPin so that is the first argument. The second argument of digitalWrite() tells the function to send either a high or low voltage to the pin. But instead of writing high or low, we can use the buttonState variable and the function will write whatever value is stored in that variable to the ledPin.

Floating Pins

If you build the project above and test it, you’ll notice that something weird is going on. The LED will probably flash on and off whenever you move your hand close to the button. What could be causing that?

The Arduino’s digital pins are extremely sensitive. Even weak electromagnetic fields created by your hand can be detected by the Arduino. And those are registered as high signals by the digitalRead() function.

When GPIO pins are allowed to pick up stray electromagnetic fields, they’re called floating pins. We need to fix this by making sure the buttonPin stays low when the button is not pressed. So how can we do that?

Pull Down Resistors

The easiest way is to connect a resistor from the left side of the push button to ground, like this:

LED Push Button With Pull Down Resistor.png

When the button is not pressed, stray electromagnetic energy will flow to ground through the resistor. When the button is pressed, the resistor will restrict the current flow to ground and the current will flow to pin 7. This is called a pull down resistor because it connects a pin to ground to keep it in a low voltage state. The value of the pull down resistor can vary, but is usually higher than 10K Ohms.

Pull Up Resistors

Pull up resistors are more common than pull down resistors. Pull up resistors are connected to a voltage source and keep the pin in a high voltage state:

LED Push Button With Pull Up Resistor.png

In this circuit, the pull up resistor is connected to 5 volts, and the right side of the button is connected to ground. Pressing the button will send a low signal to pin 7, turning the LED on. The pull up resistor is tied to 5 volts and keeps pin 7 high until the button is pressed.

The code for using a pull up resistor looks like this:

int buttonPin = 7;
int ledPin = 11;

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  int buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {
    digitalWrite(ledPin, HIGH);
  }

  if (buttonState == HIGH) {
    digitalWrite(ledPin, LOW);
  }
}

Similar to the previous program, we declare variables for the buttonPin and ledPin variables and set them as outputs. Then we use the digitalRead() function to detect the voltage state of the buttonPin and store it in the buttonState variable.

We want the Arduino to send a high signal to the ledPin when the buttonState variable is low. In the loop() section, we use two if statements to control what happens when the buttonState variable is high or low. If buttonState is low, the first if statement is executed and the ledPin is written high. If the buttonState variable is high, the program enters the second if statement and a low voltage is written to the ledPin.

So now when you press the button, the LED should turn on and not flicker when you move your hand around the circuit. The floating pin problem is solved.

Arduino’s Internal Pullup Resistor

The pull up and pull down resistors we looked at in this article are external circuit components. But the Arduino also has an internal pull up resistor that you can use for the same purpose. To use the Arduino’s internal pull up resistor, use INPUT_PULLUP as the second argument in the pinMode() function for the buttonPin like this:

int buttonPin = 7;
int ledPin = 11;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  int buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {
    digitalWrite(ledPin, HIGH);
  }

  if (buttonState == HIGH) {
    digitalWrite(ledPin, LOW);
  }
}

Now you can wire the circuit without the pull up resistor connected to 5 volts. The internal pull up resistor uses one less component and makes the circuit a bit simpler.

Hope this article has helped you to understand the different ways to use a push button for controlling devices with the Arduino. Be sure to leave a comment below if you have any questions!