📡 Serial Communication (Serial Monitor)
🎯 Lesson Objective
In this lesson, students will understand:
• What serial communication is
• Why serial communication is important in robotics programming
• How to send data from the ESP32 to a computer
• How to use the Serial Monitor in Arduino IDE
• How serial communication helps in debugging programs
This lesson introduces serial communication, which allows the microcontroller to communicate with the computer and display information.
1️⃣ What is Serial Communication?
Serial communication is a method used to transfer data between two devices one bit at a time through a communication channel.
In microcontroller programming, serial communication is commonly used to exchange data between:
💻 A computer
🧠 A microcontroller (ESP32)
📡 Other electronic modules
This allows the microcontroller to send information such as sensor readings or system messages to the computer.
The computer can display this information using the Serial Monitor in the Arduino IDE.
2️⃣ Why Serial Communication is Important
Serial communication is extremely useful when developing robotics systems.
It allows developers to:
🔎 View sensor data in real time
🛠 Debug programs and detect errors
📊 Monitor system behavior
⚙️ Test hardware components
Without serial communication, it would be difficult to know what is happening inside the program while the robot is running.
For example, we can display the distance measured by an ultrasonic sensor on the screen.
3️⃣ Starting Serial Communication
Before sending data to the Serial Monitor, serial communication must be initialized.
This is done inside the setup() function using the Serial.begin() command.
Example:
Serial.begin(9600);• Serial.begin() starts serial communication.
• 9600 is the communication speed called baud rate.
Both the microcontroller and the Serial Monitor must use the same baud rate.
4️⃣ Printing Data to the Serial Monitor
Once serial communication has started, the program can send data to the Serial Monitor.
Two commonly used functions are:
• Serial.print()• Serial.println()
Serial.print()
This function prints data without moving to the next line.
Example:
Serial.print("Distance: ");Serial.println()
This function prints data and moves the cursor to the next line.
Example:
Serial.println(distance);5️⃣ Example Program
Example program that prints a message repeatedly:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Robot Running...");
delay(1000);
}
Explanation:
• Serial communication starts in the setup() function.
• The message “Robot Running…” is printed every second.
• The Serial Monitor displays the message continuously.
6️⃣ Opening the Serial Monitor
To view the data sent from the ESP32:
1️⃣ Upload the program to the ESP32.
2️⃣ Open Arduino IDE.
3️⃣ Click Tools → Serial Monitor.
4️⃣ Set the baud rate to 9600.
The Serial Monitor will now display messages sent by the program.
7️⃣ Serial Communication in Robotics Projects
Serial communication is used in many robotics applications.
Examples include:
📏 Displaying sensor readings
📡 Monitoring robot behavior
⚙️ Checking motor control signals
🔧 Debugging program errors
For example, during development we may display:
• Distance values from ultrasonic sensors
• Light intensity values from LDR sensors
• System status messages
This helps developers verify that the robot is functioning correctly.
8️⃣ Serial Communication for Debugging
One of the most important uses of serial communication is debugging.
Debugging means identifying and fixing problems in a program.
By printing values to the Serial Monitor, developers can understand:
• Whether sensors are working correctly
• Whether conditions are evaluated correctly
• Whether the program logic is functioning as expected
Serial communication is therefore a powerful tool for testing and improving robotics programs.
🚀 What Happens Next
Now that you understand how serial communication allows the microcontroller to display data and communicate with the computer, the next step is to learn how to read sensor data inside programs.