How to debug your Arduino Program

In this post we will discuss several tips to help you debug your Arduino sketch.  When you gain more and more experience, you may find your own best way to debug.

 

A. Use the famous LED13

 

We know that most Arduino boards have a small LED connected to digital pin 13.  You can turn it on and off to see if your Arduino board works fine.  You can also use this LED to help debug your Arduino sketch.

 

The following sketch gives an example showing how to use LED13 to indicate where your sketch is running at any given time:

 


/*
Debugging with LED13

This sketch demonstrates how to use the LED connected to
digital pin13 to help you debug your Arduino sketch.

Open Source Photonics
osphotonics@gmail.com

12/19/2014
*/

// define your LED pin
int led13 = 13;  // use this pin for debugging

void setup() {
  pinMode(led13, OUTPUT);

  // turn on led13 for 1 second, make sure system works fine
  digitalWrite(13, HIGH);   // turn the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off
  delay(1000);              // wait for a second
}

void loop() {
  int randNumber;

  // generate a random number 1, 2 or 3
  randNumber = random(1,4);

  // use the random number to determine which function to call
  if (randNumber == 1) {
    bug1();
  }
  else if (randNumber == 2) {
    bug2();
  }
  else {
    bug3();
  }

  // slow down to view the result
  delay(2000);
}

void bug1() {
  // flash led13 for 200ms once
  digitalWrite(13, HIGH);   // turn the LED on
  delay(200);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off
}

void bug2() {
  // flash led13 for 150ms twice
  digitalWrite(13, HIGH);   // turn the LED on
  delay(150);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off
  delay(150);              // wait for a second
  digitalWrite(13, HIGH);    // turn the LED off
  delay(150);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off
}

void bug3() {
  // flash led13 for 100ms three times
  digitalWrite(13, HIGH);   // turn the LED on
  delay(100);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off
  delay(100);              // wait for a second
  digitalWrite(13, HIGH);    // turn the LED off
  delay(100);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off
  delay(100);              // wait for a second
  digitalWrite(13, HIGH);    // turn the LED off
  delay(100);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off
}

 

In this program, a random number 1, 2 or 3 will be generated, and depending what that number is, the program will call different functions to flash the LED different times.  So simply from the flashing pattern of the LED, we can have an idea of what random number is generated and what function has been called.  There is no extra hardware or accessories needed for this.  The same idea can be extended to debug more complicated programs.

 

B. Use the famous Serial.print() function

 

A more powerful tool for Arduino sketch debugging is to use the Serial.print() function.  This requires the use of serial port.  In some cases it may cause confliction if you use the same serial port for other purposes in your program.  However, in most cases it is a powerful tool.

 

The following sketch extends the previous example with Serial.print() function added.

 

/*
Debugging with serial.print() function

This sketch demonstrates how to use the serial port and
the Serial.print() function to help you debug your
Arduino sketch.

Open Source Photonics
osphotonics@gmail.com

12/19/2014
*/

// define your LED pin
int led13 = 13;  // use this pin for debugging

void setup() {
  Serial.begin(9600); // send and receive at 9600 baud

  pinMode(led13, OUTPUT);

  // turn on led13 for 1 second, make sure system works fine
  digitalWrite(13, HIGH);   // turn the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off
  delay(1000);              // wait for a second

}

void loop() {
  int randNumber;

  // generate a random number 1, 2 or 3
  randNumber = random(1,4);

  // use the random number to determine which function to call
  if (randNumber == 1) {
    Serial.print("randNumber is ");
    Serial.println(randNumber);    // print the number
    bug1();
  }
  else if (randNumber == 2) {
    Serial.print("randNumber is ");
    Serial.println(randNumber);    // print the number
    bug2();
  }
  else {
    Serial.print("randNumber is ");
    Serial.println(randNumber);    // print the number
    bug3();
  }

  // slow down to view the result
  delay(2000);
}

void bug1() {
  // flash led13 for 200ms once
  digitalWrite(13, HIGH);   // turn the LED on
  delay(200);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off
}

void bug2() {
  // flash led13 for 150ms twice
  digitalWrite(13, HIGH);   // turn the LED on
  delay(150);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off
  delay(150);              // wait for a second
  digitalWrite(13, HIGH);    // turn the LED off
  delay(150);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off
}

void bug3() {
  // flash led13 for 100ms three times
  digitalWrite(13, HIGH);   // turn the LED on
  delay(100);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off
  delay(100);              // wait for a second
  digitalWrite(13, HIGH);    // turn the LED off
  delay(100);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off
  delay(100);              // wait for a second
  digitalWrite(13, HIGH);    // turn the LED off
  delay(100);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off
}

 

After the program is loaded to the Arduino board, if you open the Serial Monitor, you can see the information printed there, which matches the flashing pattern of the LED13.

openSerialMonitor

serial result

If properly use the Serial.print() function, you can output a lot of useful information onto the Serial Monitor, and it will certainly help you to debug your Arduino program.

 

By the way, if you add a global variable to control the debugging mode On or Off, you can keep your Serial.print() functions in your program all the time, even in your final production version.  You can simply turn on the global control variable to execute the debugging codes, or turn off the global control variable to skip the debugging codes.

 

C. Use the famous concept of Modularizing

 

The basic idea is to divide your code into small modules, each module fulfill a simple purpose.  By doing so you can easily manage your functions, and debug each one separately.  Modularizing can also help to improve software reusability, avoid repeating code, and reduce maintenance cost.

 

In the Arduino case, suppose we want to read temperature from a sensor, and use RGB LED to indicate if it’s too hot, normal, or too cold.  To accomplish this, we can simply build a function only to read the temperature, a function only to compare the temperature to given thresholds, and a function only to turn on a specific LED.  By doing so, we can easily debug each individual function to make sure it works, and then put them together.  For example, when we debug the LED control function, we can set a fake value as the measured temperature, put it in the program and see if the LED responds correctly.

 

In summary, there are a lot of ways to write neat code and debug it for errors.  By using one or more of these different methods, you will gain more and more experience and build your own skillset in Arduino programming and debugging.

 

Hope you enjoy this post. The Arduino Nano board can be purchased from eBay.

 

Thanks for reading.  Open Source Photonics Blogs are supported by 612 Photonics.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: