HOWTO · Arduino

How to Wait for Input in Arduino

Choose serial, digital, or analog input in Arduino and learn how polling waits affect the rest of your sketch.

On this page

Choose the input source that matches the signal: use serial input for bytes sent by a computer or another device, digitalRead() for an on/off pin such as a button, and analogRead() to compare a sensor reading with a threshold. The Arduino sketches below use the standard Arduino API. The accompanying portable C++ harnesses provide deterministic stand-ins for board hardware so their input-handling logic can be compiled and followed without an Arduino board; they do not test electrical behavior.

Choose the input source: serial, digital, or analog

Use serial when a host sends characters or commands over USB or a UART connection. Use a digital pin when the signal has two states. Use an analog input when the sensor produces a changing voltage that you need to sample and compare. Pick the input pin and electrical setup for your board and sensor; the examples use pin 2 for a button and A0 for an analog sensor.

Wait for serial data with Serial.available()

Serial.available() reports how many bytes are waiting in the receive buffer. Check it before calling Serial.read() so the sketch does not read when no byte is ready. This example consumes and echoes one byte at a time, which is suitable for a simple character command. It does not assemble a full line; for line-based commands, collect bytes until a newline and handle a line terminator explicitly.

Run this portable harness to see the byte-handling pattern. In an Arduino sketch, keep setup() and loop() and use the board’s Serial object in place of the small stand-in shown here.

#include <iostream>

class SerialPort {
 public:
  void begin(int) {}
  int available() const { return next_ < 1; }
  int read() { ++next_; return 65; }
  void print(const char* text) { std::cout << text; }
  void println(int value) { std::cout << value << '\n'; }

 private:
  int next_ = 0;
};

SerialPort Serial;

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    int incomingByte = Serial.read();
    Serial.print("Received byte: ");
    Serial.println(incomingByte);
  }
}

int main() {
  setup();
  loop();
}
Received byte: 65

Set the serial monitor or sending device to the same baud rate. Since Serial.read() returns an integer byte value, the example prints a labeled numeric byte report — the Received byte: label followed by the byte’s numeric value; cast it to char when you want to print the character itself.

Wait for a digital input with digitalRead()

A digital input is read from its pin, not from the serial port. For a button wired between pin 2 and ground, configure INPUT_PULLUP: the unpressed state reads HIGH, and a pressed button reads LOW. The loop below waits for a press and then for release. This polling wait pauses other work in loop() while either condition is being waited on.

This portable harness supplies a sequence of pin states to the same wait logic. In the Arduino sketch, replace the stand-in functions with the board’s pinMode() and digitalRead() APIs.

#include <iostream>

const int INPUT_PULLUP = 2;
const int HIGH = 1;
const int LOW = 0;
const int buttonPin = 2;
int pinStates[] = {HIGH, LOW, LOW, HIGH};
int stateIndex = 0;

void pinMode(int, int) {}
int digitalRead(int) { return pinStates[stateIndex++]; }

class SerialPort {
 public:
  void begin(int) {}
  void println(const char* text) { std::cout << text << '\n'; }
};

SerialPort Serial;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  while (digitalRead(buttonPin) == HIGH) {
  }

  Serial.println("Button pressed");

  while (digitalRead(buttonPin) == LOW) {
  }
}

int main() {
  setup();
  loop();
}
Button pressed

The internal pull-up keeps the input at a defined level when the button is open. If your circuit instead wires a button to a positive supply and uses an external pull-down resistor, use INPUT and check for HIGH when pressed. Match the code’s polarity to the circuit to avoid a floating input or an inverted response.

Wait for an analog threshold with analogRead()

analogRead() samples the voltage on an analog pin and returns a numeric reading. Compare that reading with a threshold chosen for your sensor and board. This sketch reports when a sampled reading is above the threshold, including on its first pass if that is already true. After reporting, it waits until a later sample is at or below the threshold before it can report again.

This portable harness supplies two readings: one above the threshold and the next at or below it. On the board, the Arduino core supplies A0 and analogRead(); the available pins and reading range depend on the board.

#include <iostream>

const int A0 = 0;
const int sensorPin = A0;
const int threshold = 500;
int readings[] = {600, 450};
int readingIndex = 0;

int analogRead(int) { return readings[readingIndex++]; }

class SerialPort {
 public:
  void begin(int) {}
  void println(const char* text) { std::cout << text << '\n'; }
};

SerialPort Serial;

void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(sensorPin);

  if (sensorValue > threshold) {
    Serial.println("Threshold exceeded");

    while (analogRead(sensorPin) > threshold) {
    }
  }
}

int main() {
  setup();
  loop();
}
Threshold exceeded

The numeric range and voltage represented by analogRead() depend on the board. Check its documentation and calibrate threshold against the sensor’s readings. This is repeated sampling and comparison, not a wait for serial bytes.

Avoid blocking the rest of the sketch

A tight while loop keeps checking the input but prevents the rest of loop() from running until its condition changes. That is useful when the sketch should stop at that point, but it delays other tasks such as updating a display or checking another input. For a sketch that must keep working, check the input once per pass and remember the state instead of waiting inside a loop. For example, use if (Serial.available() > 0) to process available bytes and continue with the rest of loop().

If the sketch appears unresponsive, check that the selected pin matches the wiring, that the digital input has a defined level, that the serial sender uses the configured baud rate, and that the analog threshold matches the board’s reading range. A blocking wait also will not finish until the tested state changes.