📘 Debugging & Serial Testing Techniques
🎯 Lesson Objective
By the end of this lesson, students will:
-
Understand what debugging is
-
Learn types of programming errors
-
Use Serial Monitor effectively for debugging
-
Debug sensor issues
-
Debug logic errors
-
Debug hardware issues
-
Follow a professional step-by-step testing method
1️⃣ What is Debugging?
Debugging is the process of:
Finding and fixing errors in your program.
Errors are normal.
Even professional engineers debug daily.
Debugging is not failure.
It is part of development.
2️⃣ Types of Errors in Programming
There are 3 main types:
🔹 1. Syntax Errors (Compile Errors)
These occur before uploading.
Example:
Missing semicolon → Compilation error.
Arduino IDE shows error message.
Solution:
Fix syntax.
🔹 2. Logical Errors
Program runs but gives wrong output.
Example:
Instead of:
Code compiles.
But logic is wrong.
Harder to detect.
🔹 3. Hardware Errors
Code is correct.
But:
-
Wrong wiring
-
Loose connection
-
No power
-
Wrong pin
Serial Monitor helps identify this.
3️⃣ Why Serial Monitor is the Best Debug Tool
Serial Monitor allows you to:
✔ Print variable values
✔ Track execution flow
✔ Check sensor readings
✔ Confirm logic
✔ Identify where code stops
Without Serial:
You are guessing.
With Serial:
You are observing.
4️⃣ Basic Debug Print Technique
Example:
Serial.println("Program Started");
Add prints at important points:
Serial.println(“Checking Condition”);
Serial.println(“Turning Relay ON”);
This shows program flow.
5️⃣ Debugging Sensor Values
When sensor is not working:
Print raw value first.
Example:
int gasValue = analogRead(34);
Serial.print("Gas Value: ");
Serial.println(gasValue);
Now observe:
Is value changing?
If not:
Check wiring.
6️⃣ Debugging Conditions
Example:
if (temperature > 30) {
Serial.println("Condition TRUE");
} else {
Serial.println("Condition FALSE");
}
This confirms if logic is working.
Never assume condition is correct.
Always print test message.
7️⃣ Debugging WiFi Connection
Example:
Serial.println("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("Connected!");
This shows connection status.
Without print:
You don’t know if stuck.
8️⃣ Structured Debug Format (Professional Method)
Instead of random prints:
Use structured format.
Example:
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" | Gas: ");
Serial.print(gasValue);
Serial.print(" | AC State: ");
Serial.println(acState);
Output:
Temperature: 28 | Gas: 1200 | AC State: 0
This gives full system state in one line.
Professional developers use this method.
9️⃣ Step-by-Step Debugging Method
When project doesn’t work:
Step 1 – Check Power
Is ESP32 ON?
Step 2 – Check Serial Output
Is Serial printing?
If no:
Check baud rate.
Step 3 – Check Sensor Values
Print raw sensor data.
Step 4 – Check Conditions
Print condition result.
Step 5 – Check Output Pin
Print when output triggered.
Debug one layer at a time.
Never debug everything together.
🔟 Isolating Problems (Very Important)
Example:
Smart AC not working.
Break system into parts:
-
Temperature reading
-
Condition check
-
Relay control
Test each part individually.
This is professional debugging.
1️⃣1️⃣ Common Real Debug Scenarios
Problem 1 – LED Not Blinking
Check:
-
pinMode set?
-
Correct pin number?
-
LED connected properly?
-
Serial printing?
Problem 2 – Sensor Always Zero
Check:
-
Correct analog pin?
-
Power connected?
-
Ground common?
Problem 3 – Condition Not Triggering
Print variable:
Serial.println(temperature);
Maybe threshold wrong.
1️⃣2️⃣ Debugging Infinite Loop
Example:
}
If program freezes:
Add print before loop.
If nothing prints after:
You know where code stuck.
1️⃣3️⃣ Using Debug Flags
Advanced technique:
bool debugMode = true;
if (debugMode) {
Serial.println("Debug Info");
}
Later you can disable debugging easily.
1️⃣4️⃣ Good Debugging Practices
✔ Print labels with values
✔ Use clear messages
✔ Remove unnecessary prints in final code
✔ Test one feature at a time
✔ Keep debugging structured
1️⃣5️⃣ Debugging Mindset
Never think:
“Code is correct.”
Instead think:
“Let me verify every step.”
Debugging is investigation.
Be patient.
Observe.
Test.
Correct.
📌 Lesson Summary
In this lesson, we learned:
-
What debugging is
-
Types of errors
-
How to use Serial Monitor
-
How to debug sensors
-
How to debug logic
-
How to isolate problems
-
Professional debugging flow
Debugging is the most important programming skill.
Master it → You become independent.