What Is Serial Communication?
Serial communication sends data one bit at a time over a single wire, and it is how an Arduino talks to your computer over the USB cable. The Serial library lets your sketch print sensor readings to the Serial Monitor for debugging and receive commands typed back. Because the board has no screen of its own, Serial is your primary window into what a running sketch is actually doing — an indispensable debugging tool.
Cricket analogy: Serial is like a running commentary feed: the sketch narrates each event ball by ball to the Serial Monitor, giving you the only live view of a match happening out of sight.
Setting Up Serial with begin()
Before using Serial you must call Serial.begin(baudRate) inside setup(), where the baud rate is the communication speed in bits per second. 9600 is a common default, and 115200 is popular for faster output. The critical rule is that the baud rate set in your sketch must exactly match the baud rate selected in the Serial Monitor; a mismatch produces garbled characters instead of readable text.
Cricket analogy: The baud rate is like both teams agreeing on the over rate before play. If the bowler and scorer count at different speeds, the scorecard turns to nonsense — just like a baud mismatch garbling text.
void setup() {
Serial.begin(9600); // start serial at 9600 baud
}
void loop() {
int reading = analogRead(A0);
Serial.print("Sensor: "); // no newline
Serial.println(reading); // value + newline
// Echo back any command typed in the Serial Monitor
if (Serial.available() > 0) {
char c = Serial.read();
if (c == 'r') {
Serial.println("Reset requested");
}
}
delay(500);
}print, println, and Formatting
Serial.print() sends text or a value without a line break, while Serial.println() adds a carriage return and newline at the end so each entry appears on its own line. You can pass a second argument to control formatting: Serial.print(value, DEC) prints decimal, and HEX, OCT, or BIN print other bases. For floats, a second number sets the decimal places, so Serial.print(3.14159, 2) prints 3.14.
Cricket analogy: println is like ending a commentary line after each ball so every delivery gets its own entry, while print keeps stacking words on the same line, like reading the over as one run-on sentence.
Serial.println() by default sends both a carriage return (\r) and a newline (\n). When parsing incoming commands from another device, remember these line-ending characters may arrive too, so trim or account for them when comparing strings.
Reading Incoming Data
To receive data, check Serial.available(), which returns the number of bytes waiting in the input buffer. If it is greater than zero, call Serial.read() to take one byte, or Serial.readStringUntil('\n') to grab a whole line. Because data arrives one byte at a time and may lag behind, you typically read inside loop() and only act once a complete message has arrived, guarding each read with the available() check.
Cricket analogy: Serial.available() is checking how many balls are left to be bowled before you react: you wait until a full over of information has arrived rather than acting on a single ball out of context.
If your sketch prints faster than the receiver reads, or you never open the Serial Monitor, the output buffer fills and Serial.print() will block, stalling loop(). Also, adding heavy Serial printing inside a tight loop can slow a time-sensitive sketch noticeably, so remove or reduce debug prints in production.
- Serial sends data one bit at a time and is Arduino's main link to a computer over USB.
- Call Serial.begin(baud) in setup(); the sketch and Serial Monitor baud rates must match.
- Serial.print() omits a line break; Serial.println() adds a newline for one entry per line.
- A second argument formats output as DEC, HEX, OCT, BIN, or sets float decimal places.
- Serial.available() reports buffered incoming bytes; read only when it is greater than zero.
- Serial.read() takes one byte; readStringUntil('\n') grabs a full line.
- Heavy or blocked Serial output can stall or slow a sketch, so trim debug prints in production.
Practice what you learned
1. What must match between the sketch and the Serial Monitor?
2. What is the difference between Serial.print() and Serial.println()?
3. What does Serial.available() return?
4. How would you print 3.14159 to two decimal places?
5. Why should you reduce Serial printing in a production sketch?
Was this page helpful?
You May Also Like
Conditionals in Arduino
Learn how if, else if, else, and switch statements let an Arduino sketch make decisions based on sensor readings and pin states.
Functions in Arduino Sketches
Learn how to define, call, and return values from functions to organize Arduino code and avoid repetition.
Arrays in Arduino
Store and manage collections of related values using arrays, and combine them with loops to control multiple pins efficiently.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics