Course Content
๐Ÿ“˜ MODULE 9 โ€“ Automatic Dustbin
0/1
๐Ÿ“˜ MODULE 10 โ€“ Obstacle Avoiding Robot
0/1
๐Ÿ“˜ MODULE 11 โ€“ Edge Avoiding Robot
0/1
Arduino Hands-On Programming and Robotics Course

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.

Scroll to Top