Today we are going to see what is probably the best programming environment for processors like Arduino (or similar) using Visual Studio Code together with PlatformIO.
There is no doubt that when talking about programming environments for Arduino, logically, the Arduino IDE is the most widespread option. Although the Arduino environment is suitable for beginners, we also know that it has important limitations.
Although this has improved in version 2.0, it still lacks common functionalities in any modern IDE. So, it is difficult to imagine using the Arduino IDE in advanced or professional environments.
Thus, on the blog we have seen several alternatives to programming environments for Arduino, highlighting Visual Studio + Visual Micro that we saw in this post. Despite being 6 years old, it is still one of the best options but its price has increased considerably.
Fortunately, for a few years now we have a new environment consisting of Visual Studio Code + PlatformIO. In summary, technically it is more or less on par with Visual Micro, with the advantage that the latter is completely free.
In case you still don’t know it, Visual Studio Code is a multi-platform “lightweight” IDE developed by Microsoft that has become one of the most used IDEs, especially in web development.
On the other hand, PlatformIO is a free extension for Visual Studio Code that adds functionality to program embedded devices. Among these are both Arduino and many other processors such as the ESP8266 or the ESP32.
This new option is becoming very popular and not without reason, since the difference in functionality compared to the Arduino IDE is huge. For example, it has autocompletion, error highlighting, Git integration, or remote and collaborative development.
On the other hand, another advantage is that it is still very easy to use. Obviously, it has more options and configurations. But the advantages it provides make it, overall, even easier (what is difficult is programming without autocompletion or error highlighting).
First of all, let’s assume we have Visual Studio Code installed. If you haven’t done so, there is a guide on its page. But the process is simple. Download installer and “next, next, next”, without further mystery.
Now, within Visual Studio Code, we go to the “Extensions” palette, look for “PlatformIO” and install it. That easy! We already have PlatformIO installed.
Once the extension is installed, we can display the VS Code command palette by pressing Control + Shift + P. Here we type “Platform” (the first letters are sufficient) to see all available commands. For example, “PlatformIO Home” will take us to the main screen of the extension.
Alternatively, and even easier, a button will have been added to the side action bar. By clicking it, a menu opens and from there we can access the PlatformIO functionalities.
Create a new project
To create a new project, simply, from the Home screen of the extension we choose new project.
A wizard will open to configure the project, where we must choose the name and the device we are going to use. (There are already more than 1000 types of devices available!). Optionally, we can choose the location of the project.
With this, a project with the following structure will be created.
│ └── README
Here it should be noted that the main file is “src/main.cpp”. That is, the ‘.ino’ files from the Arduino IDE are over.
The platform.ini file
In the base folder of any PlatformIO project there is a file called ‘platform.ini’. In fact, it is the existence of this file that allows the extension to recognize a project.
platform = espressif32
board = m5stack-atom
framework = arduino
There are a lot of options that can be added here. The complete list is very extensive, so we leave you the link to the official documentation. However, practically all the options can be managed from the project configuration window in a much more comfortable way.
PlatformIO has a library management different from the Arduino IDE, which can be somewhat surprising for novice users. But, in reality, it is an improvement over this.
In the Arduino IDE, there is a library directory, where all libraries are installed globally. This was done to make it very easy to manage libraries.
However, this is a big problem in the long run, due to updates. So you can find that when updating “my library” from version 2.0 to 2.4, many of your previous projects may stop working due to incompatibilities with the new version.
Instead, in PlatformIO, libraries are added individually per project. In this way, project A can use “my library” v2.0, and project B version 2.4, without generating a conflict between them.
That is, what is a package manager throughout life, like nuget, chocolatey, npm, pip. In the case of PlatformIO, this package manager is called PIO, and is also available through CLI.
Compile and upload the project to the board
Of course, we can also verify, compile or upload the code to the board. For this we can use the command palette (Control + Shift + P) or use the buttons that PlatformIO adds to the bottom of Visual Studio Code.
It also includes a Serial Port monitor, although, in my opinion, it is quite weak. Personally, I use mine, which is why I have Arduino Live Serial.
So far this post. If you haven’t used Visual Studio Code and Platform yet, seriously, give it a chance. It is a great development environment, comfortable, practical, and with all the editing advantages that Visual Studio Code provides.
Of course, there are more options than the ones we have seen in this introduction post. We refer you to the official PlatformIO page if you want to expand the information.