Arduino: Pushbutton

[pushbutton explanation]

A pushbutton really only has two connections, even though it has four legs.

When the button is pressed, there will be a connection between all of the legs. Otherwise, the two top legs are connected, and the two bottom legs are, but there's no connection between the top and the bottom.

To see how a pushbutton works, you might want to start by wiring it up to an LED -- just use the Arduino for power and ground.

[Wiring a pushbutton, no Arduino logic yet]

Basic pushbutton

Wire up your pushbutton as shown. You can use the same LED and resistor from the blink exercise, but unplug the wire going to pin 13 and instead connect it to one side of the switch.

Then run a red wire from the other side of the switch to the Arduino pin labeled "5v". That's the Arduino's 5 volt power source. (It's like plugging it wire in to a battery.

Once you have it wired up, try pressing the button.

[Wiring a pushbutton as an Arduino sensor]

Wiring the pushbutton up to the Arduino

Now wire the resistor back to pin 13 again, like you had in your first sketch. (If you want to, you can verify that it still blinks.)

Now wire the pushbutton as shown: one side goes to 5v, the other side goes to the Arduino's pin 2.

In your Arduino sketch:

You can read the value of the pushbutton like this:

   int p = digitalRead(2);
Remember, 2 is the pin we wired the pushbutton to.

If the button is pressed, p will be HIGH.
If it's not pressed, p will be LOW. You can use if (p) to do something depending on whether the button is pressed.

So you can do something like this inside your blink sketch:

void loop() {
  int p = digitalRead(2);
  if (p == HIGH) {
    digitalWrite(13, HIGH);
    delay(1000);
    digitalWrite(13, LOW);
    delay(1000);
  }
}

[Wiring a pushbutton as an Arduino sensor]

Pull-down resistor

Is your button flaky? Does it seem like sometimes it isn't noticing when you let go of the pushbutton?

When you have the button pressed, pin 2 is connected through the switch to the Arduino's 5v power source. But what happens when the button isn't pressed?

In that case, pin 2 isn't connected to anything. We say that it's floating. It might be HIGH or it might be LOW -- there's no predicting.

So we need to make sure pin 2 goes LOW when the switch isn't pressed. To do that, we use something called a pull-down resistor. Connect a high value resistor, like 1 Mω, to the switch output and a wire from the resistor to ground.

Then, if the switch isn't pressed, there's a circuit from ground through the resistor to pin 2.

If the switch is pressed, that circuit through the resistor is still there; but there's also a direct connection to 5v through the switch. Since there's no resistor on that 5v connection, it's much easier for electricity to flow that way, and it "wins". Pin 2 sees 5v (HIGH), not ground (LOW).

Try that and see if your switch works better.

Things to try next

In your program, you did something (made the light blink) only if the switch was pressed. But what if the switch isn't pressed? You can handle that with an else:

  if (p == HIGH) {
    digitalWrite(13, HIGH);
    delay(1000);
    digitalWrite(13, LOW);
    delay(1000);
  }
  else {
    // do something else here
  }
}

Can you make the LED go on solid when the button is pressed, otherwise blink? Or maybe make it blink faster or slower when the button is pressed?

Or if you made two LEDs blink in the first exercise, try using that now: if the button is pressed, make one LED blink, otherwise make the other one blink.

More info, credits

There's a good detailed pushbutton tutorial at LadyAda.net. That's also where the button legs image came from.