Button: Arduino Basics

Button: Arduino Basics

An in-depth exploration of controlling your Arduino with a button!

  • 15,439 views
  • 0 comments
  • 13 respects

Components and supplies

Apps and online services

About this project

This project adds user input: a way to send a message to your Arduino, in the language of hardware.

Let's load up File > Examples > Digital > Button

This sketch includes a great description of the circuit right in the opening comments. Hook it up like so:

Notice how the LED's part of the circuit looks now. Most breadboards have power and ground rails running their entire length. That makes it easier to hook up circuits with just a single wire for each running back to your controller. So, both the resistor from the button and the LED are hooked back into this ground rail – instead of taking up both of the Arduino's GND pins.

The resistor we've just added is a pulldown resistor. Our code is going to check to see whether or not our digital pin 2 is connected to power, via the button-press. But while unconnected, the pin's value floats somewhere between HIGH (powered) and LOW (grounded). So, we let a bit of power flow to the ground pin all the time, through this 10 k‎Ω resistor, which "pulls it down" LOW. But when the button is pressed, power starts flowing between the 5V pin and pin 2, so it goes HIGH.

CODE

This time, we begin with some declarations of different variables that we'll be using. In the Blink sketch, if you wanted to change which pin the LED was on, you had to change pin 13 in three different places. This time, we tell the Arduino that we'll be using "buttonPin" to represent pin 2, and "ledPin" to represent pin 13. Now, anytime we swap things around physically, we just need to change that pin number in one place.

Those variables are going to stay the same the whole time the program is running, so we call them constants – hence the const int. The other variable we have will change to reflect our button-pressing, so it's just a plain int.

This time, we've got INPUTs (button) and OUTPUTs (LED) in the setup section.

In our loop, the first thing we do is check pin 2 to see whether it's HIGH or LOW, using digitalRead. Then, depending on the result, we use an if/else statement to trigger the right action. If it's HIGH (button pressed), set the LED HIGH as well. Else (otherwise), set the LED LOW.

FYI, you can have more than two conditions – in which case, you tell it "if the button is on, then turn the LED on; else if the button is not pressed, then turn the LED off; else (say the button has ceased to exist or Schrödinger is visiting) shrug your shoulders and do nothing."

Alternate circuit

There's a slightly simpler way to hook up this circuit, using the Arduino's built-in pullup resistors. As you might suspect, these are the opposite of pulldown resistors: they let a small amount of power flow between the voltage source and the pin, without you making any physical connections, so that the pin is normally held HIGH unless you've connected it to ground. Super convenient!

All you'll need to do is change the button's type to INPUT_PULLUP, and then connect the button directly to pins d2 and GND – no external resistor needed.

As you might suspect, this swaps the Arduino's behavior. Since the pin is normally HIGH now, the LED will stay on until you press the button. So, you can switch around your conditions:

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

Adapt this

So, in this case we used a button, but that's just one kind of digital input! What else can you use?

  • Switch – Add physical control to toggle different sections of code.
  • Reed switch – Sense whether a door is open or closed, using these two-part magnetic switches.
  • Wires – You don't even need a "real" button... tape two wires or pieces of metal to different surfaces and detect when they come into contact. I once built a horrible instrument this way, using weighted tinfoil balls to connect two wires as a primitive switch.

Also, see the code below for a LATCHING / TOGGLE version – a button that stays "pressed" until you hit it again.

Next steps

Get real-time text feedback from your button using the Serial Monitor!

See the whole series of Hackster 101 tutorials on Hackster and YouTube

Code

"Button": Arduino example sketchC/C++
/*
  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);
  }
}
LatchingButtonC/C++
A latching button: push once, LED turns on. Push again, LED turns off. Includes a simple debouncer, so you don't have to worry too much about how quickly you let go.
/*
  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) {
    // toggle the LED state:
    digitalWrite(ledPin, !digitalRead(ledPin);
    // debounce:
    delay(150);
  }
}

Schematics

arduino-button.png
Arduino button
arduino-button-with-pullup.png
Arduino button with pullup

Comments

Similar projects you might like

LED Blinkies: Arduino Basics

by Alex Glow

  • 5,635 views
  • 1 comment
  • 11 respects

Servo: Arduino Basics

by Alex Glow

  • 15,533 views
  • 2 comments
  • 53 respects

NeoPixel LEDs: Arduino Basics

by Alex Glow

  • 43,196 views
  • 1 comment
  • 46 respects

Basic Arduino + JavaScript (Workshop)

by Alex Glow

  • 11,873 views
  • 1 comment
  • 27 respects

Fade Lights and Push Button

Project tutorial by Phuong Vo

  • 1,639 views
  • 2 comments
  • 7 respects

You've Been Using a Button Wrong this Whole Time

Project tutorial by SMM2

  • 2,729 views
  • 1 comment
  • 3 respects
Add projectSign up / Login