Use PlatformIO to write Arduino code

Fang Jin
2 min readAug 12, 2023

If you have been using VSCode and Arduino IDE and flip between them like me, one to write code and one to upload code. You are not alone, however, you don’t have to. Arduino sketch code is essentially C/C++ code, so it can belong to a typical software development process.

The last time I wrote C++ code is like twenty years ago, when I was writing a simulation library for my school project. I don’t quite remember how to do the Make file and etc now. Luckily there’s a nice plugin called PlatformIO for Visual Studio Code that can help us to deal with all the project organization and build process, similar to the Create React App from the frontend software business. It can create a C++ application for embedded programming.

Once you turn the plugin on, you can use it to create an app based on your board type, in my case, Arduino Leonardo. Afterwards it’ll setup the project for you with a configuration file platformio.ini :

[env:leonardo]
platform = atmelavr
board = leonardo
framework = arduino

Now you can start to write code in the main.cpp file under src folder.

Notice the setup and loop function, which isn’t too different from an Arduino sketch, except the file extension is changed back to cpp . This reminds us we are writing an official C++ project.

Now comes the best part, remember we normally need to compile the code and upload it using the Arduino IDE, now we can do everything using VSCode editor. Bring your commands Command+Shift+P, and find PlatformIO:build/upload, you can then upload the compiled code right to your device with no delay.

This is amazing.

I like this approach a lot, since now I don’t have to work with multiple IDEs at the same time. Moreover, since this is basically a software business, I can apply Version control, Vim, Prettier to my code. Whatever the plugin you like to have when working with a code IDE suddenly all available to you. I really recommend this approach to whoever spend more time in coding than device hacking :)

--

--

Fang Jin
Fang Jin

Written by Fang Jin

Front-end Engineer, book author of “Designing React Hooks the Right Way” and "Think in Recursion"

No responses yet