Lesson 75: Using Serial Communication
Learn how Serial.begin(), baud rate, print/println, and basic input reading work in Arduino.
Progress indicator
Lesson 75 of 75
Learning Objectives
- Understand what Serial communication is and why it is essential for debugging embedded systems.
- Use Serial.begin() correctly and match baud rate in both code and Serial Monitor.
- Understand print() vs println() and build clean, readable output lines.
- Use Serial.available() and Serial.read() safely for beginner command input handling.
- Avoid common mistakes like baud mismatch, missing checks, and noisy output loops.
Concept Explanation
What is Serial Communication
Serial communication is a method to send and receive data one bit at a time between your ESP32/Arduino board and a computer (or another device).
In beginner projects, Serial is your primary debugging window. Instead of guessing why something is wrong, you can print values such as sensor readings, button state, and decision results directly in Serial Monitor.
Serial.begin() Syntax
Use Serial.begin(baudRate) in setup() to start serial. Example: Serial.begin(115200).
- Write it once in
setup(), not repeatedly inloop(). - Common baud rates:
9600,57600,115200. - Choose one value and match that exact value in Serial Monitor.
How Serial Communication Works
- Your code writes bytes using
Serial.print()orSerial.println(). - UART hardware transmits bytes at the selected baud rate.
- Serial Monitor receives bytes and displays them as text.
- When you type in Serial Monitor, bytes return to the board input buffer.
- Your code reads those bytes and turns them into actions (for example, LED control).
Baud Rate Concept
Baud rate is the communication speed in bits per second. Both board and Serial Monitor must use the same value (for example, 9600 or 115200), otherwise output appears unreadable.
Higher baud rate means faster output, but printing too much data continuously can still slow your program because serial transmission takes time.
Serial.print() vs Serial.println()
print()writes text and keeps the cursor on the same line.println()writes text and then moves to a new line.- Common pattern: use multiple
print()calls for labels/values, then oneprintln()at the end of the line.
temp=27.4 | button=LOW | led=ONSerial.read() Basics
Serial.read() reads one incoming byte from the serial input buffer. It is often stored in a char variable for command-based control.
If you send the text 123, the board receives characters one-by-one:'1', then '2', then '3'.
Serial.available() Explained
Serial.available() returns how many bytes are waiting to be read. Always check this before calling Serial.read() to avoid invalid reads.
Think of it as: “Is there new input in the mailbox?” If yes, read it. If no, continue normal loop tasks.
When to Use Serial
- Debug variable values and code flow.
- Receive simple commands from a computer.
- Inspect sensor and state changes during testing.
- Create quick diagnostics before building complex UI or networking.
Example Code
This example reads serial commands and turns an LED ON/OFF.
const int LED_PIN = 2;
char incomingChar = '\0';
bool ledState = false;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
Serial.println("Serial ready. Send: '1' to turn LED ON, '0' to turn LED OFF.");
}
void loop() {
if (Serial.available() > 0) {
incomingChar = Serial.read();
if (incomingChar == '1') {
ledState = true;
digitalWrite(LED_PIN, HIGH);
Serial.println("LED is ON");
} else if (incomingChar == '0') {
ledState = false;
digitalWrite(LED_PIN, LOW);
Serial.println("LED is OFF");
} else {
Serial.print("Unknown input: ");
Serial.println(incomingChar);
}
}
delay(50);
}Example Code Explanation
pinMode(LED_PIN, OUTPUT)prepares the LED pin so it can be controlled by code.Serial.begin(115200)opens the serial channel between board and computer.- Startup message tells the user which commands are valid (
'1'and'0'). Serial.available() > 0protects your code from reading when buffer is empty.incomingChar = Serial.read()gets exactly one command byte.- If input is
'1', program setsledState = true, writesHIGH, and confirms via Serial text. - If input is
'0', program setsledState = false, writesLOW, and confirms via Serial text. - Unknown input is echoed back so the user can see what was actually received.
- Small
delay(50)reduces loop spam and keeps behavior stable for beginners.
What Happens Inside
- USB serial driver receives data from your computer terminal.
- Data bytes are placed into the board's serial RX buffer.
- Your loop checks buffer length via
Serial.available(). Serial.read()removes one byte from the buffer.- Your condition logic interprets that byte as a command and controls hardware.
- Outgoing status text is queued in TX buffer and sent back to Serial Monitor.
- Serial Monitor displays feedback so you can validate internal program state.
Common Mistakes with Serial
- Baud rate mismatch between code and Serial Monitor.
- Calling
Serial.read()without checkingSerial.available(). - Printing too frequently and slowing down loop execution.
- Comparing to numeric
1instead of character'1'. - Expecting full words from one
Serial.read()call.
Best Practices for Serial
- Use clear labels in output (for example:
temp=,state=). - Use
println()for readable line-by-line logs. - Use command characters (
'1','0','s') for simple control. - Keep message format consistent so logs are easy to scan during debugging.
- Throttle serial output frequency when running fast loops.
Try it now
Open the simulator workspace and step through serial input handling and LED command flow.