Platformio Home Page

How to Program Arduino Through Visual Studio Code Using PlatformIO

Sometimes coding on the Arduino IDE is not enough. Think of all the tools it lacks: no spell check, no folders, no autocomplete. Sure, you can just program the Arduino on an editor and then paste it into the IDE before uploading. But PlatformIO allows you to do this without having to switch windows at all.

What is PlatformIO?

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

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

Install PlatformIO

  1. In Visual Studio Code, click the Extensions button in the left tray or press control + Transformation + X on the keyboard.
Left drawer for Visual Studio icon extensions
  1. In the search extensions bar, type platformiothen click on the result that says “PlatformIO IDE”.
Search Visual Studio Code Platformio
  1. Click the “Install” button and wait for the installation to finish.
  1. When you succeed, the PlatformIO logo should appear in the left tray area of ​​Visual Studio Code. Click on this banner whenever you want to use PlatformIO.

Coding with PlatformIO

Compared to the Arduino IDE, coding with PlatformIO requires a few extra steps. In this example we will be using the Arduino Uno and making it blink the internal LED.

  1. Click on the PlatformIO icon in the left tray area.
Left Platformio tray
  1. Click Open.
  1. The main PIO page should appear. You will find a quick access bar to the right. Select New Project.
quick access platform
  1. The project wizard should appear. You can type “PlatformIO Blink” in the name text box. Then type or search for “Arduino Uno” in the board dropdown list and select Arduino in the Framework dropdown.
Platformio Project Wizard
  1. my choice: PlatformIO automatically saves your projects to their default folder (DocumentsPlatformIOProjects). You can uncheck the checkbox that says “Use default location” and then select or create your own folder via the explorer that appears below.
Platformio default location not specified
  1. Click Finish when finished. Initialization 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, press “No, I do not trust the authors”. You can always change this option at another time.
Platformio Do you trust this user

Find the location of the code

You should be ready to program the 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 the thing that appears in the Arduino IDE as soon as you open it. There is only one difference: he says #include in line 1.

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

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

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

blink symbol

#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

As in the Arduino IDE, PlatformIO allows you to either build or build an Arduino program and upload it to your board. In the blue bar at the bottom of Visual Studio Code, you should see a check mark and arrow next to the home icon.

Visual Studio Code Platformio Bottom Tray

Check code allows you to generate your code and see if there are any issues while converting it to device 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 allow you to delete the current saved build (trash icon) from your computer and perform a unit test (flask icon) on your board.

Try an external LED lamp

This way, the LED built into the Arduino should start blinking once every second. If you really want to see it working on an external LED you should try placing the LED and a 250 ohm resistor between pin 13 and GND in series.

Ltspice Arduino Led External Schematic

Use the serial monitor

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

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

Visual Studio Code Platformio Serial Monitor

Frequently Asked Questions

How to delete PlatformIO from Visual Code Studio?

Like any other extension, you just have 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 benefit to having separate create and upload buttons?

Although it might seem redundant at first, having a build button that doesn’t load code anywhere helps a lot when you’re trying out some C++ features that I’m not sure will work on an embedded system at all. Ultimately, this depends on your use case. This, and early correction, helps to remove a lot of unwanted errors in the long run.

Can I use PlatformIO for commercial use?

Quick answer: Yes!

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

Is this article helpful?

Subscribe to our newsletter!

Our latest lessons delivered straight to your inbox

#Program #Arduino #Visual #Studio #Code #PlatformIO

Leave a Comment

Your email address will not be published. Required fields are marked *