Visual Studio Code

In this chapter, we will install Visual Studio Code with the Arduino extension for writing C++ code to run on the Arduino board. This development environment is optimal for larger projects. As a sketch grows larger and gets more complex it will be increasingly difficult to navigate and search the codebase without a proper code editor. This is where Microsoft’s free Visual Studio Code comes in handy. It supports Windows, Linux, and Mac and can handle many programming languages using Extensions. If you would like to customize the Gbox420 sketch from the GitHub Gbox420 library, you should consider using this dev environment.

Note: You still need the Arduino IDE installed along with Visual Studio Code.

The sample steps are based on a Linux operating system, but the basics apply in any environment be it Windows, Linux, or Mac. Throughout the setup I’m going to be assuming every component is installed under the User profile, inside the Apps folder:

  • Linux: $HOME/Apps/

  • Windows: %userprofile%\Apps\

Some settings do not support the $HOME or %userprofile% shortcuts, there you need to use the full path. This you can get by running:

  • Linux: echo $HOME/Apps/

  • Windows: echo %userprofile%\Apps\

Setup

Visual Studio Code can be downloaded from: https://code.visualstudio.com/download

Platform-specific install guides:

Overview of the user interface: https://code.visualstudio.com/docs/getstarted/userinterface

Important note: You also need the Arduino IDE installed on the computer where Visual Studio Code is running. If you have not done this, please complete the Setup section in the Arduino IDE chapter. Going forward I’ll assume the Arduino IDE environment is installed to:

    • Linux: $HOME/Apps/arduino-X.X.XX

    • Windows: %userprofile%\Apps\arduino-X.X.XX

Arduino extension

Visual Studio (VS) utilizes extensions to add support for different programming languages / platforms. To program an Arduino board you need to install the Arduino extension from Microsoft. Go to the Extensions icon (Ctrl + Shift + X) on the left side Activity Bar and search for the “arduino microsoft”. I recommend reading the short guide displayed in the Details tab.

Once the extension is installed open the Command Palette (Ctrl + Shift + P) and search for Arduino.

A list of Arduino-specific commands should appear. Some frequently used options have keyboard combinations next to their name, like Verify (Ctrl + Alt + R) and Upload (Ctrl + Alt + U). Before we can use these commands first we must generate a few important configuration files that we will need to use in each Arduino sketch we build with Visual Studio.

Environment settings

Create an empty folder under the Apps folder called Environment, then in Visual Studio code select File \ Open Folder and select the empty folder:

  • Linux: $HOME/Apps/Environment

  • Windows: %userprofile%\Apps\Environment

This should look something like this initially, a blank ENVIRONMENT folder:

Bring up the Command Palette by pressing Ctrl + Shift + P, and search for the Arduino Initialize command:

Leave the default suggested app.ino sketch name and press enter to generate a sample Arduino sketch:

You should end up it the most basic app.ino sketch consisting of a setup() and main() functions:

We will be more interested in the .vscode folder that contains the Visual Studio specific settings. As we proceed with the environment setup changes will be saved under this folder:

Setting up the Arduino board

Check out the Status bar at the bottom, you will see clickable <Select> options for setting up the board type, programmer, and the serial port:

Configure them by clicking each:

    • Board: Arduino/Genuino Mega or Mega 2560

    • Processor: ATmega2560 (Mega 2560)

    • Programmer: AVRISP mkII

    • Port: Depends on the USB port the Arduino board is connected to. On Windows COM*, on Linux: /dev/ttyUSB* or /dev/ttyACM*

Finding the port on Linux: Run the below commands while the board is disconnected, and then when it is connected, look for the new entry that pops up:

    • ls -l /dev/ttyU*

    • ls -l /dev/ttyA*

Finding the port on Windows: Open Control Panel -> Device Manager -> Ports(COM & LPT) -> USB-SERIAL CH340 (COM*)

Status bar on a configured Linux:

Status bar on a configured Windows:

These settings will be stored under the .\vscode folder:

    • arduino.json: Stores the Arduino board-specific settings, like board type, port, baud rate..etc

    • c_cpp_properties.json: Stores compiler settings, most importantly the location of the Arduino libraries under “includepath”

    • On Windows the path is different:

We will get back to these files when uploading the Gbox420 sketch, for now just remember that these files are unique to your system and you will need them for compiling and uploading a sketch from Visual Studio Code.

Uploading a sketch

Once the Arduino extension is installed and configured click Files \ Open file (Ctrl + O) and navigate to the location where you installed the Arduino IDE and select the /examples/01.Basics/BareMinimum/BareMinimum.ino (or any other sample sketch).

  • Linux: $HOME/Apps/arduino-X.X.XX/examples/01.Basics/BareMinimum/BareMinimum.ino

  • Windows: %userprofile%\Apps\arduino-X.X.XX\examples\01.Basics\BareMinimum\BareMinimum.ino

Now open the Explorer icon (Ctrl + Shift + E) icon on the left side Activity Bar, you should see a list of all example sketches that got loaded under ARDUINO EXAMPLES category:

Open ARDUINO EXAMPLES\ Built-in Examples \ 01.Basics\ Blink:

You will probably notice on the bottom right status bar that Visual Studio forgot the board-specific settings as soon as a new sketch is opened:

This is because VS stores these settings for each sketch under the .vscode\ arduino.json subfolder. Copy the content of the .vscode folder from the Environment folder created earlier into the example sketch from:

  • Linux: $HOME/Apps/Environment

  • Windows: %userprofile%\Apps\Environment

Now you just need to update the sketch name in the arduino.json file. Update the file to match the main sketch ino file and save it using File \ Save (Ctrl + S):

Open the Command Palette (Ctrl + Shift + P) and try to upload the sketch by selecting Arduino: Upload or pressing Ctrl + Alt + U

Output tab at the bottom you should see something like: [Done] Uploaded the sketch: SKETCHNAME.ino

Serial monitor output

Next, let’s try uploading a sketch that prints something on the Serial Monitor and check how we can view the output. From the Arduino Examples open the Built-in Examples \ 01.Basics \ ReadAnalogVoltage.

Once the sketch is open replace the Environment configuration files under the .vscode folder, and update the sketch file name, just like in the previous section.

Next, we need to set the Serial port baud rate to match our board. Using the sample board this value is 115200. Set this in two places:

    • Inside the example sketch ReadAnalogVoltage.ino:

    • In the Arduino settings under Command Palette (Ctrl + Shift + P) - Arduino: Change Baud Rate

Select the board's baud rate, for the sample board this is 115200:

With the arduino.json and the ReadAnalogVoltage.ino files updated and saved try uploading the sketch (Ctrl + Alt + U):

Now open the Serial Monitor by opening the Command Palette (Ctrl + Shift + P) and searching for Arduino: Open Serial Monitor

You should now see the output of the Arduino board presented on the Serial monitor at the bottom of the screen: