Simple Arduino Debugging Techniques

debug icon

Here are a couple of simple techniques I have found useful for debugging:

Arduino Pin 13 LED1. Turn on/off the pin 13 LED by inserting a digitalWrite(13, HIGH) before/after questionable areas of your program. If the Arduino’s built-in LED turns on, then you know the particular line of code has executed. However, this method can only be used in one spot for one shot, otherwise you won’t be able to determine which location is responsible for turning on/off the LED. Remember, a man with 2 watches doesn’t know what time it is!

4 LEDs2. To overcome the single LED limitation above, simply add extra LEDs (remember that an LED must have a resistor in series, otherwise it will go up in smoke). As an example, you can set pin D5 to output LOW (or ground), and insert an LED and resistor between it and say, pin D4. Then set D4 to output LOW to turn the LED off and HIGH to turn it on.

Hello3. Another method I often use is printing debug messages to the serial port (like, Serial.println(“Here”) ). However, it takes a significant amount of processing time and resources to write to the UART. Therefore, this technique can easily disrupt critical timing tasks in your program. Additionally, serial printing can not be called from inside an interrupt routine. It’s also problematic if you are using the URAT for other tasks. I have found this technique to be most useful during the early stages of program development.

Arduino Pin 13 LED4. I highly recommend using a logic analyzer like the Saleae Logic. The Saleae software includes several protocol analyzers (SPI, I2C, 1-wire, etc.) that make debugging much easier. A limited functionality but much cheaper alternative is the Bus Pirate. It’s very handy if you need to measure how long something executes, like a function call (set an unused pin HIGH at the start and LOW at the end and use the Saleae Logic to measure the time the pin is HIGH; see my example here). While a full discussion of a logic analyzer is beyond the scope of this post, I believe that for mcrocontroller development, a logic analyzer is probably a more usefull tool than an oscilloscope (note that a logic analyzer only works with digital signals).

Timing the Little Things5. Would you like to know how long it takes to execute a small section of your code? You say you don’t have an oscilloscope or a logic analyzer? There is a simple and accurate method to determine the number of clock cycles your code requires, and with very little impact on your code. Read about the technique which is explained here.

AVR Studio Debug6. On occasion I have re-written my Arduino code (or snippets of questionable code) in order to run inside the ATMEL AVR Studio. The studio allows the use of the simulator which incorporates some advanced debugging features (C vs. assembler level, single-stepping, variable watch, timing, etc).

Array Dump7. Dump into Array: With this technique, we add code which dumps strategic information into an array at run time, and then observe it at a later time (i.e. when the program terminates). For example, assume ‘good’ and ‘bad’ are two 8-bit variables we want to capture. Our first step is to define a buffer in RAM to save the debugging measurements:

#define DEBUG_BUFFER_SIZE 32
uint8_t GoodBuffer[DEBUG_BUFFER_SIZE];
uint8_t BadBuffer[DEBUG_BUFFER_SIZE];
uint8_t DumpCount = 0;

The variable DumpCount is used as an index into the buffers. Add a routine like this to your program:

void Save(void) {
  if (DumpCount < DEBUG_BUFFER_SIZE) {
    GoodBuffer[DumpCount] = good;
    BadBuffer[DumpCount] = bad;
    DumpCount++;
  }
}

Simply add ‘Save();’ statements at strategic places within your code. Then, you can either use a debugger (see item #6 above) to display the results, or add code to print the results once the program has completed.

8. Finally, while not simple, the ultimate method would be to use DebugWire, JTAG and/or JTAGICE. However, this requires expensive hardware and a modification to the Arduino. You can read more here and here.

Advertisements

About Jim Eli

µC experimenter
This entry was posted in Uncategorized and tagged , , , , , . Bookmark the permalink.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s