Using a Button with Arduino [Guide + Code]

Anything cool has buttons, and using a button with Arduino is simple.

Buttons are everywhere and there is a certain pleasure in pressing them. Knowing how to employ them in your projects is very beneficial. Using a button is as easy as you might think, but at the same time can be a source of frustration.

In this example we simply hook up 5 volts to one side of a button and to the other side of the button we connect pin 2. When you press the button it completes an electrical connection, pin 2 will “see” the 5 volts and if we digitalRead() at pin 2, it will report HIGH. During the times the button is not being pressed, pin 2 reports LOW.

To turn on an LED by pressing the button, we simply make an if statement whose condition says something like “…if the voltage at pin 2 is HIGH, turn on the LED at pin 13…” It is really that easy.

We also have pin 2 connected to ground at all times through a resistor. This is because when we read values at pin 2, we want to get either a HIGH or a LOW reported. If pin 2 is not connected to ground, then when the button is not being pressed it becomes what is called a floating pin – it’s not connected to anything. Floating pins on the Arduino are fine for the most part – unless you are trying to record an input from them – then they are bad, and can give you spurious information.

You Will Need

  1. LED (1)
  2. 10,000 Ohm resistor (1)
  3. 220 Ohm Resistor (1)
  4. Momentary Push Button (1)
  5. Jumper Wires (3)
  6. Green apple

Step-by-Step Instructions

Follow these steps and you’ll be using a button with Arduino like it’s cake.

  1. Connect one of the Arduino GND pins to one of the long power rails on the breadboard – this will be the ground rail.
  2. Connect the short leg of the LED to this same ground rail on the breadboard then connect the long leg to a row on the breadboard.
  3. Connect the 220-ohm resistor from pin 13 to the same row that you have the long leg of the LED attached.
  4. Place the pushbutton on the breadboard. Most buttons will straddle the center trench on the breadboard.
  5. Connect a jumper wire from the 5-volt pin to one side of the pushbutton.
  6. Connect a jumper wire from pin 2 to the other side of the pushbutton.
  7. Connect one side of the 10k resistor from the ground rail on the breadboard to the other side to the pushbutton – on the same side that pin 2 connects.
  8. Plug the Arduino board into your computer with a USB cable.
  9. Open the Arduino IDE.
  10. Open the sketch for this section.
  11. Click the Verify button on the top left. It should turn orange and then back to blue.
  12. Click the Upload button. It will also turn orange and then blue once the sketch has finished uploading to your Arduino board.
  13. Press the button a couple times and see how the LED at pin 13 reacts.
Using a button with Arduino

This image built with Fritzing.

Code for Using a Button with Arduino

He is all the code you’ll need. As you can see, using a button with Arduino doesn’t take a ton of code.

We’ll discuss this code line by line in the next section.

/*
  Button

 Turns on and off a light emitting diode(LED) connected to digital
 pin 13, when pressing a pushbutton attached to pin 2.


 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground

 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.


 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

Using a button with Arduino line by line

A word to the wise, read the whole sketch above before going further. Reading code can make your brain hurt, but it gets less painful over time.

This sketch starts with declaring and initializing variable constants that define which pins are used for the button and LED.

const int buttonPin = 2;     // the number of the pushbutton pin

const int ledPin = 13;      // the number of the LED pin

Programming Electronics Academy members, use the coding challenges in the Bread and Butter: I/O section of the Arduino Course for Absolute Beginners to drive home these basic coding skills.

Not a member yet?  Sign up here.

The only other variable required to make this sketch work is a variable to track the status of pin 2 – we want to know if pin 2 is HIGH or LOW.

int buttonState = 0;         // variable for reading the pushbutton status

Once the variables accounted for, we move on to the setup() of the sketch. We need to let the Arduino know we will be using pin 2 as in INPUT and pin 13 as an OUTPUT.

void setup() {

     pinMode(ledPin, OUTPUT);        // initialize the LED pin as an output:

     pinMode(buttonPin, INPUT);     // initialize the pushbutton pin as an input:

}

That’s a pretty easy setup() – but it is very important. If you are going to apply voltage to a pin on your Arduino make sure that its pin mode is set as an INPUT, otherwise you could damage the pin.

Arduino pins are like people, most of us don’t like surprises – but if you tell them what is in store, they are all game for a little voltage.

The first line of code in the loop() reads the state of pin 2 and assigns the value to our buttonState variable:

buttonState = digitalRead(buttonPin); // Read the state of the pushbutton value.

As you may recall, the digitalRead() function returns the value, either HIGH or LOW, of the pin you put in the parenthesis. In this example, if the button is pressed, then 5 volts is applied to the pin and the digitalRead() function will return HIGH – and this value is stored in the buttonState variable. When the button is not pressed, digitalRead() will return LOW because it is connected to ground.

Now that we know what the button is up to, we can use an if statement condition to add functionality:

if (buttonState == HIGH) {

     // turn LED on:

     digitalWrite(ledPin, HIGH);

}else {

     // turn LED off:

     digitalWrite(ledPin, LOW);

}

For the condition of this if statement to be met, the button must be pressed. This if statement says, “If the button is being pressed turn the LED on otherwise turn it off.” The code executed by the if statement turns the LED on and off by using the digitalWrite() function.

The steps this loop() accomplishes are:

  1. Checks the state of the button.
  2. Turns the LED on or off according to the HIGH or LOW state.

Using a button with Arduino simple yet powerful. It demonstrates what you can do with some basic programming and microcontroller know-how.

Try On Your Own

  • Can you make the LED at pin 13 blink rapidly when you hold the button down?
  • Attach another LED to pin 12. Change the code, so that both LEDs illuminate with the button press.

Further Reading

installing Arduino libraries

Installing Arduino Libraries | Beginners Guide

IoT sewage project

Pumping poo! An IoT sewage project

ESP32 webOTA updates

How to update ESP32 firmware using web OTA [Guide + Code]

error message Brackets Thumbnail V1

expected declaration before ‘}’ token [SOLVED]

Compilation SOLVED | 1

Compilation error: expected ‘;’ before [SOLVED]

Learn how to structure your code