Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 37 ยท Serial and Communication

The Serial Monitor and Printing Data

Use Serial on Arduino: baud rates, Serial.print and println, number formats, the Serial Plotter, reading incoming bytes, and how long data takes to send.

The serial port is your window into a running sketch. With one line you can print a sensor value, a state name or an error message to your computer, and with another you can send commands back. It is also the oldest and simplest way two chips talk to each other: the USB connection on an Uno is a serial link (UART) behind a USB converter, and the same protocol connects GPS modules, Bluetooth modules and other boards.

The Serial Monitor needs a real board, which this site cannot run. The sketches are read-only and show real syntax; the C programs are labelled stand-ins that reproduce the formatting rules and the timing math, using printf where Arduino uses Serial.print.

Starting the port

C++
void setup() {
  Serial.begin(9600);      // open the port at 9600 baud
}

void loop() {
  Serial.println("Hello from Arduino");
  delay(1000);
}

Serial.begin(speed) sets the baud rate, roughly bits per second. The number in your sketch and the dropdown at the bottom of the Serial Monitor must match, or you will see garbage characters. Common speeds are 9600 (the default in most examples), 115200 (fast, popular for logs) and 250000 (used by 3D printers).

On boards with native USB (Leonardo, Micro, many ARM boards), the port may not be ready right after reset. The common idiom is while (!Serial) { } after begin, which waits for the monitor. Do not use it on an Uno where it is not needed, and never in a battery project that must run without a computer attached.

Serial.print() sends text without a line ending; Serial.println() adds carriage return and line feed (\r\n). They accept strings, characters, integers and floats, and you can give a second argument for the format:

C++
void setup() {
  Serial.begin(115200);

  int value = 42;
  Serial.println(value);            // 42
  Serial.println(value, BIN);       // 101010
  Serial.println(value, HEX);       // 2A
  Serial.println(value, OCT);       // 52
  Serial.println(3.14159, 2);       // 3.14  (2 decimal places)
  Serial.println('A');              // A
  Serial.println((int)'A');         // 65
  Serial.println(F("Stored in flash"));
}

void loop() { }

Remember the F() macro: it keeps string literals in flash memory instead of copying them to the scarce RAM (the Uno has only 2 KB). A sketch with a lot of messages can run out of RAM unless you wrap them (details in debugging and memory).

Arduino has no printf for floats on AVR boards, but you can format with snprintf for integers or build the line from several print calls. Printing comma-separated values makes a CSV that spreadsheets import directly:

C++
Serial.print(millis());
Serial.print(',');
Serial.print(temperature, 1);
Serial.print(',');
Serial.println(humidity, 1);
C Stand-in: number formats like Serial.print (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The Serial Plotter

Instead of the Monitor, open Tools, Serial Plotter in the IDE: it draws a live graph of the numbers you print. Print one value per line for a single trace, or several values separated by spaces or commas (optionally labelled name:value) for several traces. It is excellent for tuning filters, watching an analog input or comparing a raw and a smoothed signal.

C++
Serial.print("raw:");
Serial.print(raw);
Serial.print(",smooth:");
Serial.println(smooth);

How long does sending take?

Serial data is sent as frames. With the default 8N1 setting each byte needs a start bit, 8 data bits and a stop bit: 10 bits per byte. At 9600 baud that is 960 bytes per second, about 1.04 ms per byte. Printing a 40-character line costs roughly 42 ms, during which a blocking sketch is mostly waiting. Actually the hardware has a 64-byte transmit buffer on AVR boards, so short prints return instantly, but once the buffer is full, print blocks until there is room.

Two lessons follow. Raising the baud rate (115200) reduces the time your sketch loses to printing. And printing from every pass of a fast loop can slow the whole sketch; print less often or only on change.

C Stand-in: transmission time at different baud rates (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Reading data back

Serial.available() tells how many bytes are waiting in the receive buffer (64 bytes on AVR); Serial.read() returns one byte, or -1 if none. Always check available() first:

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

void loop() {
  if (Serial.available() > 0) {
    char c = Serial.read();
    if (c == '1') {
      digitalWrite(LED_BUILTIN, HIGH);
    } else if (c == '0') {
      digitalWrite(LED_BUILTIN, LOW);
    }
  }
}

Other helpers: Serial.parseInt() reads digits into a number (it waits up to a timeout, 1 second by default), Serial.readStringUntil('\n') reads a whole line into a String, and Serial.setTimeout(ms) changes the wait. For anything beyond one-character commands, build a proper line parser; the next lesson shows how in a serial command parser.

Practical tips

  • Debug by printing, but remove or reduce prints in production code.
  • Use one Serial per UART: an Uno has one (shared with USB on pins 0 and 1, so do not connect other devices to those pins while uploading). A Mega has Serial1 to Serial3.
  • Add a unit and a label to each printed value, and use consistent separators.
  • Printing floats costs time and code space; print integers (for example tenths of degrees) when you can.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You see garbage characters in the Serial Monitor. What is the most likely cause?

    Both ends must use the same baud rate. Mismatched rates produce nonsense characters.
  2. 2. How many bits does one byte take on the wire with the default 8N1 settings?

    One start bit, eight data bits and one stop bit make 10 bits per byte.
  3. 3. What does Serial.println(42, HEX) print?

    The second argument selects the base: HEX prints 42 as 2A.
  4. 4. Why wrap string literals in F()?

    On AVR boards string literals are copied to RAM at startup. F() leaves them in flash memory and reads them as needed.

Summary

  • Serial.begin(baud) opens the port; the monitor must use the same rate.
  • print and println support formats (BIN, HEX, decimal places); F() saves RAM.
  • The Serial Plotter graphs printed numbers; each byte costs 10 bit-times at 8N1.
  • Read input with available() and read(), never blindly.

Next: Writing a Serial Command Parser