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.

Prerequisites

  • 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.

VISUAL_STUDIO_HOW_TO_PL_MP_image2.jpg

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.

VISUAL_STUDIO_HOW_TO_PL_MP_image1.gif

When running Arduino commands like those above you may encounter a firewall message like the one shown below. Select “Allow Access” and continue.

VISUAL_STUDIO_HOW_TO_PL_MP_image3.jpg

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.

VISUAL_STUDIO_HOW_TO_PL_MP_image5.jpg

Click on <Select Programmer> and select AVR ISP (Arduino AVR Board)

VISUAL_STUDIO_HOW_TO_PL_MP_image4.gif

Click on <Select Board Type> and select Arduino/Genuino Uno

VISUAL_STUDIO_HOW_TO_PL_MP_image7.gif

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.

VISUAL_STUDIO_HOW_TO_PL_MP_image6.gif

You can access the Serial Monitor by clicking on the plug symbol on the blue line

VISUAL_STUDIO_HOW_TO_PL_MP_image7.gif

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.

VISUAL_STUDIO_HOW_TO_PL_MP_image7.gif

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.

VISUAL_STUDIO_HOW_TO_PL_MP_image12.gif

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.

VISUAL_STUDIO_HOW_TO_PL_MP_image10.gif

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.

VISUAL_STUDIO_HOW_TO_PL_MP_image11.gif
Paul Lunn
Dr Paul Lunn is a Lecturer in IOT at Coventry University, UK. He enjoys tinkering with music, programming, Arduino, ESP32, and Raspberry Pi's