How to Use a Push Button - Arduino Tutorial

286K24317

Intro: How to Use a Push Button - Arduino Tutorial

Push buttons or switches connect two points in a circuit when you press them. This example turns on one led when the button pressed once, and off when pressed twice.

In this tutorial you will also learn how to use 'flag' variable to control an event.

So, let's get started!

STEP 1: What You Will Need

For this tutorial you will need:

  • Arduino uno
  • Breadboard
  • LED
  • 220 Ohm & 10 KOhm resistor
  • Push button

STEP 2: The Circuit

When the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to ground (through the pull-down resistor) and we read a LOW. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to 5 volts, so that we read a HIGH.


Tip:

You can also wire this circuit the opposite way, with a pullup resistor keeping the input HIGH, and going LOW when the button is pressed. If so, the behavior of the sketch will be reversed, with the LED normally on and turning off when you press the button.

If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it will randomly return either HIGH or LOW. That's why you need a pull-up or pull-down resistor in the circuit.

STEP 3: The Code

Here's the 'Button' code, embedded using codebender!

Keep in mind that setup( ) routine runs only once after power on / re-program or press the reset button. In the program below, the first thing you do is to initialize pin 9 as an output pin with pinMode( ) function in setup( ) routine.

The loop( ) routine runs over and over again, forever. In the main loop, you read the state of button (pressed=high, unpressed=low) and you store it in buttonState variable. When button pressed once, the led turns on, and when pressed twice, the led turns off.

Read comments below

Try downloading the codebender plugin and clicking on the Run on Arduino button to program your Arduino with this sketch. And that's it, you've programmed your Arduino board! You can keep playing with that by clicking the "Edit" button and start making your own modifications to the code. For example you can add a second led or make it blink when button pressed.

STEP 4: Well Done!

You have successfully completed one more Arduino "How to" tutorial and you learned how to use:

  • buttons
  • flag variable to control an event

I hope you liked this, let me know in the comments.

There will be more of them, so make sure to click Follow button :)

8 Comments

There is an error in the code that is the cause of a number of the other comments.

Line 26 should be "if (buttonState == HIGH) {" not "if (buttonState == LOW) {" since in the circuit diagram the button pin is connected to the top of the resistor after the button, so it is by default LOW and not HIGH.
Thank you!!, i would like to ask how can i press push button and it remais in one state until i press it again for changing state, plz tell me friendz.

Hello. i'm new on arduino and i wonder if you tell me about this: when i push and release button, motor works until i push and release button again?

I am running an 8 relay board turning lights on and off in sequence. When I first start the Arduino all the relays come on and everything lights up then shuts off and that's fine (good way to check to see if everything is working) but after that I want to just run the loop on a button press. My code has an exit(0); to stop the loop at the end of the run which should in effect turn it off until the next time I start the loop. Any suggestions?

Yep so im stuck the light just stays on I think its problem that by button is on the bread board like the wrong way round ? but not sure how to test or fix this ?

Cool!

But did you know that by declaring the button pin like this:

pinMode(buttonPin, INPUT_PULLUP);

you can use the built in pullup resistor in your arduino.

then since it is a pullUP resistor you would connect the button from the pin to gnd. so when the button is pressed it reads LOW and when the button is not pressed it reads HIGH

more info: https://www.arduino.cc/en/Reference/PinMode & https://www.arduino.cc/en/Tutorial/InputPullupSerial

Hi! Thank you for your comment!! That's cool and correct ;)

Nice work. It would be great to see either a software or hardware debounce discussed here. An actual schematic would be good too, so beginners actually learn something used in real electrical engineering.