I need a code to pause arduino code untill a button is pressed. I have a long code 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" and condition but it failed for my purpose, because when "IF" detects that the button is not pressed, it moves to next lines (as usual). But i have long code and I want to wait it forever untill 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

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: http://hacking.majenko.co.uk/the-while-loop

  • 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 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

New contributor
rahuldottech is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.