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.
VSCode Advantages
- Lots of useful key-stroke sequences, fully programmable.
- Integrated with Git.
- Intellisense that works.
- Jump to definition and mouseover display in-line help.
- Hugely useful on-screen click objects.
- Full support for folder trees.
- Plugin support.
- Seriously have you used the Arduino IDE? Who needs a list?
VSCode Disadvantages
- 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).
Process
Assume you have a working project you want to convert to VSCode. Just
- Run VSCode and open the project folder.
- (optional but recommended) Use the source control tab to initialize a new git repository and check in your existing code.
- From the command palette run: Arduino: Initialize
- 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:
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:
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.
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):
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.
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
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:
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).
vscode «#include errors detected. Please update your includePath
I’m trying to use vscode with arduino but have no success. The problem seems to be something with the libraries path. But I havent been able to fix that ! I’m on linux.
«message»: «#include errors detected. Please update your includePath. IntelliSense features for this translation unit (/home/harold/Arduino/Saaf_Curing/Saaf_Curing.ino) will be provided by the Tag Parser.»,
I don’t know how to find my includePath. I’m not able to do any advices given in vscode.
I wonder if vs code is the right direction at all as it seems complicated ?
6 Answers 6
Although the question mentions Arduino, the following suggestions apply basically any time VSCode tells you to «update your includePath».
What is includePath?
The includePath is an attribute in c_cpp_settings.json , which is in the .vscode folder of the main folder you have opened in VSCode using File → Open Folder.
You can edit c_cpp_settings.json directly, but it is usually easier to use the «C/C++ Configurations GUI». To do that, open the Command Palette (Ctrl+Shift+P) and run «C/C++: Edit Configurations (UI)». Then look for the «Include path» setting.
The includePath tells VSCode (specifically the IntelliSense component of the C/C++ extension) where to look when resolving #include «filename» directives. That allows VSCode to see definitions of symbols defined in those files.
So should I fiddle with includePath when VSCode tells me to?
Not at first! Before changing the include path, if you haven’t already, first set the «Compiler path» to point at your C/C++ compiler, and set «IntelliSense mode» to match the compiler as closely as possible.
You may also need to adjust the Compiler arguments, particularly if the compiler is capable of generating code for multiple targets, for example, both 32-bit and 64-bit code. (If you don’t know what that means, skip it at first.)
Next, in Command Palette, run «C/C++: Log Diagnostics». The output will show you which compiler VSCode found and what it detected as its built-in include path and preprocessor defines.
Then, run these commands in a shell:
Here, I have assumed you are using gcc as your compiler. If not, substitute the actual compiler command name. If your compiler is not a variant of GCC (for example you are using the Microsoft cl.exe compiler), you’ll need to look at its documentation or Google to find switches that print the predefined macros and include paths (e.g., see here for cl.exe ).
Compare the output of the above command to what VSCode shows in its C/C++ diagnostics output. Hopefully they are very similar. If not, try adjusting the Compiler path, IntelliSense mode, or Compiler arguments. Once you’ve gotten them as close as possible by adjusting just those three settings, go on to the next step.
Now adjust includePath if necessary
If there are still significant differences between the compiler built-in configuration and what VSCode detects, fix that by (in the C/C++ settings UI) modifying the Include path, Defines, and C/C++ standard fields. Re-run the C/C++ Log Diagnostics command to see the effects.
It is probably not necessary to add all of the pre-defined preprocessor symbols. This really only matters if there are #ifdef directives that depend on them, and which are causing VSCode to see the wrong code as active. I suggest only adding predefined symbols if, while browsing your code, you see a specific case that VSCode gets wrong.
Finally, if your project has header files in places that the compiler does not search by default, that is, you normally have to pass -I switches on the compiler command line, add those as well to the Include path. The same goes for any -D arguments, which must be added to the Defines.
microsoft/vscode-arduino
Use Git or checkout with SVN using the web URL.
Work fast with our official CLI. Learn more.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Visual Studio Code extension for Arduino
Welcome to the Visual Studio Code extension for Arduino preview ! The Arduino extension makes it easy to develop, build, deploy and debug your Arduino sketches in Visual Studio Code, with a rich set of functionalities. These include:
- IntelliSense and syntax highlighting for Arduino sketches
- Verify and upload your sketches in Visual Studio Code
- Built-in board and library manager
- Built-in example list
- Built-in serial monitor
- Snippets for sketches
- Automatic Arduino project scaffolding
- Command Palette ( F1 ) integration of frequently used commands (e.g. Verify, Upload. )
- Integrated Arduino Debugging New
Either the Arduino IDE or Arduino CLI are required.
The Arduino IDE can be installed the Arduino download page.
- The supported Arduino IDE versions are 1.6.x and later.
- The Windows Store’s version of the Arduino IDE is not supported because of the sandbox environment that the application runs in.
- Note: Arduino IDE 1.8.7 had some breaking changes, causing board package and library installation failures. These failures were corrected in 1.8.8 and later.
The Arduino CLI can be downloaded from the repository’s release page
- The extension has only been tested with v0.13.0.
- If you use the CLI you will have to set arduino.path since the CLI does not have a default path.
Open VS Code and press F1 or Ctrl + Shift + P or Cmd + Shift + P to open command palette, select Install Extension and type vscode-arduino .
Or launch VS Code Quick Open ( Ctrl + P or Cmd + P ), paste the following command, and press enter.
You can also install directly from the Marketplace within Visual Studio Code, searching for Arduino .
You can find code samples and tutorials each time that you connect a supported device. Alternatively you can visit our IoT Developer Blog Space or Get Started Tutorials.
This extension provides several commands in the Command Palette ( F1 or Ctrl + Shift + P or Cmd + Shift + P ) for working with *.ino files:
- Arduino: Board Manager: Manage packages for boards. You can add 3rd party Arduino board by configuring Additional Board Manager URLs in the board manager.
- Arduino: Change Baud Rate: Change the baud rate of the selected serial port.
- Arduino: Change Board Type: Change board type or platform.
- Arduino: Change Timestamp Format: Change format of timestamp printed before each line of Serial Monitor output.
- Arduino: Close Serial Monitor: Stop the serial monitor and release the serial port.
- Arduino: Examples: Show list of examples.
- Arduino: Initialize: Scaffold a VS Code project with an Arduino sketch.
- Arduino: Library Manager: Explore and manage libraries.
- Arduino: Open Serial Monitor: Open the serial monitor in the integrated output window.
- Arduino: Select Serial Port: Change the current serial port.
- Arduino: Send Text to Serial Port: Send a line of text via the current serial port.
- Arduino: Upload: Build sketch and upload to Arduino board.
- Arduino: CLI Upload: Upload complied code without building sketch (CLI only).
- Arduino: Upload Using Programmer: Upload using an external programmer.
- Arduino: CLI Upload Using Programmer: Upload using an external programmer without building sketch (CLI only).
- Arduino: Verify: Build sketch.
- Arduino: Rebuild IntelliSense Configuration: Forced/manual rebuild of the IntelliSense configuration. The extension analyzes Arduino’s build output and sets the IntelliSense include paths, defines, compiler arguments accordingly.
- Arduino: Upload Alt + Cmd + U or Alt + Ctrl + U
- Arduino: Verify Alt + Cmd + R or Alt + Ctrl + R
- Arduino: Rebuild IntelliSense Configuration Alt + Cmd + I or Alt + Ctrl + I
Option | Description |
---|---|
arduino.path | Path to Arduino, you can use a custom version of Arduino by modifying this setting to include the full path. Example: C:\\Program Files\\Arduino for Windows, /Applications for Mac, /home/ /Downloads/arduino-1.8.1 for Linux. (Requires a restart after change). The default value is automatically detected from your Arduino IDE installation path. |
arduino.commandPath | Path to an executable (or script) relative to arduino.path . The default value is arduino_debug.exe for Windows, Contents/MacOS/Arduino for Mac and arduino for Linux, You also can use a custom launch script to run Arduino by modifying this setting. (Requires a restart after change) Example: run-arduino.bat for Windows, Contents/MacOS/run-arduino.sh for Mac and bin/run-arduino.sh for Linux. |
arduino.additionalUrls | Additional Boards Manager URLs for 3rd party packages as a string array. The default value is empty. |
arduino.logLevel | CLI output log level. Could be info or verbose. The default value is «info» . |
arduino.clearOutputOnBuild | Clear the output logs before uploading or verifying. Default value is false . |
arduino.allowPDEFiletype | Allow the VSCode Arduino extension to open .pde files from pre-1.0.0 versions of Arduino. Note that this will break Processing code. Default value is false . |
arduino.enableUSBDetection | Enable/disable USB detection from the VSCode Arduino extension. The default value is true . When your device is plugged in to your computer, it will pop up a message » Detected board ****, Would you like to switch to this board type «. After clicking the Yes button, it will automatically detect which serial port (COM) is connected a USB device. If your device does not support this feature, please provide us with the PID/VID of your device; the code format is defined in misc/usbmapping.json .To learn more about how to list the vid/pid, use the following tools: https://github.com/EmergingTechnologyAdvisors/node-serialport npm install -g serialport serialport-list -f jsonline |
arduino.disableTestingOpen | Enable/disable automatic sending of a test message to the serial port for checking the open status. The default value is false (a test message will be sent). |
arduino.skipHeaderProvider | Enable/disable the extension providing completion items for headers. This functionality is included in newer versions of the C++ extension. The default value is false . |
arduino.defaultBaudRate | Default baud rate for the serial port monitor. The default value is 115200. Supported values are 300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400 and 250000 |
arduino.defaultTimestampFormat | Format of timestamp printed before each line of Serial Monitor output. You can find list of all available placeholders here. |
arduino.disableIntelliSenseAutoGen | When true vscode-arduino will not auto-generate an IntelliSense configuration (i.e. .vscode/c_cpp_properties.json ) by analyzing Arduino’s compiler output. |
arduino.analyzeOnOpen | When true, automatically run analysis when the project is opened. Only works when arduino.analyzeOnSettingChange is true. |
arduino.analyzeOnSettingChange | When true, automatically run analysis when board, configuration, or sketch settings are changed. |
The following Visual Studio Code settings are available for the Arduino extension. These can be set in global user preferences Ctrl + , or Cmd + , or workspace settings ( .vscode/settings.json ). The latter overrides the former.
Note: You only need to set arduino.path in Visual Studio Code settings, other options are not required.
The following settings are as per sketch settings of the Arduino extension. You can find them in .vscode/arduino.json under the workspace.
- sketch — The main sketch file name of Arduino.
- port — Name of the serial port connected to the device. Can be set by the Arduino: Select Serial Port command. For Mac users could be «/dev/cu.wchusbserial1420».
- board — Currently selected Arduino board alias. Can be set by the Arduino: Change Board Type command. Also, you can find the board list there.
- output — Arduino build output path. If not set, Arduino will create a new temporary output folder each time, which means it cannot reuse the intermediate result of the previous build leading to long verify/upload time, so it is recommended to set the field. Arduino requires that the output path should not be the workspace itself or in a subfolder of the workspace, otherwise, it may not work correctly. By default, this option is not set. It’s worth noting that the contents of this file could be deleted during the build process, so pick (or create) a directory that will not store files you want to keep.
- debugger — The short name of the debugger that will be used when the board itself does not have a debugger and there is more than one debugger available. You can find the list of debuggers here. By default, this option is not set.
- prebuild — External command which will be invoked before any sketch build (verify, upload, . ). For details see the Pre- and Post-Build Commands section.
- postbuild — External command to be run after the sketch has been built successfully. See the afore mentioned section for more details.
- intelliSenseGen — Override the global setting for auto-generation of the IntelliSense configuration (i.e. .vscode/c_cpp_properties.json ). Three options are available:
- «global» : Use the global settings (default)
- «disable» : Disable the auto-generation even if globally enabled
- «enable» : Enable the auto-generation even if globally disabled
- buildPreferences — Set Arduino preferences which then are used during any build (verify, upload, . ). This allows for extra defines, compiler options or includes. The preference key-value pairs must be set as follows:
Pre- and Post-Build Commands
On Windows the commands run within a cmd -, on Linux and OSX within a bash -instance. Therefore your command can be anything what you can run within those shells. Instead of running a command you can invoke a script. This makes writing more complex pre-/post-build mechanisms much easier and opens up the possibility to run python or other scripting languages. The commands run within the workspace root directory and vscode-arduino sets the following environment variables: VSCA_BUILD_MODE The current build mode, one of Verifying , Uploading , Uploading (programmer) or Analyzing . This allows you to run your script on certain build modes only. VSCA_SKETCH The sketch file relative to your workspace root directory. VSCA_BOARD Your board and configuration, e.g. arduino:avr:nano:cpu=atmega328 . VSCA_WORKSPACE_DIR The absolute path of your workspace root directory. VSCA_LOG_LEVEL The current log level. This allows you to control the verbosity of your scripts. VSCA_SERIAL The serial port used for uploading. Not set if you haven’t set one in your arduino.json . VSCA_BUILD_DIR The build directory. Not set if you haven’t set one in your arduino.json .
For example under Windows the following arduino.json setup
vscode-arduino auto-configures IntelliSense by default. vscode-arduino analyzes Arduino’s compiler output by running a separate build and generates the corresponding configuration file at .vscode/c_cpp_properties.json . vscode-arduino tries as hard as possible to keep things up to date, e.g. it runs the analysis when switching the board or the sketch.
It doesn’t makes sense though to run the analysis repeatedly. Therefore if the workspace reports problems («squiggles») — for instance after adding new includes from a new library — run the analysis manually:
Manual rebuild: Arduino: Rebuild IntelliSense Configuration, Keybindings: Alt + Cmd + I or Alt + Ctrl + I
When the analysis is invoked manually it ignores any global and project specific disable.
vscode-arduino’s analysis stores the result as a dedicated IntelliSense-configuration named Arduino . You have to select it from the far right of the status bar when you’re in one of your source files as shown here:
This system allows you to setup and use own IntelliSense configurations in parallel to the automatically generated configurations provided through vscode-arduino. Just add your configuration to c_cpp_properties.json and name it differently from the default configuration ( Arduino ), e.g. My awesome configuration and select it from the status bar or via the command palette command C/C++: Select a Configuration.
Debugging Arduino Code preview
Before you start to debug your Arduino code, please read this document to learn about the basic mechanisms of debugging in Visual Studio Code. Also see debugging for C++ in VSCode for further reference.
Make sure that your Arduino board can work with STLink, Jlink or EDBG. The debugging support is currently fully tested with the following boards:
Steps to start debugging:
- Plug in your board to your development machine properly. For those boards that do not have an on-board debugging chip, you need to use a STLink or JLink connector.
- Go to the Debug View ( Ctrl + Shift + D or Cmd + Shift + D ). and set breakpoints in your source files.
- Press F5 to select your debugging environment.
- When your breakpoint is hit, you can see variables and add expression(s) to watch on the Debug Side Bar.
To learn more about how to debug Arduino code, visit our team blog.
See the Change log for details about the changes in each version.
Supported Operating Systems
Currently this extension supports the following operating systems:
- Windows 7 and later (32-bit and 64-bit)
- macOS 10.10 and later
- Ubuntu 16.04
- The extension might work on other Linux distributions, as reported by other users, but without guarantee.
You can find the full list of issues on the Issue Tracker. You can submit a bug or feature suggestion, and participate in community driven discussions.
To run and develop, do the following:
- git clone https://github.com/microsoft/vscode-arduino
- cd vscode-arduino
- Run npm i
- Run npm i -g gulp
- Open in Visual Studio Code ( code . )
- Press F5 to debug.
To test, press F5 in VS Code with the «Launch Tests» debug configuration.
Code of Conduct
This project has adopted the Microsoft Open Source Code of Conduct. For more information please see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
The Microsoft Enterprise and Developer Privacy Statement describes the privacy statement of this software.
This extension is licensed under the MIT License. Please see the Third Party Notice file for additional copyright notices and terms.
If you would like to help build the best Arduino experience with VS Code, you can reach us directly at gitter chat room.