VSCode and PlatformIO

The Arduino IDE is a good software for learning how to program microcontrollers.
When you are more experienced, it quickly becomes limiting because many very useful features are missing: Dark theme, auto-completion, multilanguage, efficient library management, git, analysis tools …
In this article we will see how VSCode and PlatformIO can effectively replace the Arduino IDE.

VSCode

Visual Studio Code logo

Microsoft’s Visual Studio Code is a very good code editor (or IDE). It integrates the features that any good modern IDE must have (auto-completion, highlight, customizable …) VS Code is open-source, free and cross-platform (compatible with Windows, Mac, Linux). In addition, there are thousands of extensions with a lot of features.
VS Code is a very flexible and indispensable tool.
I invite you to do research and then adopt this software.

There is a fork without Microsoft’s telemetry tools: VSCodium

PlatformIO

PlatformIO logo

PlatformIO adds support for different microcontrollers in VSCode. Moreover everything becomes simpler, library management, compilation tools, programming tools …
The portability of projects is also improved thanks to the integration of libraries.
However, PlatformIO requires the use of configuration files and the VSCode interface can be confusing. Learning to use is not insurmountable.

Installing PlatformIO in VSCode

PlatformIO is an ecosystem that is added on top of an integrated development environment (IDE). It can be used alone from the command line as described in the article “Programming an Arduino with a Raspberry Pi via SSH“.

First, you need to download and install VSCode.

To install PlatformIO in VSCode, go to the extensions menu, search for “PlatformIO” and click on “Install”. Installation is very easy, as with other VSCode extensions

VSCode: Installing the PlatformIO extension

Start a new project

To open PlatformIO, click on the logo in the left banner of VSCode.
Then in “QUICK ACCESS”, click on “PIO HOME-> Open”.

Opening PlatformIO in VSCode

When we create a new project by clicking on “+ New Project”, we must enter a project name, a target (board) and a Framework.
Many boards can be used with PlatformIO (AVR, STM32, ESP32, ESP8266 …). It is possible to use frameworks other than Arduino. For example STM32Cube for STM32 or ESP8266 RTOS SDK …
As an example we will create a “TestProject” project for an Arduino UNO with the Arduino Framework.

New PlatformIO project

PlatformIO automatically downloads the tools necessary to use the card selected for the project. No need to worry about the board manager like in the Arduino IDE.

When the project is created, the first thing to do is to configure the project via the “platformio.ini” file.

PlatformIO init file

For our example project we will use a OneWire temperature sensor and display the temperature on the terminal every 2 seconds.
To use the terminal integrated in the IDE, you must add the line “monitor_speed = 115200” (the baudrate must correspond with the program).
PlatformIO automatically detects the serial port assigned to the Arduino board. However, when several cards are connected to the computer, it is sometimes necessary to specify the port with the line “upload_port = …”.
To complete the configuration file of our example, we will also integrate the OneWire library with the line “lib_deps = paulstoffregen / OneWire @ 2.3.5“.

Here is the resulting “platformio.ini” file:

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:uno]
platform = atmelavr
board = uno
framework = arduino
//upload_port = COM7
monitor_speed = 115200

lib_deps = paulstoffregen/OneWire @ 2.3.5

Example of an Arduino program “main.cpp”:

#include <Arduino.h>
#include <OneWire.h>

#define ONEWIRE_PIN A0

uint8_t addr[8];
uint8_t data[9];
float temp;

OneWire ds;

void setup() {
  Serial.begin(115200);
  ds.begin(ONEWIRE_PIN);
  //Reset the 1-Wire bus
  ds.reset_search();
  //Check Sensor
  if (!ds.search(addr)) 
    Serial.println("No Sensor found");
  if (OneWire::crc8(addr, 7) != addr[7]) 
    Serial.println("Invalid Address");
  if (addr[0] != 0x28)
    Serial.println("Invalid Sensor");
}

void loop() {
  //Starts a temperature measurement and waits 800ms
  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);
  delay(800);
  //Reset the 1-Wire bus, select the sensor and send a scratchpad read request
  ds.reset();
  ds.select(addr);
  ds.write(0xBE);
  //Scratchpad reading
  for (byte i = 0; i < 9; i++)
    data[i] = ds.read();
  //Calculation of temperature in degrees Celsius
  temp = (int16_t) ((data[1] << 8) | data[0]) * 0.0625; 
  //Print temperature
  Serial.print("Temp.: ");
  Serial.print(temp);
  Serial.println("*c");
  delay(1200);
}

With the addition of the line “lib_deps = paulstoffregen / OneWire @ 2.3.5”, it is possible to use the OneWire library without installing it in the IDE.

Compilation, Upload and Terminal

The icons at the bottom of the interface allow VSCode run the compilation, download, terminal …

VSCode and PlatformIO : Icons for Compilation, Upload and Terminal

Libraries

The previous example already includes a library.
Indeed, it is possible to link libraries to a project. They will be automatically downloaded and added to the projects. This makes it possible to obtain projects completely independent from a collection of libraries and to have portable projects. It is even possible to specify a specific version of libraries. This makes it possible to use different versions on several projects or to automatically keep libraries up to date.

PlatformIO has a library search tool:

PlatformIO libraries search

When we want to use a library, the “Installation” tab allows us to consult the information to add to the “platformio.ini” file to use the library.

Librairie OneWire PlatformIO

With PlatformIO, it is easy to find and use libraries. The “Examples” tab allows you to browse the example programs supplied with the libraries.

Conclusion

The use of VSCode with PlatformIO can be scary when you do not know this kind of tools. We learn quickly and we end up abandoning the Arduino IDE.
Everything is simpler, faster and more powerful. PlatformIO greatly improves the management of cards, libraries and projects. Many tools are helping the developer (auto-completion, dark theme, flexibility and customization of the workspace …)
It is possible to work on projects with multiple languages, to integrate unit tests and many other things.

Consult the PlatformIO documentation to browse the possibilities offered by this tool.

Leave a Reply

Your email address will not be published.