I've recently started developing for Arduino. Initially I used the Arduino IDE but I soon realised it was not up to par. Development was considerably slower with the need for IDE restarts every now and then. I found VS Code with the Arduino Extension which I've come to love. However I have a few issues and I'm not sure how to solve it.
First of all vs code throws #include errors and asks me to update IntelliSense. However it builds/uploads and runs perfectly well, it also finds the classes etc. defined in said includes so it looks like it's a false positive in some way (i.e. the path is included in includePath settings). Reading the error message also shows it having issues finding a header referenced in Arduino.h called "avr/pgmspace.h". I'm unsure if these errors are related. pgmspace.h is nowhere to be found (it should have been included in the Arduino SDK).
Finally because of the #include error anything related to that particular header file will not be highlighted properly and is just plain gray text which is a bit annoying.
Anyone knows how to fix this? I'm on a Mac btw.
Open VS Code, then open your Arduino library folder by navigating to File > Open Folder.... (or tap CTRL+K then CTRL+O.) Then select the Arduino library folder you'd like to open in the IDE. It should be the library's top-level directory, where "src" and "examples" directories are contained.
Place your caret on the first line of any C# or Visual Basic file. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select Add file header. To apply the file header to an entire project or solution, select Project or Solution under the Fix all occurrences in: option.
The accepted answer didn't work for me. Can't find nor create c_cpp_properties.json file. Also, I wanted it to be global, and not only to one project/workspace/folder.
So, for VSCode 1.14 (2019) I just navigate till settings.json (the global one), and add this json section:
"C_Cpp.default.includePath": [
"C:/Program Files (x86)/Arduino/libraries/**",
"C:/Program Files (x86)/Arduino/hardware/arduino/avr/cores/arduino/**",
"C:/Program Files (x86)/Arduino/hardware/tools/avr/avr/include/**",
"C:/Program Files (x86)/Arduino/hardware/tools/avr/lib/gcc/avr/5.4.0/include/**",
"C:/Program Files (x86)/Arduino/hardware/arduino/avr/variants/standard/**",
"C:/Users/<YOUR USERNAME>/.platformio/packages/framework-arduinoavr/**",
"C:/Users/<YOUR USERNAME>/Documents/Arduino/libraries/**",
"{$workspaceFolder}/libraries/**",
"{$workspaceFolder}/**"
],
"C_Cpp.intelliSenseEngine": "Tag Parser"
Posted another answer with the entire procedure and all details about the approach: Visual Studio Code includePath
Update: Extra libraries do not need to be installed. IntelliSense can operate using only the headers installed by the Arduino app, but a few others may help. More updates below.
When VSCode builds, it uses the SDK. However, IntelliSense cannot read the SDK files to operate (as far as I can tell), which throws these annoying errors and eliminates most code completion capabilities.
Both includePath
and browse.path
need to be configured. includePath
does not include recursively (but that feature seems to be coming soon). browse.path
is recursive, but including the exact location of header files is in includePath
is still necessary for IntelliSense features. browse.path
will use the Tag Parser to provide tools such as the "lightbulb" that you can click to help resolve your includePath
issue. (Source: What is the difference between "includePath" and "browse.path" in c_cpp_properties.json?)
avr/pgmspace.h
is located at: /Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include/avr/pgmspace.h
. It's coded into the libraries as avr/pgmspace.h
; for this reason, we need to include the path that avr
is located in.
"includePath": [
"${workspaceFolder}/libraries",
"/System/Library/Frameworks/Kernel.framework/Versions/A/Headers",
"/Applications/Arduino.app/Contents/Java/libraries",
"/Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include",
"/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/variants/standard"
],
"browse": {
"limitSymbolsToIncludedHeaders": false,
"path": [
"/System/Library/Frameworks/Kernel.framework/Versions/A/Headers",
"/Applications/Arduino.app/Contents/Java/"
]
},
"forcedInclude": [
"/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h"
],
Revise includePath
to look like this:
"includePath": [
"{$workspaceFolder}/libraries",
"C:/Program Files (x86)/Arduino/hardware/tools/avr/lib/gcc/avr/5.4.0/include",
"C:/Program Files (x86)/Arduino/hardware/arduino/avr/cores/arduino",
"C:/Program Files (x86)/Arduino/hardware/arduino/avr/variants/standard",
"C:/Program Files (x86)/Arduino",
],
If you can get a version of any missing library, you can probably put in in your libraries
folder of your project if your includePath
includes "${workspaceFolder}/libraries"
. Make sure to namespace your libraries appropriately, e.g. libraries/avr/pgmspace.h
. I have not tested this method.
Updates:
vscode-arduino
libraries are not necessary on Windows 10.Source and further tips: Enabling Arduino Intellisense with Visual Studio Code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With