Arduino Visual Studio Code: Arduino IDE vs. PlatformIO

Published By

Arduino boards and the Arduino IDE have revolutionized the way hobbyists and builders with minimal training can create electronics at home.

However, you may eventually find this IDE limiting, especially if you're used to programming in more full-featured environments. The good news is that you have other options, including the Visual Studio Code editor paired with the PlatformIO extension.

Like the Arduino IDE, Visual Studio Code (VSC) runs on Windows, Mac, and Linux, and is free to use. While not technically open-source software, it claims to be "built on open source," based on Code – OSS. You can examine how this all shakes out in the VSC FAQ, but know that you can use it for both private or commercial uses.

To get VSC working with Arduino (and a host of other dev boards), you'll also need the PlatformIO (PIO) plugin. PIO represents "a new generation ecosystem for embedded development," and is itself open source.

 

How to Install Arduino IDE

While you won't be using the Arduino IDE, you'll still need to have it installed. If you don't have it yet, navigate to Arduino's software page and download the appropriate package for your system. Windows users should use the Windows Installer or Zip file, not the Windows app.

From there, follow this process:

1. Navigate to the VSC page, download the appropriate installer, and run the program

2. Click on the extension "boxes" icon and type in "PlatformIO" to find that package

3. Install, and after a reset, you'll be greeted with "Welcome to PlatformIO," where you can start a "+ New Project" or "Import Arduino Project." I also installed Arduino for VSC under extensions, but you may not need to. If you get stuck, you can watch a video to learn more about the installation and usage process

 

Arduino IDE: First Tests

Blinking an LED is the traditional "hello world" of hardware hacking, so I decided to copy the example code straight from the Arduino IDE using this process:

1. Navigate to the PIO homepage and click on the "+ New Project" Icon

2. Make up a name for your project and select your board and other information

3. Click on "Finish" and wait a few seconds to set everything up. The name will show up in the explorer section as a drop-down menu with several subdirectories and files inside

4. Choose the "src" directory, which contains the "main.cpp"s file. This .cpp extension stands for "C++," and substitutes in for the .ino extension typical in Arduino programs

5. Click on that extension, and you'll see the standard blank "setup()" and "loop()" routines, but there's also a statement at the top that notes "#include <Arduino.h>." You'll need this line in any program you write in PIO

6. Copy and paste the example code in, keeping the #include <Arduino.h> statement

7. Plug your Uno in via USB, then press the right arrow key on the bottom blue strip to compile and load everything

You should see the light blinking away happily at the rate you've programmed.

 

VSCode Advantages


ArduinoVSCode_IMAGE 2


Unlike the Arduino Uno IDE, the system can detect the COM port that your Uno is using. Since you set up the board type at the beginning of the project, there's no fumbling about with whether you're using a Nano, Uno, or another board. Given how many times I (and others) have made mistakes here, this ability is almost magical.

If you decide midway through your project that you'd rather use a Mega, Nano, or other board instead of an Uno, you'll have to change things around in the "platformio.ini" file in your project. This file lists the platform, board, and framework, and provides the data needed when uploading files.

To test out something a little less standard, I decided to try this out with an Arduino Nano Every and an Arduino Nano 33 IoT. As they are both relatively new boards―and I'd had some recent trouble connecting to one or both―I had my doubts about it being able to auto-connect. To my pleasant surprise, after plugging in the correct configuration for each board, they both came to life and obediently blinked away. Configuration info for a vast array of devices are available here; remember the name may be slightly different than you expect, so don't let that throw you—e.g., it lists a "NANO 33 IOT" instead of an "Arduino Nano 33 IoT".

When you need to use the serial monitor, it's available via a plug icon in the lower blue strip. Press this button, and you'll see a list of available ports. Select the serial monitor by clicking in the terminal area, then entering the corresponding device number.


ArduinoVSCode_IMAGE 3

 

VSCode IntelliSense: Auto-Complete

While you may have commands memorized with the correct capitalization rules and syntax, many users still need to look at examples or do a web search for something they've written. Such is the world of computer programming, where you need to input commands exactly for them to work. The good news is that computers can, in fact, read our minds, or at least use IntelliSense code completion to guess what you're thinking using the first few characters you enter.

When you start typing in a command such as "Serial.pr..." it puts possibilities like "println" in a dialog box that you can accept by hitting tab or enter. It's a small thing, but when this occurs on hundreds or even thousands of instances, the time savings can be immense.

 

Visual Studio Code: Switch Tabs & Navigation

Like your favorite web browser, VSC/PIO allows you to keep several code tabs open at one time, letting you flip back and forth to compare and edit at will. In addition to this tabbed design, there's a little "radar view" screen in the upper-right hand corner of the IDE that shows you a compressed picture of the code.

 

Visual Studio Error Code: Instant Correction

In the Arduino IDE, you write code, press compile or upload, then wait for it to tell you exactly why what you've written won't work. VSC/PIO highlights errors instantly, just as a word processor would. After you clear any errors, hit the build checkmark or upload arrow.

 

Drawbacks of VSC/PIO

The program crashed a few times during testing, and while the included serial monitor seems to be powerful, it can be challenging to figure out. VSC/PIO also doesn't have a readily-accessible set of examples to draw from like the official Arduino IDE. On the other hand, if you're setting up PIO, you probably have some experience in the area, so you can either search for examples or refer back to the Arduino IDE when you get stuck.

You'll also need to migrate your existing code to the new system. PIO has a built-in function to "Import Arduino Project" that helps the process along.

 

VSC/PIO Alternatives

Of course, there are several alternatives to VSC/PIO. The PIO plugin is available for the Atom text editor if you prefer that environment. You can also use Eclipse or Netbeans with Arduino boards, and there's even a Command Line Interface if you're comfortable using a terminal.


ArduinoVSCode_IMAGE 4


For a hybrid option, go into File – Preferences in the Arduino IDE and check "Use external editor". Once activated, you'll see the code greyed out on the Arduino IDE. If you use an editor like Notepad++, you can change things around and upload as usual.

Instead of the stock Arduino serial monitor, try using something like PuTTY to look for feedback from your board. To do so, you'll need to check "Implicit CR in every LF" (at least under Windows) and select Serial and the appropriate COM port. This can be an excellent alternative if you need to monitor two COM ports simultaneously (which is a bit tricky with the Arduino IDE).

 

Final Thoughts

If you're relying on Arduino boards for simple tasks like blinking an LED or two, the Arduino IDE will do the job. Once you move on to working with multiple files and managing large projects, this processing environment becomes unwieldy. Any new environment comes with a learning curve, but the benefits are potentially enormous.

 

 

Related news articles

Latest News

Sorry, your filter selection returned no results.

We've updated our privacy policy. Please take a moment to review these changes. By clicking I Agree to Arrow Electronics Terms Of Use  and have read and understand the Privacy Policy and Cookie Policy.

Our website places cookies on your device to improve your experience and to improve our site. Read more about the cookies we use and how to disable them here. Cookies and tracking technologies may be used for marketing purposes.
By clicking “Accept”, you are consenting to placement of cookies on your device and to our use of tracking technologies. Click “Read More” below for more information and instructions on how to disable cookies and tracking technologies. While acceptance of cookies and tracking technologies is voluntary, disabling them may result in the website not working properly, and certain advertisements may be less relevant to you.
We respect your privacy. Read our privacy policy here