Using VS Code with Win 10 - My Success Story

After many hours of messing around, I finally got VS code to build the EXACT same code as the Arduino IDE for my Due board. Thought I would share my account of key enablers to achieve success in getting VS Code to now be able to work my project without reporting any "problems".

I believe the following to be the general process I went through:

  1. Install Arduino IDE. Developed my application for the Due. Got things working, then decided I might want to use VS Code for future work.

  2. Install VS Code. Initially installed the latest version, but found it had problems with pulling up the Arduino Serial Monitor or selecting the port to use for it. Recently found online a number of folks experiencing the same thing, they suggested a successful workaround was to revert back to version 1.35, which can be found here: Visual Studio Code May 2019

  3. Install Microsoft Arduino extension in VS Code.

Aside from getting the "correct" version of VS Code that works the serial port properly, these steps were all straightforward. And despite the "problems" reported in VS Code, it did build my project successfully. But the reason for wanting to move to VS Code was to enable all its cool incremental features over the capable Arduino IDE. So I had to tackle the "problems" reported in each file. This took a lot of time, but it boiled down to a manageable number of changes....

  1. Changes in settings.json
    These aren't likely necessary any longer, but I changed them so will mention that. It was helpful to do these to help debug the state of change along the way.

My settings.json now looks like this:

{
    "C_Cpp.errorSquiggles": "Enabled",
    "C_Cpp.dimInactiveRegions": true,
    "C_Cpp.loggingLevel": "Debug",
}

The "Debug" setting tells VS Code to send to the Output screen all the include directories being used, and whether or not they are recursive. This is very helpful in determining the effectiveness of your changes in the next section. "C_Cpp.dimInactiveRegions" tells VS Code to dim sections of code that are not compiled because of condition compilation directives. This is helpful in finding macros and constants that haven't been defined but should have been, meaning another include directory is needed.

  1. Changes to the "IncludePath". In c_cpp_properties.json, there is a section in "configurations" called "includePath". These famously need to be modified in order for all the core files to be found by VS Code. Would be nice if the Microsoft VS Code extension did a better job of taking care of this automatically, but it doesn't, so here we are. It is likely that different projectcs using different features from within the core may need different include paths than what I had, that's why using logging level: Debug is a good thing, as mentioned in #4 above.

Here is what I ended up with for this project:

                "C:/Users/username/AppData/Local/Arduino15/packages/arduino/tools",
                "C:\\Users\\username\\Documents\\ArduinoData\\packages\\arduino\tools\\arm-none-eabi-gcc\\4.8.3-2014q1\\arm-none-eabi\\include",
                "C:\\Users\\username\\Documents\\ArduinoData\\packages\\arduino\tools\\arm-none-eabi-gcc\\4.8.3-2014q1\\arm-none-eabi\\include\\sys",
                "C:\\Users\\username\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\sam\\1.6.12",
                "C:\\Users\\username\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\sam\\1.6.12\\system\\**",
                "C:\\Users\\username\\Documents\\ArduinoData\\packages\\arduino\\hardware\\sam\\1.6.12\\system\\CMSIS\\Device\\ATMEL",
                "C:\\Users\\username\\Documents\\ArduinoData\\packages\\arduino\\hardware\\sam\\1.6.12\\system\\CMSIS\\Device\\ATMEL\\sam3xa",
                "C:\\Users\\username\\Documents\\ArduinoData\\packages\\arduino\\hardware\\sam\\1.6.12\\system\\CMSIS\\Device\\ATMEL\\sam3xa\\include",
                "C:\\Users\\username\\Documents\\ArduinoData\\packages\\arduino\\hardware\\sam\\1.6.12\\variants\\arduino_due_x\\",
                "${workspaceFolder}\\**",
                "C:\\Users\\username\\Documents\\Arduino\\**",
                "C:\\Users\\username\\Documents\\Arduino\\48VCharger\\**",
                "C:\\Users\\username\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\sam\\1.6.12\\**",
                "C:\\Users\\username\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\**"
  1. Other Configurations in "c_cpp_properties.json"

While I retained the "WindowsSdkVersion", "cStandard", and "cppStandard" settings, I did change "compilerPath", "intelliSenseMode", and added two more entries to "forcedInclude" in order for VS Code to specifically find two Due-specific header files. I ended up with this:

            "windowsSdkVersion": "10.0.17763.0",
            "compilerPath": "C:/Users/username/Documents/ArduinoData/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/arm-none-eabi-gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64",
            "forcedInclude": [
                "C:\\Users\\username\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\sam\\1.6.12\\cores\\arduino\\Arduino.h",
                "C:\\Users\\username\\Documents\\ArduinoData\\packages\\arduino\\hardware\\sam\\1.6.12\\system\\CMSIS\\Device\\ATMEL\\sam3xa\\include\\sam3x8e.h",
                "C:\\Users\\username\\Documents\\ArduinoData\\packages\\arduino\\hardware\\sam\\1.6.12\\system\\CMSIS\\Device\\ATMEL\\sam3xa\\include\\sam3xa.h"
  1. This one probably isn't necessary, but at one point I modified the "arduino.json" file in my project directory to specify COM6 for the serial Monitor, finding that value from the Arduino IDE when that was connected to my Due board. You should be able to do that from VS Code by hitting the F1 key, and typing Arduino, and you'll see the arduino-specifc commands pop up. This file also specifies the folder name that will be used to store your build files (Nice feature!!) , which is created as a folder inside your main project folder.

My project's "arduino.json":

{
    "board": "arduino:sam:arduino_due_x_dbg",
    "sketch": "48VCharger.ino",
    "port": "COM6",
    "output": "build"
}
  1. Modify the name of my Arduino libraries folder.

After completing step 7, I was able to build my project and VS Code was not identifying any problems. However, after doing a binary file compare on the .BIN files between the VSCode process and the Arduino IDE, I noticed the file size was slightly different and the total Arduino memory reported from each process was slightly different. I was able to deduce that the VS Code build was not in fact using the library and header in my project file, even though I used #include "headername.h", and it was instead using the version of that same file in my Arduino "libraries" directory. Since I had customized that library file a small amount, I wanted that version to be included in this project. So I had to rename the "libraries" folder in the main Arduino project folder. Then VS Code built exactly the same output file as the Arduino IDE, and I verified all the data section names and sizes were identical by examining the .elf files. Lesson learned here is that even though the Arduino IDE requires (and recreates) the libraries directory in your main project directory, it does not use that library if you have the same library files defined in your exact project folder, whereas VS Code seems to start looking for "" libraries in the libraries folder, not the project folder.

Anyways, I thought I would share this as I hope it is helpful to someone else.

Good luck!

1 Like

Thank you very much for sharing

hi ssabin you helped me too thanks