Arduino Serial Communication
Arduino serial communication (UART) allows the board to send and receive data with a computer or other serial devices.
Difficulty: Beginner
Created: December 2022
Technically reviewed: August 2026
Reference platform: Arduino Uno family / general Arduino API
In the first ten lessons, we often used the Serial Monitor:
Serial.begin(9600);
Serial.println(value);
Until now, it mainly allowed us to see the variables and results produced by the program.
Serial, however, is not simply an output or debugging tool. It provides a communication channel between the Arduino and another device.
We can send data:
Arduino → computer
and receive it as well:
computer → Arduino
At the end of this lesson, we will send text commands from the Serial Monitor:
ON
OFF
STATUS
The Arduino:
receives the command
↓
interprets it
↓
makes a decision
↓
controls the built-in LED
↓
sends a response to the computer
This will be our first simple application protocol.
Required equipment
- a board from the Arduino Uno family;
- a data-capable USB cable;
- a computer running Arduino IDE 2.
The practical examples use LED_BUILTIN, so no separate LED or resistor is required.
What does serial communication mean?
In serial communication, information travels through the communication channel as a sequence of bits.
A byte normally consists of eight bits. A byte can represent, for example:
- a numeric value;
- a character code;
- part of a larger message;
- binary data.
“Serial” does not generally mean that every connection uses exactly one wire.
A UART connection, for example, can use separate signal paths:
- for transmitting data;
- for receiving data.
A common electrical reference, or common GND, is normally required as well.
UART, USB, Serial and Serial Monitor
These concepts are related, but they are not identical:
- UART: a hardware serial communication peripheral;
- TX/RX: the signal paths used for transmission and reception;
- Serial: the communication object used in the Arduino program;
- USB: the physical and protocol layer connected to the computer;
- Serial Monitor: the Arduino IDE interface used to send and receive data.
On some boards, the computer’s USB connection reaches the main microcontroller UART through an onboard USB-to-UART converter. Other boards use a virtual USB serial connection.
It is therefore not true for every Arduino board that:
Serial = always pins 0/RX and 1/TX
Always check the documentation for the particular board.
A brief note about the Uno family
On the Uno R3, the hardware UART used by Serial is connected to pins 0/RX and 1/TX and to the onboard 16U2 USB-to-serial interface.
On the Uno R4, the virtual USB connection to the computer is Serial, while the exposed hardware UART is normally Serial1.
The main examples in this lesson use the computer’s Serial Monitor and therefore use the Serial object.
TX and RX
The abbreviations mean:
TX = transmit
RX = receive
For a direct UART connection between two devices, the signal paths are normally crossed:
Device A Device B
TX --------------------> RX
RX <-------------------- TX
GND ------------------- GND
Important: this is only a conceptual wiring diagram. Before connecting the devices, check:
- the logic voltage levels used by both devices;
- the permitted input voltages;
- the pinout of the particular board;
- whether level shifting is required.
Matching supply voltages alone do not prove that the UART signals are compatible.

Starting communication
We already know this statement from the previous lessons:
Serial.begin(9600);
begin() prepares the serial connection.
With a hardware UART, 9600 configures the communication rate. The two UART devices must use compatible settings.
Everyday Arduino terminology calls this the baud rate. With a simple UART configuration, 9600 indicates the approximate number of transmitted bits per second, not “9600 characters per second.”
With a virtual USB serial connection, 9600 does not necessarily have the same physical line-speed meaning. We still use it in the examples because it is a common and familiar Arduino pattern.
The Arduino IDE 2 Serial Monitor
In the Serial Monitor, we can:
- view data sent by the Arduino;
- send text to the Arduino;
- select the communication rate;
- choose which line ending is appended to the transmitted text.
In the current IDE 2 interface, the line-ending options are normally:
- No line ending: no line ending is appended;
- Newline:
'\n'; - Carriage return:
'\r'; - Both NL & CR: both line-ending characters.
For the main ON/OFF/STATUS program, select:
Newline
The program also ignores '\r', so it can be used with the “Both NL & CR” setting as well.

print() and println()
Serial.print() does not start a new line automatically:
void setup()
{
Serial.begin(9600);
Serial.print("Hello");
Serial.print(" Arduino");
}
void loop()
{
}
The result appears on one line:
Hello Arduino
Serial.println() also sends a line ending after the data:
void setup()
{
Serial.begin(9600);
Serial.println("Hello");
Serial.println("Arduino");
}
void loop()
{
}
The result is:
Hello
Arduino
The line ending sent by Arduino println() consists of two bytes:
'\r'
'\n'
They are called:
- CR – carriage return;
- LF or NL – line feed/newline.
In beginner programs, it is sufficient to think of them as the end of a line. When receiving data, however, we need to know that the Serial Monitor may send one or two separate line-ending characters.
Formatted output of numbers
print() sends a human-readable textual representation of a number:
void setup()
{
Serial.begin(9600);
Serial.println(29);
Serial.println(29, BIN);
Serial.println(29, OCT);
Serial.println(29, DEC);
Serial.println(29, HEX);
}
void loop()
{
}
The expected result is:
29
11101
35
29
1D
For a floating-point number, we can specify the number of decimal places:
Serial.println(1.23456, 3);
The result is:
1.235
print() and write()
print() produces a textual representation.
write() sends a raw byte or byte sequence.
void setup()
{
Serial.begin(9600);
Serial.print("Serial.print(65): ");
Serial.println(65);
Serial.print("Serial.write(65): ");
Serial.write(65);
Serial.println();
}
void loop()
{
}
In the Serial Monitor:
Serial.print(65): 65
Serial.write(65): A
The difference is that:
Serial.print(65);
sends two textual digits:
'6', '5'
In contrast:
Serial.write(65);
sends one byte with the value 65. In ASCII-compatible character encoding, it appears as the character A.
write() does not generally “write a character.” It transmits a raw byte value, which the receiving side interprets according to its own rules.
The receive buffer
The serial system may temporarily store incoming data in a receive buffer.
This is useful because the Arduino program does not necessarily process a byte at the exact moment when it physically arrives.
The buffer capacity is implementation-dependent. This lesson does not rely on a particular buffer size.
If the program reads the data too slowly, the buffer may fill and additional data may be lost.
How many bytes have arrived?
Serial.available() reports how many bytes can currently be read:
if (Serial.available() > 0)
{
// At least one byte is available.
}
This does not mean that a complete word or message has necessarily arrived. It only reports the number of bytes currently available.
Reading one byte
Serial.read() removes and returns one byte from the receive buffer:
if (Serial.available() > 0)
{
int incomingByte = Serial.read();
}
Using the int type is important because read() can return:
- a byte value from
0to255; - or
-1when no data is available.
Because the example checks available() first, a successful read is expected.
To use the byte as a character:
if (Serial.available() > 0)
{
int incomingByte = Serial.read();
if (incomingByte >= 0)
{
char incomingChar =
static_cast<char>(incomingByte);
Serial.println(incomingChar);
}
}
One read() call removes one byte. It does not automatically empty the entire receive buffer.
First interactive program: single-character LED control
The following program:
- turns on the built-in LED when it receives
'1'; - turns it off when it receives
'0'; - sends feedback to the Serial Monitor.
For this example, set the Serial Monitor line ending to No line ending so that no separate '\r' or '\n' character is sent.
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Serial.begin(9600);
Serial.println("Send: 1 or 0");
}
void loop()
{
if (Serial.available() > 0)
{
int incomingByte = Serial.read();
if (incomingByte == '1')
{
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("LED: ON");
}
else if (incomingByte == '0')
{
digitalWrite(LED_BUILTIN, LOW);
Serial.println("LED: OFF");
}
else
{
Serial.println("Unknown character");
}
}
}
The communication flow is:
Serial Monitor
│
│ '1'
▼
Arduino
│
│ checks the condition
▼
LED turns on
│
│ "LED: ON"
▼
Serial Monitor
Why does one read() not read a complete word?
If we send:
ON
the data consists of multiple bytes:
'O'
'N'
If the Serial Monitor also appends a newline:
'O'
'N'
'\n'
Each Serial.read() call reads only one of these bytes.
For a multi-character command, we therefore:
- receive the characters one after another;
- collect them in a String or character buffer;
- use the line ending to identify the end of the message;
- then interpret the complete command.
A simple text protocol
We will use the following commands:
ON
OFF
STATUS
The communicating participants agree that:
- one command arrives on each line;
'\n'marks the end of the command;'\r'is ignored;- a command can contain no more than 16 characters;
- the Arduino sends a response for every processed command.
This is already a very simple application protocol.
Mini-project: ON/OFF/STATUS LED control
Set the Serial Monitor line ending to:
Newline
“Both NL & CR” can also be used because the program discards '\r'.
const size_t maxCommandLength = 16;
String command;
bool commandTooLong = false;
bool ledIsOn = false;
void processCommand()
{
command.trim();
if (command.length() == 0)
{
return;
}
if (command == "ON")
{
ledIsOn = true;
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("LED: ON");
}
else if (command == "OFF")
{
ledIsOn = false;
digitalWrite(LED_BUILTIN, LOW);
Serial.println("LED: OFF");
}
else if (command == "STATUS")
{
if (ledIsOn)
{
Serial.println("LED: ON");
}
else
{
Serial.println("LED: OFF");
}
}
else
{
Serial.print("Unknown command: ");
Serial.println(command);
}
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Serial.begin(9600);
Serial.println("Commands: ON, OFF, STATUS");
}
void loop()
{
while (Serial.available() > 0)
{
int incomingByte = Serial.read();
if (incomingByte < 0)
{
continue;
}
char incomingChar =
static_cast<char>(incomingByte);
if (incomingChar == '\r')
{
continue;
}
if (incomingChar == '\n')
{
if (commandTooLong)
{
Serial.println(
"Error: command is too long.");
commandTooLong = false;
command = "";
continue;
}
processCommand();
command = "";
continue;
}
if (commandTooLong)
{
continue;
}
if (command.length() < maxCommandLength)
{
command += incomingChar;
}
else
{
commandTooLong = true;
command = "";
}
}
}
How the program works
The global command String is required because the characters of a command may not all arrive during a single execution of loop().
The program processes every available byte:
while (Serial.available() > 0)
It discards '\r':
if (incomingChar == '\r')
{
continue;
}
'\n' terminates the command:
if (incomingChar == '\n')
processCommand() then interprets the completed text.
The String cannot grow without limit:
if (command.length() < maxCommandLength)
If the input is too long, the program:
- does not store any more characters;
- discards the remaining input until the next line ending;
- sends an error message;
- waits for the next command from a clean state.
This is better than allowing a String to grow without limit because invalid or continuous input cannot consume an increasing amount of memory.
The processing does not wait idly for a new character. When no data is available, loop() can continue running and perform other tasks later.
Convenient but waiting Stream operations
The Arduino Stream API also provides convenient functions:
Serial.readString();
Serial.readStringUntil('\n');
Serial.parseInt();
Serial.parseFloat();
These functions can be useful, but when data or a terminating character is missing, they may wait until the configured timeout expires.
The default timeout in many cores is 1000 ms:
Serial.setTimeout(1000);
Other program tasks may be delayed while the function is waiting.
This does not mean that these functions should never be used. They can be convenient in simple programs that are not timing-sensitive. If the program must also handle an LED, motor, communication channel or another time-sensitive task, however, the blocking wait must be considered deliberately.
The main mini-project therefore processes the available data one character at a time.
What does Serial.flush() do?
Serial.flush() does not discard incoming data.
In modern Arduino hardware Serial implementations, it normally waits until previously submitted outgoing data has finished transmitting:
Serial.println("Done");
Serial.flush();
This can be necessary, for example, when the connection will be disabled or the hardware will be switched to another state immediately after transmission.
Most simple programs do not need to call flush() after every print().
Physical UART connection to another device
The Serial Monitor is only one possible communication partner.
Using UART, an Arduino can communicate with, for example:
- another microcontroller;
- a sensor module;
- a communication module;
- a USB-to-serial adapter.
Before making the physical connection, check:
- which Serial object belongs to the exposed UART;
- which pins are TX and RX;
- which logic levels the two devices use;
- whether level shifting is required;
- whether the devices share a common GND reference.
On the Uno R3, using pins 0 and 1 can interfere with uploading and with the serial connection to the computer.
On the Uno R4, the computer-facing Serial channel and the physical Serial1 UART can be separate channels.
Not every Arduino board provides a Serial1 object, so the documentation for the particular board is always authoritative.
Common mistakes
Serial.begin() is missing
void setup()
{
// Missing:
// Serial.begin(9600);
}
Using incompatible communication settings
With a hardware UART connection, the communicating devices must use compatible data rates and frame settings.
Confusing print() and write()
Serial.print(65);
sends the text 65.
Serial.write(65);
sends one byte with the value 65.
Calling read() without checking
A correct basic pattern is:
if (Serial.available() > 0)
{
int incomingByte = Serial.read();
}
Expecting one read() call to return a complete word
read() reads one byte at a time. A multi-character message must be collected.
Ignoring line endings
Depending on the IDE setting, '\r', '\n' or both may arrive. These are real incoming bytes as well.
Allowing the input String to grow without limit
Set a maximum length for incoming messages and handle input that is too long.
Waiting only for data to arrive
This pattern prevents the program’s other tasks from progressing:
while (Serial.available() == 0)
{
// Only waiting.
}
In this introductory lesson, we instead check for available data regularly and read only when something is available.
Trying to clear the input with flush()
Serial.flush() is not a general-purpose receive-buffer clearing operation.
Connecting TX to TX
For two UART devices, the normal arrangement is:
TX → RX
RX ← TX
No common GND
A direct electrical UART connection normally requires a common reference between the communicating devices.
Not checking logic voltage levels
Incompatible logic levels can cause incorrect operation or hardware damage.
Assuming that Serial and Serial1 mean the same thing on every board
The available objects and physical signal paths depend on the board and its core.
Summary
In this lesson, we learned that:
Serialis not simply debugging output, but a communication channel;- serial communication transmits data as a sequence of bits and bytes;
- UART normally uses separate TX and RX signal paths;
- the TX and RX paths are normally crossed in a UART connection between two devices;
- a common GND and compatible logic levels are required;
- USB, UART, Serial and Serial Monitor are not identical concepts;
- the meaning of
SerialandSerial1depends on the board; Serial.begin()prepares the connection;print()sends a textual representation;println()also appends a line ending;write()sends a raw byte or byte sequence;available()reports how many bytes can be read;read()removes one byte;read()returns anintso that it can also represent-1;- a multi-character message must be collected;
'\n'can mark the end of a message;- the length of an incoming String must be limited;
- a simple ON/OFF/STATUS command set is already an application protocol;
- some convenient Stream functions can block until their timeout expires;
Serial.flush()waits for outgoing transmission to finish and does not clear incoming data.
The complete communication flow is:
Serial Monitor
↓
ON / OFF / STATUS
↓
Arduino receives the bytes
↓
assembles the command
↓
interprets it
↓
controls the LED
↓
sends a response
↓
Serial Monitor
🧪 Try it!
1. print() and println()
Write a program that assembles the following single line using several print() calls:
Arduino Communication: Part 1
Then use println() to start a new line.
2. print() and write()
Send the value 65 using:
Serial.print(65);
Serial.write(65);
Explain in your own words why one case displays 65 and the other displays A.
3. Single-character LED control
Create a program that:
- turns on
LED_BUILTINwhen it receives'1'; - turns it off when it receives
'0'; - sends an error message for every other character.
Use the No line ending setting in the Serial Monitor.
4. A new command
Extend the ON/OFF/STATUS mini-project with a new command:
HELP
The response should list the available commands:
ON, OFF, STATUS, HELP
