Skip to content

Pushbuttons

A pushbutton provides an easy way for the user to interact with your sketch. This example requires the following external components connected to your Arduino - please consult the appropriate component information page if you have not used these before:

The code for this example is shown in the box below. You can load it into your Arduino IDE from the File menu: File → Examples → 02.Digital → Button

The aim is to control the internal LED so that it is on when the button is pressed, but off otherwise.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    // 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);
      }
    }

Before you can run the example, you will need to construct the following circuit. Please make sure that your Arduino is not connected to the PC while you are making hardware changes.

When everything is ready, connect your Arduino, upload the sketch and test your circuit. Here are some troubleshooting tips:

  • If the LED is dimly lit all the time, your button may not be properly connected. Try pressing it firmly into the breadboard.

  • If the LED does not come on, check all of the connections to make sure they are the same as shown in the diagram

What is the resistor for?

There may be unexpected sources of voltage in a circuit which produce unwanted effects. In this case, we want to keep the voltage at the input pin low when the switch is open. To do this, we connect a 1kΩ resistor from the  pin to ground. When the button is pressed,  the voltage 'prefers' the low-resistance route through the pin which then recognises a high voltage signal. This arrangement is known as a pull-down resistor.

Add an external LED

This variation combines the button example with the blink example. You will need to make some changes to your circuit. Follow the example in the diagram below.

We are connecting the LED on pin 13. Because the internal LED is also connected to this pin, we do not need to make any changes to the sketch.

If your external LED does not work as expected, try the following:

  • If the LED does not come one when you press the button, check that it is connected the right way round. See the LED information page for more help.

*If the LED still does not come on, check all of the connections to make sure they are the same as shown in the diagram

Here are some points to notice:

  • The external LED takes its power from the output pin. There is no need to connect its anode to the power bus.

  • The cathode of the LED is connected to the ground on the power bus. All of the ground pins on the Arduino (there are three of them) are kept at the same low voltage.

  • The circuit is modular - it can be divided into sections. Remember this when you are constructing more complicated circuits. You can combine elements from earlier examples for more complex behaviour.

Add another button

This variation requires changes to the sketch as well as some hardware changes. Basically, we are going to make a second copy of the existing circuit. Let's do the hardware first:

Some things to notice:

  • Colour-coding. The wires on the diagram are carefully coloured so that it is easy to understand. Connections to the high voltage on the power bus are red and the ones to the low voltage are black. The two sets of signal connections are different colours. Try to use similar colours when you construct a circuit on your breadboard. This will make it easier to find problems and make changes.

  • The two sets of components are laid out in the same way. This is like colour-coding - it makes it easier to find problems and make changes.

Now for the code...

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// constants 
const int button0 = 2; // the number of the first pushbutton pin
const int button1 = 3; // the number of the second pushbutton pin 
const int led0 = 13; // the number of the first LED pin 
const int led1 = 12; // the number of the second LED pin

// variables
int buttonState0 = 0; // variable for reading the status of pushbutton 0
int buttonState1 = 0; // variable for reading the status of pushbutton 1

void setup() { 
    // initialize the LED pins as outputs: 
    pinMode(led0, OUTPUT); 
    pinMode(led1, OUTPUT);

    // initialize the pushbutton pins as inputs: 
    pinMode(button0, INPUT); 
    pinMode(button1, INPUT); 
}

void loop() {
    // read the state of the pushbutton values: 
    buttonState0 = digitalRead(button0); 
    buttonState1 = digitalRead(button1);

    // check button0 
    if (buttonState0 == HIGH) { 
        digitalWrite(led0, HIGH); 
    } else {
        digitalWrite(led0, LOW); 
    }

    // check button1 
    if (buttonState1 == HIGH) {
        digitalWrite(led1, HIGH); 
    } else { 
        digitalWrite(led1, LOW); 
    }
}

Some things to notice:

  • It is a good idea to use meaningful variable names

  • It is also a good idea to show which variables are related - in this example, button0 controls led0

  • Variable names do not have to match the pin numbers

Further reading

Original tutorials

Pull Up and Pull Down Resistors