Loading... Please wait...

Debugging Arduino Sketches with Atmel Studio 7

Posted by

Atmel’s Studio 7 (Atmel is now owned by Microchip) allows you to import Arduino sketches and debug them. This brings the Arduino into the realm of a professional design engineer tool.

Getting an Arduino to the point of debuggin’ its firmware is a pretty easy process, but does take several steps. Here are some things you’ll want to take note of before beginning…

1. You’ll need a debugger/programmer. I used the Atmel-ICE which retails for about $60US and comes with a connector compatible with the Arduino ICSP connection.

2. I used an Arduino Uno 3. There could be some differences between this Arduino and other styles.

3. You’ll have to download and install AtmelStudio 7. This development environment is based on Microsoft’s Visual Studio. I’m pretty familiar with Visual Studio, so getting up to speed with AtmelStudio was not a problem. I won’t go into any detail here on how to use AtmelStudio beyond getting a project running and recovering the Arduino.

4. Speaking of recovering the Arduino… you have to make a simple hardware change to allow ‘debugWire’ to work with the Arduino. Additionally, programming the sketch using AtmelStudio will wipe out the Arduino bootloader, so you may want to recover it if you intend on using the Arduino for other projects.

5. It’s a good idea to read through this blog before jumping into this. If you’re not comfortable with the changes you’ll be making to the Arduino you should probably not try this.

Okay, here we go…

Hardware Mods:The Arduino Uno 3 microcontroller, an ATmega328p, comes loaded with a serial bootloader (optiboot_atmega328.hex). The bootloader is a program that runs on reset and programs the microcontroller’s memory with values sent via a serial interface. This program is what allows new sketches to be uploaded to the device. The bootloader is entered when the serial DTR line briefly changes state. In the schematic clip below the line marked CH1 is the DTR line, and CH2 is the RESET line.

Here is the reset pulse captured by an oscilloscope as a sketch is uploaded to the Arduino.

Unfortunately, C5 in the schematic impacts Atmel’s ‘debugWIRE’ debugger interface, which also relies on control of the ATmega328p reset line. Fortunately, and helpfully, the Arduino people have left a jumper cut-line on the PCB that allows you to disconnect C5 from the reset line.

Using a razor blade or other sharp tool cut the trace that connects the two tinned pads labeled ‘RESET EN’. Be sure not to cut the trace that runs from one of the pads to C5. NOTE… without this trace the Arduino will no longer be programmable with the Arduino software even if the bootloader is present on the microcontroller. FYI, C5 is the capacitor closest to the tinned pads, and is not marked on the PCB.

Once this cut is made you can proceed to debugging an Arduino sketch. However, I added a 2-pin header across the tinned pads that allow me to switch between Atmel’s ‘debugWIRE’ interface and the standard Arduino serial bootload mode. This is done by removing or adding a jumper.

In order to import and debug an Arduino sketch you need to do the following…

1. Make sure the reset is disconnected from C5, in my case this is done by removing the jumper from the 2-pin header I installed.

2. Plug the Atmel-ICE 6-pin header over the 3x2 footprint marked ICSP. The tab on the Atmel-ICE receptacle should face inboard (toward C5) of the Arduino Uno 3. If you’re not using the Arduino Uno 3 the tab of the Atmel-ICE connector is on the same side of the 3x2 header as pin 1.

3. You’ll need to power the Arduino Uno through the USB or power supply port.

Importing and Debugging the Sketch:

Now that the hardware is ready you can launch AtmelStudio and begin to import the Arduino sketch. In my case I’m just going to import and debug the Blink sketch.

Launch AtmelStudio and select New Project from the Start menu.

Select “Create project from Arduino sketch”. Enter a Name, Project Name, and browse to where you want to keep it, then click the OK button.

Navigate to the Arduino sketch you want to convert. Then press the “Open” button.

Select the appropriate board and device for your project. In my case its Arduino Uno and ATmega328p. Press the “OK” button.

You should see the project get built, and all of the libraries, include files, and source code will be included in the project. The project, and how it is organized, is on the right. Sketch.cpp, which is the main C++ program file is shown on the left. As you open other files new tabs are added on the left.

To debug the program you need to configure the Atmel-ICE to operate in debugWIRE mode. Select the Project pull down menu and at the bottom select “ Projectname Properties…” (or click alt-F7). Select the debugger/programmer tool (Atmel-ICE) and select debugWIRE from the Interface options.

You can now switch back to the Sketch.cpp tab. Double-clicking on the margin left of the Sketch.cpp code will set a break point (indicated by a red dot, an example is shown below). Select Debug from the run options shown in the other blue rectangle in the image below (under the Help menu).

Clicking on the blue run/pause icon just left of the debug option will start debugging.


When you first start to debug a sketch your ATmega328p will probably not have correct fuse settings. If that’s the case you will see the following message box. Click the “Yes” button.

That may bring up an additional message box. If so, toggle the power to the board and then press “OK”

You can now use AtmelStudio to step through the firmware, display variables, and establish breakpoints. Here’s one link that provides some information on how to do that…

Exiting debugWIRE:

To be honest, I’m not a fan of the debugWIRE functionality, as it seems cumbersome and likely to brick the microcontroller from time-to-time. Now that I got that off my chest, be sure that when you stop debugging you remember to exit debugWIRE. Select Debug/Disable debugWIRE and Close as shown below.

NOTE: If you don’t correctly exit debugWIRE the microcontroller will probably act a little quirky. It will do cool things like prevent you from programming the device in ISP mode. I’ve recovered from this by re-entering debugWIRE mode, running the debugger, and then disabling debugWIRE mode as previously described.

Restoring the Bootloader:

If you’re going to write code in AtmelStudio for the Arduino it seems like a one-way-street. I don’t know of a way to go from an AtmelStudio project back to an Arduino sketch. To be honest I didn’t look very hard, so maybe there is one. As far as I’m concerned, once you’re able to debug and step through code using the Arduino libraries there’s not much of a reason to go back.

But you might want to use the Arduino for another project, so here are the steps to get the serial bootloader back into your Arduino.

You need to configure the Atmel-ICE to operate in ISP mode. Select the Project pull down menu and at the bottom select the “ Projectname Properties…”(or click alt-F7). Select the debugger/programmer tool and select ISP from the Interface options.

Select Tools/Device Programming

Select the microcontroller you are using, for the Arduino Uno it is the ATmega328p. Press the “Apply” button followed by the “Read” button. You should see the device ID and the voltage show up. If not, you could still be in debugWIRE mode, or you might have a bad connection somewhere.

On the left side click on “Fuses”. Enter 0x05 in the EXTENDED box, 0xDE in the HIGH box, and 0xFF in the LOW box. Press the “Program” button.

Select “Memories” from the left side of the window in focus. Click the button marked with “…” and navigate to the location of the bootloader.

Select the appropriate bootloader hex file for the Arduino you are working with. In my case its optiboot_atmega328.hex.  Press "Open".

Now press the “Program” button. You’re almost back to normal.

Disconnect the Atmel-ICE from the Arduino (assuming programming succeeded).

In order to get the bootloader operating correctly you’ve got to short the reset pads you cut apart at the start of all this. If you remember I added a header to the tinned pads. So for me, I just need to add a jumper as shown below.

And with all that done I can now upload the original “Blink” sketch to the Arduino.

That’s it.

comments powered by Disqus

Recent Updates

Connect with us: Facebook Twitter