Developing Arduino Projects with Visual Studio

This tutorial shows how to use the Advanced Arduino Project Subsystem of VisualGDB to easily develop Arduino projects with Visual Studio. While VisualGDB also supports Arduino-compatible boards based on ARM, ESP8266 and ESP32 platforms, this tutorial covers the classic Arduino Uno board based on the AVR platform. We will create a basic “Blinking LED” project and will show how to use various Arduino-related features of VisualGDB to develop it conveniently.

Before you begin, install VisualGDB 5.4 Preview 4 or later. Although you can install the Arduino IDE to use the Arduino platforms and libraries shipped with it, this is not necessary as VisualGDB will automatically download the necessary Arduino packages if they are not already installed.

  1. Start Visual Studio and open the VisualGDB Arduino Project Wizard:01-prjname
  2. On the first page of the wizard select “Create a new ‘Blinking LED’ project”:02-led
  3. On the next page of the wizard, choose your Arduino device. In this tutorial we will select “Arduino/Genuino Uno”. If you have not created any projects for the selected Arduino platform, VisualGDB will suggest automatically installing the necessary packages. Click “Install” to proceed:03-getuno
  4. Once the package installation succeeds, ensure the correct board is selected, plug it into your computer and pick the corresponding COM port:
    04-comportNote that some boards will not appear in the list until you install the corresponding package. This happens because the Arduino package lists do not include all boards that are actually provided by the packages. If you cannot find your board in the list, try selecting a similar board, installing the package and check for your board again.
  5. In this tutorial we will focus on building the project, using libraries and programming the FLASH memory, so select “Built-in GDB simulator” as the debug method:05-dummydebug
  6. Press “Finish” to generate the project. VisualGDB will create a basic project with a single Arduino sketch. Build it by pressing Ctrl-Shift-B:06-buildNote that the Advanced Arduino projects get the project structure directly from the Arduino build tools, so VisualGDB will always show the precise list of source files used by the various project’s libraries and will configure IntelliSense to see the code exactly the same as the compiler does (including the automatic generation of Arduino function prototypes for sketch files).
  7. Now you can program the built project into the FLASH memory. Simply right-click on the project in Solution Explorer and select the corresponding command:07-program
  8. VisualGDB will invoke the Arduino tools to program the memory using the Arduino bootloader and will display a confirmation once the memory is programmed:08-uploaded
  9. Now we will show how to reference libraries from your sketches. Start typing #include <SoftwareSerial.h> and wait for the IntelliSense suggestion to appear:09-serialThe advanced Arduino projects are aware of the libraries available on the selected platform and can suggest the related header files automatically. Once you include the header file and save the source file, VisualGDB will automatically find the library, show it in Solution Explorer and configure IntelliSense to handle it properly:10-inst
  10. Replace the contents of the main sketch file with the following code:
    #include <SoftwareSerial.h>
     
    SoftwareSerial serial(10, 11);
     
    void setup()
    {
        pinMode(LED_BUILTIN, OUTPUT);
     
        Serial.begin(57600);
        while (!Serial)
        {
        }
    }
     
    int g_Iteration;
     
    void loop()
    {
        digitalWrite(LED_BUILTIN, HIGH);
        delay(1000);
        digitalWrite(LED_BUILTIN, LOW);
        delay(1000);
     
        char tmp[32];
        sprintf(tmp, "Iteration %d\r\n", g_Iteration++);
        Serial.write(tmp);
    }

    Then build the project:
    11-code

  11. Right-click on the project in Solution Explorer and click VisualGDB Project Properties. Then go to the Arduino Serial Terminal page and enable the terminal (ensure the baud rate matches the 57600 bits per second configured in the code):
    12-comport
  12. Program the device FLASH memory again and open the serial terminal via the context menu in Solution Explorer:
    13-openterm
  13. VisualGDB will display the output from your sketch in a terminal pane inside the Visual Studio window:14-termopenedNote that you can use the ANSI escape sequences if you want to add colors to your sketch output.
  14. VisualGDB automatically indexes the sources of all libraries used by the sketch. This way you can use the “Go to definition” command to locate the definitions of various functions or methods (e.g. write()), or Code Map to visualize the code structure:15-goto
  15. You can use the VisualGDB Embedded Memory Explorer to analyze the memory used by your sketch:16-eme
  16. Click “Explore details” to switch to the detailed view:
    17-emebrief
  17. Now you can see the contribution to the memory footprint of the individual sections, source files, or functions. VisualGDB will also display the sizes directly in the code, so you can make changes to your code and quickly see how it affects the footprint:18-emedetail

You can also configure VisualGDB to debug your AVR-based Arduino board. See this tutorial for a detailed list of steps.