Learn Arduino Tutorials Open the 3D lab

Beginner · 5 min read

Arduino Serial input — read numbers and commands from the keyboard

Until now the Arduino only talked to you. Serial input lets you talk back: type a number or a word in the Serial Monitor and your sketch reacts. Here is how to read it safely, without the classic "why do I get a 0?" surprise.

An Arduino UNO board connected by USB, the link used to send typed numbers and commands from the computer to the sketch
Build it yourself in 3DFree, in your browser — no board, no wires, nothing to buy.Open the 3D lab →

The short answer

Check that something arrived with Serial.available(), then read it:

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

void loop() {
  if (Serial.available() > 0) {        // is anything waiting?
    int number = Serial.parseInt();    // read it as a whole number
    Serial.print("You typed ");
    Serial.println(number);
  }
}

Open the Serial Monitor, type 42 and press Enter. The board answers You typed 42. If you have not used the monitor yet, start with the Serial Monitor guide.

How typed text arrives

Every key you press is sent as one byte (a character code) over the USB cable. The UNO stores arriving bytes in a small waiting line, the receive buffer, which holds 64 bytes. Your sketch takes them out one at a time.

FunctionWhat it does
Serial.available()how many bytes are waiting (0 if none)
Serial.read()takes one byte out; returns -1 if there is none
Serial.parseInt()reads digits and builds a whole number
Serial.parseFloat()the same, with decimals
Serial.readStringUntil('\n')reads text up to the Enter key, as a String

Typing 42 sends two bytes, the characters '4' and '2'. parseInt() turns them into the number 42 for you. If you used Serial.read() you would get the character codes 52 and 50 instead. See variables and data types for why a character is just a number.

Everything you type travels over this USB cable, one byte at a time
Everything you type travels over this USB cable, one byte at a time · open it full screen

Example: a dimmer you type into

Connect an LED with a 220 Ω resistor to pin 9, a PWM pin (see PWM and analogWrite). Type a number from 0 to 255:

const int LED = 9;

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  Serial.println("Type a brightness from 0 to 255");
}

void loop() {
  if (Serial.available() > 0) {
    int level = Serial.parseInt();
    level = constrain(level, 0, 255);   // keep it in range
    analogWrite(LED, level);
    Serial.print("Brightness: ");
    Serial.println(level);
  }
}

constrain() stops a typo like 900 from wrapping around to a dark LED. In the Serial Monitor's line-ending menu, choose No line ending for this sketch (the next section says why).

Why you sometimes get a stray 0

parseInt() waits for digits. If nothing useful arrives within one second (the default timeout), it gives up and returns 0. That happens when the monitor sends a newline character after your number: the number is read, then the leftover newline starts a new parseInt() that finds no digits and returns 0 a second later.

Three fixes:

  • Set the monitor to No line ending, as above.
  • Ignore zero results you did not expect (only fine if 0 is not a valid input).
  • Read the whole line instead, as shown next.

You can also change the wait with Serial.setTimeout(50); (in milliseconds) so the sketch does not freeze for a second.

Reading words: commands

To accept commands such as on or off, read a line of text and compare it:

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  if (Serial.available() > 0) {
    String command = Serial.readStringUntil('\n');
    command.trim();                       // remove spaces and the \r some systems add
    if (command == "on") {
      digitalWrite(LED_BUILTIN, HIGH);
    } else if (command == "off") {
      digitalWrite(LED_BUILTIN, LOW);
    } else {
      Serial.println("Unknown command");
    }
  }
}

Here the monitor must send Newline, because the sketch waits for the '\n' that ends the line. For a single letter, switch is neater: see switch and case.

Common mistakes

  • Baud rate mismatch. Serial.begin(9600) in the sketch must match the speed in the monitor, or you see garbage.
  • Reading without checking available(). Serial.read() returns -1 when nothing is waiting, and -1 looks like a real value.
  • Expecting Serial.read() to return the number you typed. It returns a character code: typing 5 gives 53. Use parseInt() or subtract '0'.
  • Using delay(1000) in the loop. Typed bytes pile up in the 64-byte buffer, and the sketch feels slow. Read often.
  • Forgetting that opening the monitor resets an UNO. The board restarts each time the monitor connects, so setup messages print again.

Check yourself

  1. What does Serial.read() return when nothing has been typed?

  2. You type 7 and read it with Serial.read(). What number do you get?

  3. Why can parseInt() return 0 a second after you typed a number?

Questions people ask

How do I read a decimal number?

Use Serial.parseFloat(). It reads digits with a decimal point, for example 3.14. The same timeout rule applies.

Can I send several numbers at once?

Yes. Call parseInt() once for each, and separate the numbers with spaces or commas when you type: 255,128,0 gives three calls, one per colour of an RGB LED.

Can the Arduino read serial data without a computer?

Yes: pins 0 (RX) and 1 (TX) can talk to other devices such as a Bluetooth module. They are shared with the USB link, so do not use them for anything else while the USB cable is in use.

How fast can I send data?

At 9600 baud about 960 characters per second. Faster speeds like 115200 work for most boards; set the same number in Serial.begin() and in the monitor.

What's next

Try the dimmer, then add a second command to control a servo. Next, learn how to save a value so it survives power-off in EEPROM.

Build it yourself in 3DFree, in your browser — no board, no wires, nothing to buy.Open the 3D lab →
Next: Arduino EEPROM — save data that survives power off →

Your progress stays on this device — no account, nothing sent anywhere.

Last updated 2026-12-01

Read next