How to program Arduino through Visual Studio Code using PlatformIO

Sometimes coding on the Arduino IDE just isn’t enough. Think of all the tools it lacks: no spell checker, no folders, no autocomplete. Of course, you could just program Arduino on an editor and then paste it into the IDE before uploading it. But PlatformIO lets you do that without having to switch windows at all.

What is PlatformIO?

PlatformIO is an extension for Visual Studio Code that lets you run and debug code for embedded systems. It currently supports 1,395 development boards from all sorts of manufacturers, allowing you to use it for more than the Arduino.

Comparing PlatformIO to the Arduino IDE might sound more like a “use it and you’ll know” sort of thing. In my opinion, it’s definitely better because you can add more features to it, like better C/C++ linters, autocomplete, and even MicroPython support. It also supports Git, which is good if you’re working on a team.

Installing PlatformIO

  1. In Visual Studio Code, click the Extensions button in the left bar or press CTRL + Gap + X on the keyboard.
Visual Studio Code Extensions left bar
  1. In the search extensions bar, type platformiothen click on the result that says “PlatformIO IDE”.
Visual Studio Code Platform Search
  1. Click the “Install” button and wait for the installation to complete.
  1. When successful, the PlatformIO logo should appear in the left status bar area of ​​Visual Studio Code. Tap this logo whenever you want to use PlatformIO.

Coding with PlatformIO

Compared to the Arduino IDE, coding with PlatformIO requires a few extra steps. For this example, we’ll use an Arduino Uno and flash its internal LED.

  1. Tap the PlatformIO icon in the left status bar.
Left deck of the platform
  1. Press Open.
  1. The PIO home page should appear. You will find a quick access bar on the right. Select “New Project”.
Quick access to the platform
  1. A project wizard should appear. You can type “PlatformIO Blink” in the Name text box. Then type or search for “Arduino Uno” in the Board drop-down menu and select Arduino from the Framework drop-down menu.
Platformio Project Assistant
  1. Optional: PlatformIO automatically saves your projects in its default folder (DocumentsPlatformIOProjects). You can uncheck the “Use default location” box, then select or create your own folder via the explorer that appears below.
Unchecked default Platformio location
  1. Click “Finish” when finished. The initialization process may take some time for the first time. It may also ask you if you trust the authors of the files in the folder. If you are only working on your own project, click “Yes, I trust the authors”. Otherwise, tap “No, I don’t trust the authors”. You can always change this option at another time.
Do you trust this user

Find where to code

You should be ready to program your Arduino after pressing this button. To start coding, you have to go to “src -> main.cpp”. This opens a file with the following code:

#include 
 
void setup() {
  // put your setup code here, to run once:
}
 
void loop() {
  // put your main code here, to run repeatedly:
}

Sound familiar? This is what appears on the Arduino IDE as soon as you open it. There is only one difference: it is written #include at line 1.

The reason is simple. You are coding in pure C++ now. PlatformIO shows all the hidden things you never see using the Arduino programming language. This is also why you can see all these new folders on the left. They are there for you to do things.

On the other hand, the Arduino IDE only allows you to program Arduino boards with the Arduino programming language, which is based on C++.

That done, let’s flash the internal LED of the Arduino!

Blink code

#include 
 
void setup() {
  // put your setup code here, to run once:
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
}
 
void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(LED_BUILTIN, HIGH); // turns on the LED.
  delay(500);
  digitalWrite(LED_BUILTIN, LOW); // turns off the LED.
  delay(500);
  Serial.println("One cycle of on and off LED"); // prints a message after a cycle of turning the LED on and off.
}

Upload to Arduino

Like in the Arduino IDE, PlatformIO allows you to build or build and upload your Arduino program to your board. In the blue bar under Visual Studio Code, you should see a checkmark and an arrow next to the house icon.

Visual Studio Code Platformio Bottom Tray

The verification icon allows you to create your code and see if there are any problems converting it to machine code. Meanwhile, the arrow icon allows you to create your current code and then upload it directly to your board (assuming it’s connected to your computer).

The next two buttons let you delete the current saved version (trash can icon) from your computer and do a unit test (flask icon) on your board.

Try an external LED

With that out of the way, your Arduino’s onboard LED should start blinking once per second. If you really want to see it working on an external LED, you should try putting an LED and a 250 Ω resistor between pin 13 and GND in series.

Ltspice Schematic Arduino External Led

Use Serial Monitor

An IDE for embedded systems is not an IDE if it does not have a serial monitor. Tap the plug icon to open the serial monitor. This will open a new terminal process that will show you everything your board wants to tell you.

In this case, our Blink code should make the Arduino say “One cycle of LEDs on and off” to the serial monitor after each blink cycle.

Visual Studio Code Platformio Serial Monitor

Frequently Asked Questions

How do you remove PlatformIO from Visual Code Studio?

Like any other extension, you just need to go to the Extensions tab in the left tray area, click on PlatformIO IDE, and then click on the uninstall button.

Is there a point in having separate Build and Upload buttons?

Although it might seem redundant at first, having a Build button that doesn’t download code anywhere helps a lot when experimenting with some C++ features that you’re not sure would work on an embedded system. Ultimately, it depends on your use case. This, and early debugging, helps eliminate many unwanted long-term bugs.

Can I use PlatformIO for commercial purposes?

Quick answer: yes!

Slightly longer answer: you can program Arduino and many other boards with PlatformIO for your business. But they also offer an in-house IDE solution for businesses in PIO Labs.

Was this article helpful?

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Comments are closed.