ESP32 — Serial Monitor and Debugging
Serial communication is one of the most useful tools when programming an ESP32.
When your project doesn't work, you often cannot see what the ESP32 is thinking.
The Serial Monitor lets you see information from the ESP32 while the program is running.
ESP32 │ │ USB ↓ Computer │ ↓ Serial Monitor │ ↓ You see messages, sensor values and errors
1. What is Serial Communication?
Serial communication means sending data one bit after another between devices.
For ESP32 development, a common example is:
ESP32 ↔ Computer
You can send:
Hello Temperature: 28 Motion detected WiFi connected Sensor value: 1234
2. Why Do We Need Serial Monitor?
Suppose your LED isn't turning ON.
Without Serial Monitor:
Why isn't it working? 🤔
With Serial Monitor:
PIR = 0 LDR = 3200 Button = 1 Relay = OFF
Now you can understand what your program is doing.
Serial Monitor is therefore one of your most important debugging tools.
3. Starting Serial Communication
In setup():
Serial.begin(115200);
Here:
115200 ↓ Baud rate
A common ESP32 setting is:
Serial.begin(115200);
4. Serial.print()
Used to print information without moving to a new line.
Serial.print("Hello"); Serial.print(" ESP32");
Output:
Hello ESP32
5. Serial.println()
println() prints and then moves to the next line.
Serial.println("Hello"); Serial.println("ESP32");
Output:
Hello ESP32
6. Serial.print() vs Serial.println()
Serial.print() ↓ Same line Serial.println() ↓ New line
Example:
Serial.print("Temperature: "); Serial.println(28);
Output:
Temperature: 28
7. Basic ESP32 Serial Program
void setup() { Serial.begin(115200); Serial.println("ESP32 Started!"); } void loop() { Serial.println("ESP32 is running..."); delay(1000); }
Serial Monitor:
ESP32 Started! ESP32 is running... ESP32 is running... ESP32 is running...
8. Baud Rate
The baud rate determines the communication speed.
Common values:
9600 19200 38400 57600 115200
For ESP32 development, you will commonly see:
Serial.begin(115200);
The Serial Monitor must use the same baud rate.
For example:
Code: 115200 Serial Monitor: 115200
✅ Correct.
If your code uses:
Serial.begin(115200);
but Serial Monitor is set to:
9600
you may see:
������
or other unreadable characters.
9. Serial Monitor in Arduino IDE
After uploading your ESP32 program:
- Open Arduino IDE.
- Select the correct ESP32 board.
- Select the correct COM/serial port.
- Open Serial Monitor.
- Set baud rate to the same value used in your code.
For example:
Serial.begin(115200);
Set Serial Monitor:
115200 baud
10. Printing Variables
Suppose:
int temperature = 28;
You can print:
Serial.println(temperature);
Output:
28
Or:
Serial.print("Temperature: "); Serial.println(temperature);
Output:
Temperature: 28
11. Printing Multiple Variables
int temperature = 28; int humidity = 65; Serial.print("Temperature: "); Serial.print(temperature); Serial.print(" | Humidity: "); Serial.println(humidity);
Output:
Temperature: 28 | Humidity: 65
This is very useful for debugging sensors.
12. Debugging a Button
Suppose your button isn't working.
Instead of guessing, print its state:
#define BUTTON_PIN 4 void setup() { Serial.begin(115200); pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { int state = digitalRead(BUTTON_PIN); Serial.print("Button: "); Serial.println(state); delay(200); }
You might see:
Button: 1 Button: 1 Button: 1 Button: 0 Button: 0 Button: 1
Now you know when the button changes state.
13. Debugging an LDR
#define LDR_PIN 34 void setup() { Serial.begin(115200); } void loop() { int value = analogRead(LDR_PIN); Serial.print("LDR = "); Serial.println(value); delay(500); }
Example:
LDR = 3250 LDR = 3180 LDR = 2800 LDR = 1500 LDR = 900
You can now determine what values correspond to dark and bright conditions in your actual circuit.
14. Debugging a PIR Sensor
#define PIR_PIN 27 void setup() { Serial.begin(115200); pinMode(PIR_PIN, INPUT); } void loop() { int motion = digitalRead(PIR_PIN); if (motion == HIGH) { Serial.println("Motion Detected!"); } else { Serial.println("No Motion"); } delay(500); }
Output:
No Motion No Motion Motion Detected! Motion Detected! No Motion
15. Debugging Wi-Fi
Serial Monitor becomes extremely useful when working with Wi-Fi.
Example:
Serial.println("Connecting to WiFi...");
Then:
Serial.println("WiFi connected!");
And:
Serial.print("IP Address: "); Serial.println(WiFi.localIP());
You might see:
Connecting to WiFi... ........ WiFi connected! IP Address: 192.168.1.20
Now you know the ESP32 successfully connected.
16. Debugging Program Flow
You can print messages at important locations.
Example:
Serial.println("Program started"); Serial.println("Initializing sensors"); Serial.println("Connecting WiFi"); Serial.println("Starting web server"); Serial.println("System ready");
Output:
Program started Initializing sensors Connecting WiFi Starting web server System ready
This tells you exactly where the program reached.
17. Debugging with Labels
Instead of:
Serial.println(value);
Use:
Serial.print("Sensor value = "); Serial.println(value);
Much better.
For multiple values:
Serial.print("PIR = "); Serial.print(pir); Serial.print(" | LDR = "); Serial.print(ldr); Serial.print(" | Temperature = "); Serial.println(temp);
Output:
PIR = 1 | LDR = 1240 | Temperature = 28
18. Debugging Conditions
Suppose:
if (temperature > 30) { digitalWrite(FAN_PIN, HIGH); }
If the fan isn't starting, print:
Serial.print("Temperature = "); Serial.println(temperature);
You may discover:
Temperature = 27 Temperature = 28 Temperature = 29
So the condition:
temperature > 30
is never true.
The problem isn't the fan.
The problem is your condition.
19. Debugging if Statements
You can also print which branch is executing:
if (temperature > 30) { Serial.println("Condition TRUE"); digitalWrite(FAN_PIN, HIGH); } else { Serial.println("Condition FALSE"); digitalWrite(FAN_PIN, LOW); }
This makes debugging much easier.
20. Debugging Multiple Devices
For your smart-home type projects, you could print:
PIR = 1 LDR = 1200 Temperature= 29 Humidity = 65 Rain = 0 Smoke = 0 Light = ON Fan = OFF Security = ARM
Code:
Serial.print("PIR: "); Serial.println(pir); Serial.print("LDR: "); Serial.println(ldr); Serial.print("Temperature: "); Serial.println(temp); Serial.print("Humidity: "); Serial.println(humidity); Serial.print("Light: "); Serial.println(lightState ? "ON" : "OFF"); Serial.print("Fan: "); Serial.println(fanState ? "ON" : "OFF");
This creates a simple system dashboard in the Serial Monitor.
21. Serial Plotter
Arduino IDE also provides a Serial Plotter.
Instead of only seeing:
Temperature: 28 Temperature: 29 Temperature: 30
you can visualize changing numerical values as a graph.
For example:
Serial.println(analogRead(34));
The plotter can show the sensor value changing over time.
This is useful for:
- LDR
- Potentiometer
- Temperature
- Distance
- Voltage
- Other continuously changing measurements
22. Multiple Values in Serial Plotter
You can output multiple values in a structured format.
For example:
Serial.print("LDR:"); Serial.print(ldr); Serial.print("\tTemperature:"); Serial.println(temp);
The plotter can use these labeled values as separate series.
This is useful when comparing two sensor readings.
23. Serial.printf()
ESP32 also supports formatted printing.
Example:
int temperature = 28; int humidity = 65; Serial.printf( "Temperature: %d C | Humidity: %d%%\n", temperature, humidity );
Output:
Temperature: 28 C | Humidity: 65%
Useful format specifiers include:
%d → integer %f → floating-point %s → string %c → character
For example:
Serial.printf("Value = %d\n", value);
24. Printing Floating-Point Values
float voltage = 3.25; Serial.println(voltage);
You can also control decimal places:
Serial.println(voltage, 2);
Output:
3.25
For example:
float temperature = 28.5678; Serial.print("Temperature: "); Serial.println(temperature, 2);
Output:
Temperature: 28.57
25. Serial Input
Serial communication isn't only for ESP32 → computer.
You can also send commands from the computer to ESP32.
For example, type:
ON
into Serial Monitor.
ESP32 can read it.
if (Serial.available()) { String command = Serial.readStringUntil('\n'); command.trim(); if (command == "ON") { Serial.println("Turning ON"); } if (command == "OFF") { Serial.println("Turning OFF"); } }
This creates a simple command interface.
26. Complete Serial Control Example
#define LED_PIN 2 void setup() { Serial.begin(115200); pinMode(LED_PIN, OUTPUT); Serial.println("ESP32 Ready"); Serial.println("Type ON or OFF"); } void loop() { if (Serial.available()) { String command = Serial.readStringUntil('\n'); command.trim(); if (command == "ON") { digitalWrite(LED_PIN, HIGH); Serial.println("LED ON"); } else if (command == "OFF") { digitalWrite(LED_PIN, LOW); Serial.println("LED OFF"); } else { Serial.println("Unknown command"); } } }
Now your Serial Monitor becomes a control panel.
27. Common Serial Problems
Problem 1 — Nothing appears
Check:
Serial.begin(115200);
And make sure Serial Monitor uses:
115200
Problem 2 — Garbage characters
Example:
������������
Usually the baud rate doesn't match.
If your code says:
Serial.begin(115200);
set Serial Monitor to:
115200
28. Problem — Wrong COM Port
If Arduino IDE says something like:
Port not found
or your board doesn't appear:
Check:
USB cable ↓ USB driver ↓ Device Manager ↓ COM port ↓ Arduino IDE
Some ESP32 development boards use USB-to-UART chips such as:
CP210x CH340/CH34x FTDI
depending on the board.
29. Problem — Port Busy
You may see an error similar to:
Could not open port Port is busy
Possible causes:
- Another program is using the port.
- Another Serial Monitor is open.
- Another terminal application is connected.
- A previous upload/serial process is still using the port.
Close programs using that serial port and try again.
30. Problem — Upload Works but Serial Monitor Doesn't
Check:
1. Correct COM port 2. Correct baud rate 3. Serial.begin() 4. USB connection 5. Board reset/restart
Also remember that the Serial Monitor only shows messages that your program actually sends.
31. ESP32 Boot Messages
When the ESP32 resets or starts, you may sometimes see bootloader/system messages before your own program's output.
For example, you might see technical information about:
rst: boot: flash: load:
Don't immediately assume your program is broken.
ESP32 can output startup information during reset.
Your own program output will normally appear after your code starts.
32. Debugging Strategy
When your project doesn't work, don't immediately rewrite everything.
Use this process:
Problem ↓ Check power ↓ Check wiring ↓ Check GPIO number ↓ Print sensor value ↓ Print condition result ↓ Print output state ↓ Find exact failure point
For example:
PIR not turning LED ON ↓ Print PIR value ↓ PIR = 0 ↓ Check PIR wiring ↓ Check PIR power ↓ Check GPIO
This is much faster than randomly changing code.
33. Debugging Example
Suppose this doesn't work:
if (ldr < 1500) { digitalWrite(LED_PIN, HIGH); }
Add:
Serial.print("LDR = "); Serial.println(ldr); if (ldr < 1500) { Serial.println("Dark → LED ON"); digitalWrite(LED_PIN, HIGH); } else { Serial.println("Bright → LED OFF"); digitalWrite(LED_PIN, LOW); }
Now the Serial Monitor tells you:
LDR = 2800 Bright → LED OFF LDR = 2600 Bright → LED OFF LDR = 900 Dark → LED ON
Now you know the logic is working.
34. Debugging a Motor Project
For an ESP32 + motor driver:
ESP32 ↓ GPIO ↓ Motor Driver ↓ Motor
Print:
Serial.print("IN1 = "); Serial.println(digitalRead(IN1)); Serial.print("IN2 = "); Serial.println(digitalRead(IN2)); Serial.print("Speed = "); Serial.println(speed);
You can determine whether:
ESP32 output ↓ Correct? ↓ Motor driver input ↓ Correct? ↓ Motor
This helps isolate whether the problem is software or hardware.
35. Debug Levels
As your projects become bigger, don't print everything constantly.
You can create a simple debug switch:
#define DEBUG true
Then:
if (DEBUG) { Serial.println("Sensor initialized"); }
If you don't want debugging:
#define DEBUG false
Now those messages won't be printed.
36. Simple Debug Macro
You can make:
#define DEBUG_PRINT(x) \ if (DEBUG) Serial.println(x)
Then:
DEBUG_PRINT("System started");
This is useful in larger projects, although for beginners, normal Serial.print() is usually easier.
37. Serial Monitor as a Development Tool
Think of Serial Monitor as the ESP32's eyes and diary.
It can tell you:
What sensor sees What button does What condition is true What Wi-Fi is doing What IP address you got What command was received What state your devices are in
38. Complete Smart-Home Debug Example
#define PIR_PIN 27 #define LDR_PIN 34 #define LED_PIN 2 void setup() { Serial.begin(115200); pinMode(PIR_PIN, INPUT); pinMode(LED_PIN, OUTPUT); Serial.println("======================"); Serial.println("SMART HOME STARTING"); Serial.println("======================"); } void loop() { int pir = digitalRead(PIR_PIN); int ldr = analogRead(LDR_PIN); Serial.print("PIR: "); Serial.print(pir); Serial.print(" | LDR: "); Serial.println(ldr); if (pir == HIGH && ldr < 1500) { digitalWrite(LED_PIN, HIGH); Serial.println("Motion + Dark → LIGHT ON"); } else { digitalWrite(LED_PIN, LOW); Serial.println("Condition not met → LIGHT OFF"); } Serial.println("----------------------"); delay(1000); }
Serial Monitor might show:
====================== SMART HOME STARTING ====================== PIR: 0 | LDR: 2800 Condition not met → LIGHT OFF ---------------------- PIR: 1 | LDR: 2500 Condition not met → LIGHT OFF ---------------------- PIR: 1 | LDR: 900 Motion + Dark → LIGHT ON ----------------------
Now you can actually see the logic of your IoT system.
🧠 Most Important Commands
Start Serial
Serial.begin(115200);
Serial.print();
Print + new line
Serial.println();
Check incoming serial data
Serial.available();
Read serial text
Serial.readStringUntil('\n');
Formatted output
Serial.printf();
🔥 Debugging Formula
When something doesn't work:
PROJECT NOT WORKING │ ┌────────┴────────┐ ↓ ↓ HARDWARE SOFTWARE │ │ Power/Wiring GPIO/Logic │ │ └────────┬────────┘ ↓ SERIAL MONITOR ↓ PRINT VALUES ↓ FIND THE PROBLEM
Golden rule:
Don't guess what the ESP32 is doing — print it and see.