Learn how you can use the Arduino extension on Visual Studio Code to create programs for your Arduino.
This tutorial is the second in a series of three tutorials looking at software development using Microsoftâs Visual Studio Code (VS Code). If you donât yet have VS Code installed, head over to How to Set Up Visual Studio Code. Here, we will look at development using Microsoftâs Arduino Extension.
- VS Code needs to be installed on your system.
- VS Code requires that the standard Arduino IDE is installed, as it uses some of the libraries included in the IDE.
Installation of Arduino Extension
To install the Arduino extension enter âCntrl+Shift+xâ to display the Extensions viewer and type âvscode-arduinoâ into the search bar. This should return just one result. Press the âInstallâ button to install.
Once the extension is installed, connect an Arduino Uno development board to a USB port of your computer and then restart VS Code â this ensures that everything is installed correctly.
The Command Palette
Not all VS Codeâs functionality is accessible via menus. Instead, VS Code has a command-line-type facility for interfacing with extensions called the Command Palette.
To run the Command Palette, type âCntrl+Shift+pâ to display a drop-down box that you can enter commands in. Type âArduino Examplesâ into the command palette and press return. This will open up a link to the familiar example projects â navigate to â01.Basicsâ and then double click on âBlinkâ to open the world-famous Blink project.
When running Arduino commands like those above you may encounter a firewall message like the one shown below. Select âAllow Accessâ and continue.
Set up Arduino System
We need to change a few system settings before we can start programming. All of these can be accessed via VS Codeâs interface.
Once you have opened up an Arduino .ino file, VS Code reconfigures in an Arduino mode, and gives access to special functionality in its bottom blue margin, as shown in the image below.
Click on <Select Programmer> and select AVR ISP (Arduino AVR Board)
Click on <Select Board Type> and select Arduino/Genuino Uno
To set the serial port that VS Code will communicate with your Arduino Uno we need to open up the Command Palette (Cntrl+Shift+p) and then type âArduino Select Serial Portâ. You should then select which USB port your Uno is connected to.
You can access the Serial Monitor by clicking on the plug symbol on the blue line
This extension includes a package manager, which makes external package installation really easy. Open up the command palette and type âArduino package managerâ. A new tab should appear that features many packages to install just by clicking on a button.
Verify and Upload Your Code
VS Code in Arduino mode provides a couple of convenient icons for you to click for code verification and uploading to your connected Arduino board. These are available in the upper right-hand corner. Either operation will result in a console window appearing at the bottom of VS Code informing you of the outcome.
IntelliSence and Error Checking
IntelliSence is a code completion engine that looks through all of the source code in your project â including external libraries â and then suggests functions and variables as you type your code. When you start typing, a drop-down box will appear giving suggestions based upon the word that you are entering. This is an incredibly useful tool that can speed up your coding and also provides a useful way of remembering what functions are available to you.
Software bugs are always a part of programming. VS Code constantly checks your code as you write. If it detects an error, it underlines it with a red squiggle and offers advice. It also keeps a record of the number of current errors in your project in the bottom right-hand corner of VS Code. Clicking on this provides a summary of errors in your code. Double-clicking on an error notification will take you to the error in your code.