Debugging Arduino Sketches with Atmel Studio 7


Atmel’s AtmelStudio version 7 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 3×2 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 3×2 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.


One 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.


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.


  1. Very nice tutorial, much appreciated 🙂

  2. Hi,

    You may want to uncheck the set BOOTRST fuse, so your program execution will start at address 0x0000 immediately. Now it starts execution wherever your Bootsize start is set, wraps over to 0x0000 (because all the 0xFF in flash is a NOP for the atmel avr’s) and starts then executing your program’s reset vector jump to the compiler startup code and further through to main() function ;o).

    • Thanks Tom,

      I wonder why this wasn’t an issue when I debugged the Arduino Sketch?


      • You are welcome Lon,

        The 328p just needs a blink of an eye @20Mhz to start execution of the NOP’s from Bootsector start address to reach top of flash, then overflows to 0x0000 and executes the reset vector of your application.

        Just do not forget to reset the fuse when you flashed back the arduino bootloader…

        Enjoy, Tom.

  3. This article looks to be exactly what I’m looking for…but I ran into a problem where the drop-down lists during the Arduino project import, in my case, has no entries. No boards, no Chips. Any idea why this would be? The rest of AVR studio, including my using JTAP and ISP have both been working just fine; it’s just the importing that seems it can’t find them. And yes, the path/files to the ino and install folder are correct.

    • Hi Thom,

      I’m not sure what the issue is with your software installation. You probably need to contact Microchip, who purchased Atmel since this was written. Maybe there is a file that the software parsed to determine the Arduino type and Atmel part that is no longer present when the Arduino IDE is installed.


  4. Question: How is the C5 disconnect from the reset line when the RESET-EN solder bridge is cut with a razor. It appears on the , on the schematic there is another trace that runs in parallel with the RESET-EN . That has been bugging me, if you could answer that I would greatly appreciate it.

    • Hi James,

      Now that you mention it, it kind of bugs me too. As far as I can tell (from the schematic) cutting the trace between the RESET-EN pads would still leave C5 connected to DTR.

      I’m pretty sure what’s going on though is that they modeled their RESET-EN part in a way that requires the parallel connection to make the schematic netlist work for the circuit board.

      Normally you use something called a “net-tie” to connect two nets with a shorted link. However, in this case I think they just modeled a part with two pins that looks like a net-tie on the schematic, and then they had to route the parallel trace across RESET-EN’s pads in the schematic, and between the pads on the PCB.

      Let me know if that’s not a clear description.


  5. Shubham Bora says:

    thanks a lot dude!!

Speak Your Mind


This site uses Akismet to reduce spam. Learn how your comment data is processed.