Debugging an Arduino with an Arduino

As every Hackaday reader knows, and tells us at every opportunity in the comments, adding an Arduino to your project instantly makes it twice as cool. But what if, in the course of adding an Arduino to your project, you run into a problem and need to debug the code? What if you could use a second Arduino to debug the first? That would bring your project up to two Arduinos, instantly making it four times as awesome as before you started! Who could say no to such exponential gains?

Debugging an ATTiny85

Not [Wayne Holder], that’s for sure. He writes in to let us know about a project he’s been working on for a while that allows you to debug the execution of code on an Arduino with a second Arduino. In fact, the target chip could even be another AVR series microcontroller such as a the ATTiny85. With his software you can single-step through the code, view and modify values in memory, set breakpoints, and even disassemble the code. Not everything is working fully yet, but what he has so far is very impressive.

The trick is exploiting a feature known as “debugWIRE” that’s included in many AVR microcontrollers. Unfortunately documentation on this feature is hard to come by, but with some work [Wayne] has managed to figure out how most of it works and create an Arduino Sketch that lets the user interact with the target chip using a simple menu system over the serial monitor, similar to the Bus Pirate.

[Wayne] goes into plenty of detail on his site and in the video included after the break, showing many of the functions he’s got working so far in his software against an ATTiny85. If you spend a lot of time working on AVR projects, this looks like something you might want to keep installed on an Arduino in your tool bag for the future.

Debugging microcontroller projects can be a huge time saver when your code starts running on real hardware, but often takes some hacking to get working.

14 thoughts on “Debugging an Arduino with an Arduino

  1. Cue lawyers?
    This is extremely cool, as the only way to do this previously was to shell out for the atmel ICE, along with it’s expensive cables. And which only appears to work with AVR studio, and unfortunately I use the gcc/avrdude toolchain.

    1. The AVR Dragon is a lot less expensive and also supports several programming and debug interfaces. Of course, the Arduino is even cheaper.

      I didn’t like having to disable ISP every time I wanted to start debugging using Debugwire. Not sure though if that’s required for every device with Debugwire.

      1. Yes, SPI must be disabled when DEBUGWIRE is enabled. The problem is that on some parts, like the ATmega48/88/168/328, sometimes the DEBUGWIRE and SPI get disabled and there is no way to communicate with the AVR in a low-voltage serial programming mode. You then have to resort to the HVPP mode to reset the fuses back to a sane state. I’ve seen the same thing happen on the ATtiny25/45/85’s as well, which you then have to use the HVSP mode to reset the fuses. In cases where the DEBUGWIRE is still enabled and SPI isn’t, AVRDUDE can temporarily change the fuses to enable and program in SPI mode but it is temporary, so you need to properly set the fuses first.

        I always liked the AVR DRAGON and it wasn’t too expensive to acquire at US$60. I was lucky enough to acquire a 2nd DRAGON at an ATMEL workshop for free. Its semi-permanently wired for HVPP for the ATmega48/88/168/328, sockets, headers and wires.

        Personally, I prefer to use parts with the JTAG interface for debugging (ATmega2560, AT90USB1286, ATmega32, etc.) because that interface has never failed me. It does however, leave me with 4 analog pins (ADC0 to ADC3) that I can’t use while debugging code. Sigh.

        I see MICROCHIP is having a sale on the ATATMEL-ICE at 50% off for the “full kit”. At half of US$130, you’re spending the same US$60 you would be spending on a DRAGON and it has a nice case and cables as well Looks like one can also acquire a “bare board” (no case and cables) for US$50, which is less than the AVR DRAGON.

        Peace and blessings.

  2. “As every Hackaday reader knows, and tells us at every opportunity in the comments, adding an Arduino to your project instantly makes it twice as cool.”

    Those 555 projects will be so much better.

  3. This is really great – thank you. I have an AVR dragon and i’ve had an atmega in and out of debug mode with avr studio but i was worried and confused the whole time. this is way more accessible. It actually makes the whole ISP programming process much clearer. I’m definitely going to play with it so i’ve got it in my toolbox when i need it.

  4. I’ve posted a new, part 3 article on my debugger project, along with new code and an overview video.  The new code  is now able debug an Arduino UNO running at 16 MHz and also includes some bug fixes and enhancements.

Leave a Reply

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

You are commenting using your 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