The Arduino IDE has become the first programming experience for many people. While it gets the job done, it lacks key features found in most modern code editors.

Visual Studio Code (VS Code) has quickly become one of the most popular code editors but does not support Arduino development as a standard.

Enter PlatformIO, an open source tool for programming Arduino boards (and more)! Learn how to get PlatformIO and use it to create a simple Arduino sketch in VS Code.

Project requirements

You will need:

  • Arduino compatible board. This tutorial uses a nano-clone.
  • The Arduino IDE is available from the Arduino website (free).
  • Visual Studio Code, available from Microsoft (free).

Today’s guide is for Windows 10, but it should also work on Linux and Mac.

Note. Arduino IDE version for Windows 10 will not work with this guide.

How to install PlatformIO

VS Code has many extensions to help programmers, and PlatformIO is no different. Open the VS Code Extensions Marketplace from the left pane and find PlatformIO IDE . Click «Install» and be prepared for a short wait.

Note . Do not close VS Code during this process, as this may result in installation errors.

PlatformIO extension in VS Code

Once the installation is complete, a prompt will appear in the bottom right corner to reload VS Code. Click on it and the program will restart showing the home page of the PlatformIO extension.

Project setup

The PlatformIO home page has several options, including opening existing Arduino projects and links to project ideas. Today you will start from scratch, so click » New project» .
PlatformIO homepage

You will be prompted to enter a name for your project and the board you will be using. This tutorial uses an Arduino Nano; PlatformIO supports over 650 boards, so you’ll most likely find yours on the list.

Tab Framework should automatically fill in as arduino, if you are using an Arduino compatible board. Leave the checkbox » Location» filled in to use the default installation location. You only need to uncheck this box if you are working with a previously made Arduino project.
PlatformIO new project window

Click Ready and be prepared for a short wait. PlatformIO will download and install all the dependencies needed for the project and restart the IDE when it’s finished.
PlatformIO Initialization

PlatformIO Workflow

Once VS Code restarts, you will see the new project folder open in the panel explorer . At this point it is not necessary to know what each file in that directory does, you will use the directory SRC only for your Arduino sketches.

PlatformIO project structure

You’ll also notice a few new icons in the bottom bar of VS Code. This is the equivalent of the buttons in the Arduino IDE — a checkbox for compiling, an arrow for downloading, and a blank for the serial monitor.

New icons in the bottom toolbar of VS Code

Now that everything is set up, let’s get started coding!

Empty program

PlatformIO creates an empty sketch with every new project.

Open it in the Explorer tab by going to SRC/main.cpp, which will open the thumbnail. You will notice that the file has a different extension than the normal Arduino sketches. CPP stands for C Plus Plus (C++), the programming language used by the Arduino language and IDE.

Note. AT this tutorial uses a working sketch, as it is familiar to those with an Arduino IDE background. However, C++ sketches are commonly referred to as programs .

Here you will see some familiarity — the sketch has functions settings and cycle, like a normal Arduino sketch. The main difference is at the top of the program. header #include must be present in every PlatformIO project for it to work.

main.cpp file for Arduino sketches in PlatformIO

Now let’s set up the Hello World hardware world — a blinking LED sketch.

Flashing LEDs

You will be creating this Blink sketch, we will go through each line below.

Simple Blink Sketch for Arduino

Start by defining your built-in LED pin. If you are using a board that does not use pin 13 for the built-in LED, change it accordingly:

#define onboard 13 

Use your setting function to set the output mode.

 pinMode(onboard,OUTPUT); 

Notice how VS Code prompts you for the code and complete it when you press the key Enter !

VS Code suggests and complements the code

Finally, create the blinking LED logic in the loop function by setting the pin HIGH and LOW With delay .

  digitalWrite(onboard, LOW); delay(1000); digitalWrite(onboard, HIGH); delay(1000); 

If you have any errors, VS Code will let you know during coding rather than waiting for you to compile or upload your code. Live error checking and completion makes coding less expensive and faster!

Next, you’ll learn how to upload your sketch to your board.

Board upload

If you haven’t already, plug your board into a USB port. Save the sketch and click the icon ticks on the bottom panel to compile it, or click the button with arrow to compile and upload the sketch in one go. After a short wait, your LED should flash!

Arduino Nano clone with blinking LED
You may have noticed a step that is missing from the normal Arduino workflow. Usually you need to specify which COM- port is connected to your Arduino. If you scroll through the output while uploading the sketch to the board, you will notice that PlatformIO will automatically detect the board for you.

Output loading I/O platform

serial monitor

Finally, add one more line of code for each function to test the serial monitor. In settings add:

 Serial.begin(9600); 

And in your loop function, add a message to be printed on the serial monitor every time the loop ends:

 Serial.println("loop completed"); 

Upload the sketch and click on the plug icon on the bottom panel to open the serial port monitor and see your message.

Arduino Serial Monitor in VS Code

The New World of Arduino Coding

Using VS Code and PlatformIO gives a breath of fresh air to Arduino coding. It also makes remote collaboration much easier with the Live Share feature in Visual Studio.

This guide only covers the basics, but anything you can do in the Arduino IDE is possible with PlatformIO, so pick a great project. and get the code!

Похожие записи