Use Visual Studio Code for Arduino

VSCode has an Arduino plugin available in preview. Use it. Put your Arduino desktop application in cold-storage. Comparing the two is like comparing a beaver with a 2 ton backhoe.

 
Visual Studio Code with an Arduino App

VSCode Advantages

  1. Lots of useful key-stroke sequences, fully programmable.
  2. Integrated with Git.
  3. Intellisense that works.
  4. Jump to definition and mouseover display in-line help.
  5. Hugely useful on-screen click objects.
  6. Full support for folder trees.
  7. Plugin support.
  8. Seriously have you used the Arduino IDE? Who needs a list?

VSCode Disadvantages

  1. For now you can’t type into the serial monitor window. That’s not cool. — Update: the latest version of the Arduino plugin now lets you send a string to the serial monitor. It’s clumsier than a modem program but very usable.

Getting Started

Prerequisites

  • A working version of Arduino desktop that can build.
  • Visual Studio Code with Microsoft’s Arduino Extension installed. Install it from the Extensions tab (below the debug button on the far left).
 
The Arduino Extension (per-workspace enabled)

Process

Assume you have a working project you want to convert to VSCode. Just

  1. Run VSCode and open the project folder.
  2. (optional but recommended) Use the source control tab to initialize a new git repository and check in your existing code.
  3. From the command palette run: Arduino: Initialize
  4. Edit the project files (see below) for any workspace-specific project variables (such as includes).

Taking Advantage of Visual Studio Code with Arduino

Once the Arduino extension is enabled for your project there are some tricks to make life better.

Use a src folder to hold your code folders

Strangely, arduino doesn’t support subfolders (it won’t automatically build and link them) unless they begin with a src subfolder root. See here: Recursive src Folder Discussion

Take a look at the picture at the start and make sure your source tree looks like:

ArduinoRootFolder
--- src
------- FolderA
------- FolderB
-------------SubfolderA

Add an ‘output’ option to arduino.json

Depending on how the plugin works today, you may not get an output option defined in the arduino.json settings file. Add this line just above the closing brace:

,"output": "build"

Here I’ve defined the output folder as build. It will create a build folder in the project root where the caching of all the Arduino stuff takes place. That has two advantages:

  • It will decrease build time substantially. Strangely if you don’t set output you don’t get caching.
  • Since Arduino is kind of bad at figuring out caches you can just delete the entire build folder contents to force a clean build.

Warning: from what I can tell putting the build folder inside your project tree sometimes causes issues. This is documented in the Arduino source. A better solution is to build in a folder outside of the project.

Investigate the buttons at the bottom

In typical new-age dumb software fashion there are lots of useful buttons on the bottom of the display that aren’t obvious but that you should totally learn to use.

 
The bottom stripe on Visual Studio Code showing two mouse-over tooltips.

Mouse over them and you’ll see all sorts of easy ways to change stuff.

Visual Studio Code Settings for Arduino

The meat of the matter. There are four project settings files related to visual studio code and to Arduino. They can be confusing and you can often change them in more than one place. I’m going to skip tasks.json but discuss the rest.

Start by running Arduino Initialize from the Command Palette (Ctrl-Shift-P)

This will create a .vscode folder if there isn’t one. It will also add the current Arduino settings to a file named arduino.json placed into the .vscode folder.

The following discussion refers to files found in the .vscode folder in your project. Add them there if they don’t exist.

arduino.json

This contains Arduino-specific settings. Most are set when you make changes with buttons and the Command Palette. Only the “output” folder did I notice as needing manual maintenance.

Add to your arduino.json (see above):

”output”: “build”

c_cpp_properties.json

This file determines what gets linked and how intellisense finds files. Builds will still work but your mouseover helps and go-to-definitions and etc will be wrong if this is wrong.

{
  "configurations": [
    {
      "name": "Win32",
      "includePath": [
        "${workspaceRoot}",
        "C:/Sr/Sketch/hardware/espressif/esp32/cores/esp32",
        "C:/Sr/Sketch/hardware/espressif/esp32/libraries",
        "C:/msys32/home/Mark/esp/esp-idf/components/esp32/include",
        "C:/Sr/Sketch/libraries",
        "C:/Users/Mark/AppData/Local/Arduino15/packages"
      ],
      "browse": {
        "limitSymbolsToIncludedHeaders": false,
        "path": [
          "${workspaceRoot}",
          "C:/Sr/Sketch/hardware/espressif/esp32/cores/esp32",
          "C:/Sr/Sketch/hardware/espressif/esp32/libraries",
         "C:/msys32/home/Mark/esp/esp-idf/components/esp32/include",
          "C:/Sr/Sketch/libraries",
          "C:/Users/Mark/AppData/Local/Arduino15/packages"
        ]
      },
      "intelliSenseMode": "msvc-x64"
    }
  ],
  "version": 3
}

My c_cpp_properties.json starts with workspaceRoot and the next three lines point at the standard ESP32 libraries (since this projects uses an ESP32) and then generically points at the default sketch library set and the local arduino packages. C:/Sr/Sketch is my Arduino Sketch folder.

Note the Local Appdata folder. This is the standard Windows location (with my name Mark). In many systems this folder is hidden or marked ‘system’ and in either case you won’t see it in a browse tree. You can type cd %appdata% to go the remote appdata folder.

settings.json

This is the normal VSCode settings file. You can change it in workspace settings (File / Preferences / Settings and then the Workspace Settings tab) or you can use an editor.

User Settings for Arduino

“arduino.enableUSBDetection”: false

Turn off enableUSBDetection or it will (at least in my case) constantly nag you to change your board to what it thinks it reads from USB. If you mistakenly click Ok it will delete your current settings and use that incorrect driver to boot (sigh). So, turn it off.

Workspace Settings for Arduino

I set the external boards URL for the ESP32 supported boards:

"arduino.additionalUrls":"C:/Sr/Sketch/hardware/espressif/esp32/package/package_esp32_index.template.json",

Tricks

How to Remove a Board Library

It’s not obvious how to get rid of a library (or two) and sometimes arduino seems to choke. The easiest thing to do is to delete most of the contents of the arduino appdata folder (look in %appdata%\..\local for an Arduino15 folder). One of these files is the settings. Keep that.

After you delete the rest of the files all the installed board libraries will disappear and you get to start over (usually a good thing).