I need some code to pause the Arduino code until a button is pressed. I have a long program and at point I need a condition that if a Button on pin A2 is pressed then go forward, otherwise wait.

I tried using if with a condition but it failed for my purpose. I think this is because when if(...) detects that the button is not pressed, it moves to next lines (as usual). But I have a long program and I want to wait forever until the button is pressed.

  • Is it active high or active low (are you using pullup or pulldown resistors)? – Majenko Sep 5 '15 at 11:29
  • See my edit please. I am just using a Pushbutton with a resistor. I tested the button with different programs. It all works fine. – Kumar rajit Sep 5 '15 at 11:31
  • You haven't answered my question though. Is the resistor connected to +5V or to GND? It makes a big difference to the code snippet I will spoon feed you. – Majenko Sep 5 '15 at 11:40
  • The resistor is connected to Ground. Help me – Kumar rajit Sep 5 '15 at 13:17

I need a code to pause arduino code until a button is pressed.

I wrote a simple function to do this.




/* the function */
void buttonWait(int buttonPin){
  int buttonState = 0;
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {

void loop() {
  buttonWait(2); // wait for button press on pin 2
  // do something
  // ...

This code assumes that you have a pulldown resistor, but you can easily modify it to work with a pullup resistor instead, if you feel the need:


simulate this circuit – Schematic created using CircuitLab

See example code here!

  • Thank you rahuldottech!! I had been fussing with my code for several months on and off and tonight...I hit gold!! – Osprey Jun 23 '19 at 21:31
  • @Osprey That's great to hear! :) – undo Jun 24 '19 at 4:19
  • The battery doesn't make sense in that circuit. you have it wired to use +5V from VCC, which is correct, but why have a battery hooked to +5V? (Unless you are planning to bypass the internal regulator and the battery is really a regulated 5V supply?) – Duncan C Dec 24 '20 at 0:12

The construct you are looking for is called while.

Instead of doing "If the button is pressed then continue" you need to do "while the button is not pressed do nothing".

Assuming you have an "active low" button (that is, a resistor pulling it up to +5V when not pressed, so digitalRead(A2) == LOW is "pressed"), you can do:

while (digitalRead(A2) == HIGH) {
  // Do nothing

So while it's not pressed (digitalRead(A2) == HIGH) don't do anything. As soon as digitalRead(A2) returns LOW (so it is not == HIGH any more) the while loop will finish and your sketch can continue.

I write all about the while loop in an article here.

  • will it continue right from where it has stopped..? Lets say if my motor is at 67 degrees it should resume from the same. Is it doing the same..? – Sonali_B Nov 8 '17 at 7:42

Here are two ways you might have wired the button. If you wired it with a pullup, you will need to test for LOW (the button will ground its output when pressed). If pulldown, the button will raise its output when pressed and you would test for HIGH.

If you will be testing the button again within less than a second, don't forget to debounce it. Switches send a bunch (technical term :) of pulses for a significant fraction of a second during a make or bread. See Nick Gammon's "Switches" page for a thorough discussion.

Pullup vs. Pulldown Schematic


I also put an high value resistor in parallel to the switch (for e. 1MOhm). In this case you have a voltage divider and pin input isn't floating.

Your Answer

