Lesson P12 – Debugging and Troubleshooting Arduino Programs
🎯 Learning Objectives
After completing this lesson, students will be able to:
✅ Understand what debugging is
✅ Identify common programming errors
✅ Understand compiler errors
✅ Understand logical errors
✅ Use Serial Monitor for debugging
✅ Troubleshoot hardware and software issues
✅ Develop systematic problem-solving skills
1. Introduction
Every programmer makes mistakes.
Even professional engineers and software developers encounter errors.
The difference is:
Beginners panic when errors occur.
Professionals know how to find and fix them.
The process of finding and fixing errors is called:
Debugging
Debugging is one of the most important skills in programming.
2. What is Debugging?
Debugging is the process of:
- Finding errors
- Identifying causes
- Correcting mistakes
- Testing solutions
until the program works correctly.
Real-Life Example
Imagine your motorcycle does not start.
You check:
✔ Fuel
✔ Battery
✔ Spark Plug
✔ Wiring
You identify the problem and fix it.
This process is similar to debugging.
3. Why Debugging is Important
Without debugging:
❌ Programs fail
❌ Sensors behave incorrectly
❌ Motors don’t move
❌ Robots malfunction
❌ Projects become unreliable
With debugging:
✅ Errors are fixed quickly
✅ Projects become stable
✅ Development becomes easier
4. Types of Errors in Arduino
Most Arduino errors fall into three categories:
Syntax Errors
Runtime Errors
Logical Errors
5. Syntax Errors
Syntax errors occur when program rules are violated.
Examples:
- Missing semicolon
- Missing bracket
- Wrong spelling
- Incorrect function usage
Example
Wrong:
int value = 10
Missing:
;
Correct:
int value = 10;
Another Example
Wrong:
digitalwrite(13,HIGH);
Correct:
digitalWrite(13,HIGH);
Arduino functions are case-sensitive.
6. Compiler Errors
Before uploading code:
Arduino IDE compiles the program.
If mistakes exist:
Compilation fails.
Example Error
expected ';' before '}'
Meaning:
A semicolon is missing.
Important Rule
Always read compiler messages carefully.
Most beginners ignore them.
Compiler messages often tell exactly what is wrong.
7. Runtime Errors
Runtime errors occur after successful upload.
Program uploads successfully but behaves incorrectly.
Example
Sensor disconnected.
Program uploads successfully.
Sensor values become incorrect.
This is a runtime issue.
Examples of Runtime Errors
- Loose wires
- Wrong pin connections
- Insufficient power
- Damaged components
8. Logical Errors
Logical errors are the most difficult to find.
Program:
✔ Compiles Successfully
✔ Uploads Successfully
❌ Produces Wrong Result
Example
Goal:
Turn LED ON when temperature > 40
Correct:
if(temp > 40)
Wrong:
if(temp < 40)
Program works.
But logic is incorrect.
9. Understanding Error Messages
When an error occurs:
Arduino IDE shows messages.
Example:
'temperature' was not declared in this scope
Meaning:
Variable does not exist.
Example
Wrong:
Serial.println(temp);
Variable created:
int temperature = 25;
Correct:
Serial.println(temperature);
10. Using Serial Monitor for Debugging
Professional Arduino developers use:
Serial Monitor
for debugging.
Example
int temperature = 35;
Serial.println(temperature);
Output:
35
Now you know the variable contains the correct value.
Debugging Example
int distance = 20;
Serial.print("Distance = ");
Serial.println(distance);
Output:
Distance = 20
Useful for verifying sensor readings.
11. Debugging Decision Making
Example:
if(distance < 10)
{
Serial.println("Obstacle Detected");
}
Suppose message never appears.
Add debugging:
Serial.println(distance);
Now you can verify:
Is distance actually below 10?
12. Step-by-Step Debugging Method
When something fails:
Step 1
Check code.
Step 2
Read compiler errors.
Step 3
Verify wiring.
Step 4
Check power supply.
Step 5
Use Serial Monitor.
Step 6
Test individual components.
Step 7
Retest entire project.
13. Common Arduino Problems
Problem 1
Upload Failed
Possible Causes:
- Wrong COM Port
- Wrong Board Selected
- Faulty USB Cable
Solution
Check:
Tools
↓
Board
↓
Arduino Uno
Check:
Tools
↓
Port
↓
Correct COM Port
14. Problem 2
Arduino Not Powering ON
Possible Causes:
- Bad USB cable
- Damaged board
- Power issue
Solution
Check:
- USB Connection
- Power LED
- Another USB Port
15. Problem 3
Sensor Not Working
Possible Causes:
- Wrong wiring
- Wrong pin number
- Missing library
Solution
Check:
✔ VCC
✔ GND
✔ Signal Pin
✔ Code
16. Problem 4
Servo Motor Not Moving
Possible Causes:
- Insufficient power
- Wrong signal pin
- Faulty servo
Solution
Test with Servo Example Program.
17. Problem 5
Serial Monitor Shows Garbage Characters
Example:
¤@#&%$^
Cause:
Baud rate mismatch.
Solution
Match:
Serial.begin(9600);
and Serial Monitor:
9600
18. Hardware Troubleshooting Checklist
Before blaming code:
Check:
✅ Power Supply
✅ Wiring
✅ Component Orientation
✅ Loose Connections
✅ Battery Voltage
✅ Damaged Components
19. Software Troubleshooting Checklist
Check:
✅ Semicolons
✅ Brackets
✅ Variable Names
✅ Function Names
✅ Pin Numbers
✅ Conditions
20. Divide and Conquer Technique
Large projects are difficult to debug.
Example:
Obstacle Avoiding Robot
Contains:
- Arduino
- Ultrasonic Sensor
- L298N
- Motors
Instead of testing everything together:
Test:
Sensor First
↓
Motors Second
↓
Motor Driver Third
↓
Full Robot Last
This technique saves enormous time.
21. Debugging with LEDs
LEDs can be used as indicators.
Example:
digitalWrite(13,HIGH);
Place this inside conditions.
When LED lights:
You know that code section executed.
Example
if(distance < 10)
{
digitalWrite(13,HIGH);
}
Helps confirm condition execution.
22. Common Beginner Mistakes
Mistake 1
Changing multiple things at once.
Always change one thing at a time.
Mistake 2
Ignoring error messages.
Read them carefully.
Mistake 3
Not using Serial Monitor.
Serial Monitor is your best debugging tool.
Mistake 4
Assuming hardware is perfect.
Many issues are caused by wiring mistakes.
23. Best Practices
✅ Test small code sections
✅ Use Serial Monitor frequently
✅ Keep wiring organized
✅ Save backup copies of working code
✅ Comment important changes
✅ Test one component at a time
24. Real-World Example
Suppose your Automatic Water Dispenser is not working.
System:
- IR Sensor
- Relay
- Pump
- Arduino
Debugging Process:
Step 1:
Check sensor values using Serial Monitor.
↓
Step 2:
Check relay operation.
↓
Step 3:
Check pump power.
↓
Step 4:
Test complete system.
Problem found quickly.
📊 Summary
In this lesson, we learned:
✅ What debugging is
✅ Syntax errors
✅ Runtime errors
✅ Logical errors
✅ Compiler messages
✅ Serial Monitor debugging
✅ Hardware troubleshooting
✅ Software troubleshooting
Debugging is a critical skill that helps transform a non-working project into a successful and reliable system.
📖 Key Terms
Debugging
Finding and fixing errors.
Syntax Error
Violation of programming rules.
Runtime Error
Error occurring during execution.
Logical Error
Program runs but produces incorrect results.
Compiler
Software that converts code into machine language.
Troubleshooting
Systematic problem-solving process.
🎯 Quiz
1. What is debugging?
A. Uploading code
B. Finding and fixing errors ✅
C. Wiring components
D. Installing Arduino IDE
2. Which error occurs when code violates programming rules?
A. Runtime Error
B. Logical Error
C. Syntax Error ✅
D. Hardware Error
3. Which tool is most commonly used for Arduino debugging?
A. Calculator
B. Serial Monitor ✅
C. Paint
D. Browser
4. What causes garbage characters in Serial Monitor?
A. Wrong sensor
B. Low battery
C. Baud rate mismatch ✅
D. Missing LED
5. What should be checked first when troubleshooting?
A. Buy a new Arduino
B. Check code, wiring, and power ✅
C. Restart computer
D. Delete project
🏠 Assignment
Task 1
Create a list of 10 common Arduino programming mistakes.
Task 2
Write a program with a deliberate syntax error and identify the compiler message.
Task 3
Use Serial Monitor to display a sensor value and explain how it helps debugging.
Task 4
Create a troubleshooting checklist for an ultrasonic distance meter project.
Task 5
Explain the difference between syntax errors, runtime errors, and logical errors with examples.
✅ P12 completes the core Arduino Programming Fundamentals section.
The next lessons should move into practical programming concepts such as:
- P13 – Digital Input and Output Programming
- P14 – Analog Input and PWM Programming
- P15 – Sensor and Actuator Integration Logic
These deserve more detailed coverage because they directly connect programming concepts to real Arduino projects.