Getting started with Platform.IO

Since I tend to spend a lot of time in Visual Studio Code for other projects these days, I spent a little time moving my Arduboy projects from Arduino to VSCode this morning, and thought I’d write up the process.

First, set up Platform.IO. It installs as an extension to VSCode. Details on basic installation are at A professional collaborative platform for embedded development · PlatformIO, so follow that to get it installed on your system.

If you want to move an existing Arduino-based project over, go to the PlatformIO home screen (with the home icon installed at the bottom of the VSCode window), and choose “Import Arduino Project”. You’ll eventually get to navigate to find the folder where your .ino file is, and it will create a new project folder, and copy over everything.

After that copy, you’ll need to rename your new .ino file to have a .cpp extension instead. You probably should move your header files from the src folder into the parallel include folder. You’ll also need to add the Arduboy libraries you are using. I found that the “use libraries from your Arduino” folder option didn’t work well for me. However, all of the libraries that I’d used from Arduino were also available via PlatformIO. To search for these, bring up the library UI by going to the Platform IO home, then clicking on the Library tab. You can search for libraries like “Arduboy2”, “ArduboyTones”, or “FixedPoints”, and use the “add to project” button to get them added to your platformio.ini file.

This platformio.ini file is the key to understand how the build works. For my projects, it ends up looking like

[env:arduboy]
platform = atmelavr
board = arduboy
framework = arduino
lib_deps = 
	mlxxxp/ArduboyTones@^1.0.3
	pharap/FixedPoints@^1.1.0
	mlxxxp/Arduboy2@^6.0.0

The lib_deps list is saying that I need those libraries at that version number or later. The build ending will automatically pull down the code and include it in the project. The platform line specifies that we’re using the AVR toolchain, while board says we’re building for Arduboy, and framework enables the Arduino API support, including the automatic invocation of the setup() and loop() functions.

I found that after adding the libraries, I still had a little build problem with my code being unable to find the Arduboy2.h header, but it was solved by closing the IDE and reopening it.

For the most part, the toolbar buttons (which also map to commands you can invoke) work like the Arduino IDE. Check will build, but not upload it to the target. Upload does a build first, then invokes an upload tool. You get the build output all shown in a terminal pane and can easily step through problems.

For making a new project, you can either create it with the Platform IO dialog wizard, or just copy the folder to a new location and use VSCode’s Open Folder to open it. The PlatformIO extenstion will see the platformio.ini file and enable it. Since I do a lot of different project types, I turned on the setting to only run the PlatformIO extension when that file was in the workspace directory.

If you want to see how I setup my projects, I added the setup as a new branch in my GitHub repos.

I ended up doing more manual migration for the BeamEmUp repo which made it look like a more natural branch from main, where with Ravine, I just force push the branch which removed the connection. I may end up trying to fix up the Ravine branch to have proper history when I get a change.

3 Likes

I have been using VSCode for a long time now but my approach was totally different to yours.

I already had the Arduino IDE installed with the Homemade package and all the other libraries I need (ArduboyTones, Playtunes, etc)

I simply installed the ‘Arduino for Visual Studio Code’ plugin and from there on it detects that your project has a .ino file within it (or it does so when you open the file) and presents you with the options to pick a board, bootloader and so on from the Homemade Package. Ctrl-Shift-B to build.

I wonder what advantages platform.io has over my approach and vice versa?

1 Like

So far, the big advantages I’ve seen are around build speed. The Arduino build system felt significantly slower to rebuild a project and upload.

For example, rebuilding and uploading Ravine Despoiler from PlatformIO from a clean state took 24.1 seconds on my Windows laptop. Touching the main source file and rebuilding took 9.4 seconds. When I tried the same test in the Arduino IDE, my first build and upload took 26.3 seconds, then adding a space in the INO file and rebuilding/uploading took 18.2 seconds.

I also found the project configuration to be a lot more in line with other build systems used in the Python, Rust, or JavaScript worlds. You also can easily ditch the Arduino framework and build directly on the platform SDKs if needed.

The increase in speed is interesting. I assume both systems are only recompiling changed items so I am surprised that PlatformIO is taking half the time when you alter the main .ino.

The configuration comment is also interesting. I am guessing for a beginner the Arduino plugin is simpler as it just picks up the config and libraries of the Arduino IDE. You can change the board type and a few other params via VSCode / Homemade package and the arduino.json file but most beginners would probably not need to do that.

I do like the idea of getting away from the Arduino handling of ino files and some of their questionable compiler flags :slight_smile:

6 posts were split to a new topic: Using VSCode for Arduboy - Tips & Tricks