Debugging Arduino AVR boards with Visual Studio

This tutorial shows how to debug the AVR-based Arduino boards using Visual Studio and the Advanced Arduino Project Subsystem of VisualGDB. Before you begin, install VisualGDB 5.4 Preview 4 or later, get an AVR debug probe (e.g. AVR Dragon) and follow our basic Arduino tutorial to create a simple project.

Warning: due to the limitations of the AVR architecture, the AVR devices need to be explicitly switched between the production mode (DWEN fuse not set) and the debug mode (DWEN fuse set). Enabling the debug mode makes regular FLASH programming via bootloader unusable; disabling it makes the debugging impossible. For all new designs, we recommend using ARM-based or ESP32-based devices that don’t have this limitation. If you are already using an AVR-based Arduino board, follow the instructions in this tutorial to enable and disable the debug mode.

  1. The original Arduino Uno board contains a capacitor that facilitates resetting the board into bootloader programmatically, however this capacitor prevents the AVR debug probes from debugging the board properly. Before we proceed with debugging, we need to modify the board to allow disconnecting the capacitor. Locate the “RESET-EN” pad on the board (see the schematic) and cut the line between the 2 pads. Then solder a pair of jumper terminals to the pads:01-jumperAs long as the jumper is not installed, the board can be debugged using an AVR debug probe, however programming FLASH memory via a bootloder won’t work. Installing the jumper will prevent debugging, but will restore the original bootloader functionality.
  2. Connect your AVR debug probe to the ICSP connector on the Arduino Uno board:02-jtag
  3. The last step required before we can proceed with debugging is to program the DWEN fuse on the device. The easiest way to do this is via the Tools->Device Programming command in Atmel Studio:00-tdp
  4. Select your AVR probe and the device (ATmega328P for AVR Uno) and click “Apply”:01-studio-connect
  5. Then set the “HIGH.DWEN” fuse and click “Program”:02-program
  6. Click “Continue” to confirm programming:
    03-confirm
  7. Once the Arduino Uno board is configured for programming, open your VisualGDB project from the basic AVR tutorial. Then open VisualGDB Project Properties, go to the Debug Settings page and select your debug probe. Ensure you set Debug Interface to DebugWire:04-config
  8. Set a breakpoint in the loop() function and press F5 to begin debugging:
    05-debugAs long as the AVR board is properly setup for debugging, VisualGDB will provide the same debugging experience as with any other supported target. The FLASH memory will be automatically programmed and VisualGDB will let you set breakpoints and step through your code. However, while the AVR chip is in the debugging mode, it will only run with a debugger connected and won’t let you use the bootloader for programming the FLASH memory.
  9. To turn the debug mode off, go back to Atmel Studio and create an empty project:06-pseudoprj
  10. Select your AVR chip in the Device Selection dialog:08-target
  11. Open Debug Settings, pick your debug probe and ensure “Skip programming” is selected:09-skipprog
  12. Start a debugging session, then click “Debug->Disable debugWIRE and Close”:10-disable
  13. If the “Disable debugWIRE and Close” command doesn’t appear, ensure you have the advanced profile enabled via Tools->Select Profile:11-profile
  14. Finally, set the jumper between the “RESET-EN” terminals to restore the bootloader functionality:03-jumper-on
  15. Now Arduino Uno will run normally without the debugger and you will be able to use the regular “Program FLASH” memory command again to program the memory using the bootloader.