How to Program Arduino Through Visual Studio Code Using PlatformIO

If the Arduino IDE doesn't cut it for you, then you should try PlatformIO on Visual Studio Code!

Platformio Home Page

Sometimes, coding on the Arduino IDE is just not enough. Think about all the tools it’s missing: no spell check, no folders, no autocomplete. Sure, you can just program Arduino on an editor and then paste it on the IDE before uploading. 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 kinds of manufacturers, letting you use it for more than just the Arduino.

Comparing PlatformIO to the Arduino IDE can be more like a “use it and you’ll know it” kind 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 work in teams.

Installing PlatformIO

  1. In Visual Studio Code, click the Extensions button on the left tray or press Ctrl + Shift + X on the keyboard.
Visual Studio Code Extensions Left Tray
  1. In the Search Extensions bar, type platformio, then click on the result that says “PlatformIO IDE”.
Visual Studio Code Platformio Search
  1. Click the “Install” button and wait until it finishes installing.
  1. When it’s successful, the PlatformIO logo should show up on Visual Studio Code’s left tray area. Press 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 make it blink its internal LED.

  1. Press the PlatformIO icon on the left tray area.
Platformio Left Tray
  1. Press Open.
  1. The PIO Home page should show up. You’ll find a quick access bar to the right. Select “New Project”.
Platformio Quick Access
  1. A Project Wizard should show up. You can type in “PlatformIO Blink” on the Name textbox. Then type in or look for “Arduino Uno” in the Board dropdown and select Arduino in the Framework dropdown.
Platformio Project Wizard
  1. Optional: PlatformIO automatically saves your projects in its default folder (Documents\PlatformIO\Projects). You can untick the checkbox that says “Use default location” and then select or make your own folder via the explorer that shows up below it.
Platformio Unticked Default Location
  1. Click “Finish” when you’re done. The initialization process may take a while for the first time. It might also ask you if you trust the authors of the files in the folder. If it’s just you working on your own project, then click “Yes, I trust the authors”. Otherwise, press “No, I don’t trust the authors”. You can always change that option at another time.
Platformio Do You Trust This User

Find Where to Code

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

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

Looks familiar? That’s the thing that shows up on the Arduino IDE as soon as you open it. There’s just one difference: it says #include <Arduino.h> at line 1.

The reason is simple. You’re coding with pure C++ now. PlatformIO shows all of the hidden things you never see while using the Arduino programming language. That’s also why you get to see all those new folders to the left. They’re there for you to do stuff on.

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

With that over, let’s make the Arduino blink its internal LED!

Blink Code

#include <Arduino.h>
 
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.
}

Uploading to Arduino

Like in the Arduino IDE, PlatformIO allows you to either Build or Build and Upload your Arduino program to your board. In the blue bar under Visual Studio Code, you should see a check and arrow next to the house icon.

Visual Studio Code Platformio Bottom Tray

The check icon lets you build your code and see if there are any problems while converting it into machine code. Meanwhile, the arrow icon lets you build your current code and then upload it straight to your board (assuming that it’s connected to your computer).

The next two buttons let you delete the current saved build (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 built-in LED should start blinking once every second. If you really want to see it work on an external LED, then you should try putting an LED and a 250 Ω resistor between the pin 13 and GND in series.

Ltspice Schematic Arduino External Led

Use the Serial Monitor

An IDE for embedded systems is not an IDE if it doesn’t have a serial monitor. Press the plug icon to open the serial monitor. It will open a new terminal process that shows you whatever your board wants to tell you.

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

Visual Studio Code Platformio Serial Monitor

Frequently Asked Questions

How do you delete PlatformIO from Visual Code Studio?

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

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

While it may seem redundant at first, having a Build button that doesn’t upload the code anywhere helps a lot when you’re experimenting with certain C++ features you aren’t sure would work on an embedded system at all. In the end, it depends on your use case. That, and early debugging, helps clear a lot of unwanted bugs in the long run.

Can I use PlatformIO for commercial use?

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.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox