Enabling Arduino Intellisense with Visual Studio Code
Arduino IDE is a great tool, but if you need to use Arduino more than just blinking LEDs, you most probably will need a better editor that supports intellisense. Visual Studio Code is an open source editor that's already in the market and already being used for many developers from NodeJS to Python projects. It's not a complete IDE so it's not as heavy as other Microsoft products such as Visual Studio Professional. Vscode is just an editor but with large extensions library; it also provides debugging, syntax highlighting, code refactoring in your projects let it be NodeJs, ReactJS, AngularJS, Python, Lua or whatever...
Using Arduino IDE for small projects just as blinking LED or reading from a sensor then manipulating a switch or a relay is an easy job. But if you need to use multiple sensors and outputting devices such as OLED screens or motors, it means you need to use sophisticated 3rd party libraries. Adafruit or U8GLib is a good example for this. If you want to use your own code rather than copy & pasting other people's code you find on the internet, it's essential that you have intellisense.
Adding this functionality is not very hard. But I had some issues which I figured out how to solve. I hope it helps other people too:
VScode is not a complete IDE, so we still need to have Arduino installed in our system. Then we need to install Arduino extension by Microsoft.
After this setting you will see that .ino file extension is enabled for vscode. Now we need to tell vscode the location of the Arduino so need to make some changes using File -> Preferences -> Settings
"arduino.path": "C:\\Program Files (x86)\\Arduino", "arduino.additionalUrls": "", "arduino.logLevel": "info", "arduino.enableUSBDetection": true, "C_Cpp.intelliSenseEngine": "Tag Parser"
If we included 3rd party libraries, they will be in a different folder than we currently work on. We need to tell vscode where it needs to include those file from. Otherwise we will get below error:
If you hover over the light bulb icon, you will see "Edit browse.path settings" button.
Clicking it will create a json file called "c_cpp_properties.json" under .vscode folder of your working directory. In this file you will need to add the path for all the .h files you use.
Now you should see that intellisense working:
After this you can still use Arduino to compile & upload the file to your Arduino, NodeMcu, Wemos, ESP8266 or whatever you're using. But you if you want to use vscode for everything you need to add one more thing. If you look at the bottom right of the editor you will see 2 buttons:
- Select Board Type
- Select Serial Port
If you click them and select your board and ports then you are set to go.
But you won't see compile or upload options in the menu. You will need to click Ctrl + Shift + P
to enable selection menu then click "Arduino: Upload" option to start compiling and uploading the code to your board.