Introduction: How to Use a Push Button - Arduino Tutorial

Picture of 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

Picture of 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

Picture of 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 :)

Comments

redkcir (author)2017-07-16

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?

DavidB552 (author)2016-10-19

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 ?

Ploopy (author)2015-08-07

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

codebender_cc (author)Ploopy2015-08-15

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

billbucket (author)2015-06-01

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.

About This Instructable

82,224views

228favorites

License:

Bio: Arduino Tutorials by Codebender.cc Team
More by codebender_cc:How to use a magnetic door switch / sensor with ArduinoHow to Measure LUX With ArduinoHow to Control an RGB LED Strip - Arduino Tutorial
Add instructable to: