PlatformIO is an open-source ecosystem for embedded development, designed to simplify and accelerate the process of programming microcontrollers and IoT devices. It provides a unified interface for working with various embedded platforms, frameworks, and toolchains. PlatformIO is widely used in conjunction with IDEs like Visual Studio Code and CLion but can also be run from the command line
PlatformIO IDE for VSCode
-
Install PlatformIO IDE for VSCode
-
View home page for PlatformIO
- Click home button at the bottom or click alien icon → PIO Home → Open
Create a PlatformIO Project
-
Open the Project Wizard:
- In Visual Studio Code, click on the PlatformIO icon (an alien head) on the left sidebar.
- Select “New Project” to open the project creation wizard.
-
Configure Your Project:
- Project Name: Choose a descriptive name for your project (e.g.,
MyFirstProject
). - Board: Select the microcontroller board you are using (e.g.,
Arduino Uno
,ESP32
, orSTM32
). - Framework: Pick the framework compatible with your board (e.g.,
Arduino
,ESP-IDF
,Mbed OS
). PlatformIO will automatically determine this based on your board selection. - Location: Set the folder where the project will be stored. You can leave the default location or choose a custom directory.
- Project Name: Choose a descriptive name for your project (e.g.,
- To update the configuration manually, modify
platform.ini
, example:
[env:uno]
platform = atmelavr
; board = uno
framework = arduino
# Mega Uno settings
board = megaatmega2560
upload_port = COM12
-
Project Folder Structure:
- After creating the project, PlatformIO will generate a folder structure like this:
MyFirstProject/ ├── include/ # Header files (optional) ├── lib/ # Custom libraries ├── src/ # Your main source code ├── platformio.ini # Configuration file
- After creating the project, PlatformIO will generate a folder structure like this:
main.cpp
In Arduino IDE, most files use the .ino extension, but in PlatformIO, all your code files will be kept in the src
folder, and there will be a main.cpp
file
#include <Arduino.h>
// put function declarations here:
int myFunction(int, int);
void setup() {
// put your setup code here, to run once:
int result = myFunction(2, 3);
}
void loop() {
// put your main code here, to run repeatedly:
}
// put function definitions here:
int myFunction(int x, int y) {
return x + y;
}
Arduino Uno Blink Test
Here’s a simple example demonstrating how to define and call functions:
#include <Arduino.h>
int led = LED_BUILTIN;
void setup()
{
// put your setup code here, to run once:
pinMode(led, OUTPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}
Build and Upload Your Code
-
Connect Your Board:
- Plug in your microcontroller to your computer via USB.
-
Build the Project:
- Click the checkmark icon in the PlatformIO toolbar or use the
PlatformIO: Build
command from the command palette. - Ensure there are no errors in your code.
- Click the checkmark icon in the PlatformIO toolbar or use the
-
Upload to Board:
- Click the arrow icon to upload your code to the microcontroller.
- Monitor the upload process in the Terminal.
-
Open the Serial Monitor:
- Click the plug icon in the PlatformIO toolbar to open the Serial Monitor.
- Set the baud rate to match your
Serial.begin()
call (e.g., 9600).
Adapt PlatformIO to Arduino Project Structure
https://www.instructables.com/A-Way-to-Run-Arduino-Sketch-With-VSCode-PlatformIO/
-
Move sketch folder/s to
src
- Note: the
- Note: the
-
To make compatible with PlatformIO:
- include
#include <Arduino.h>
in eachino
file - include function declarations above the
setup()
function (ex:void forward();
) - Include the file in
main.cpp
, example:
- include
#include <Arduino.h>
#include "arduino_blink_test/arduino_blink_test.ino"
Add a library
-
Open PIO home, click libraries, search for library, and click Add to Project
-
Or, in
platformio.ini
, add the library to thelib_deps
field
[env:uno]
libdeps = IRremote
platform = atmelavr
board = uno
framework = arduino