| name | platformio |
| description | PlatformIO embedded development platform expertise. Project setup, configuration, building, debugging, and library management for Arduino, ESP-IDF, STM32, and more. |
| allowed-tools | Read, Write, Edit, Glob, Grep, Bash |
PlatformIO Reference
Complete reference for PlatformIO embedded development.
1. Installation
pip install platformio
curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py -o get-platformio.py
python3 get-platformio.py
2. Project Structure
project/
โโโ platformio.ini # Project configuration
โโโ src/
โ โโโ main.cpp # Main source file
โโโ include/ # Header files
โโโ lib/ # Project-specific libraries
โโโ test/ # Unit tests
โโโ .pio/ # Build output (gitignored)
3. Configuration (platformio.ini)
Basic Structure
[platformio]
default_envs = esp32dev
src_dir = src
include_dir = include
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
Common Platforms
| Platform | Board Examples | Frameworks |
|---|
espressif32 | esp32dev, esp32-s3-devkitc-1 | arduino, espidf |
espressif8266 | esp12e, d1_mini | arduino |
ststm32 | nucleo_f446re, bluepill_f103c8 | arduino, stm32cube |
raspberrypi | pico | arduino |
atmelavr | uno, nanoatmega328 | arduino |
ESP32 Examples
[env:esp32-arduino]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
upload_speed = 921600
lib_deps =
bblanchon/ArduinoJson@^7.0.0
[env:esp32s3]
platform = espressif32
board = esp32-s3-devkitc-1
framework = arduino
board_build.mcu = esp32s3
board_build.f_cpu = 240000000L
build_flags =
-DBOARD_HAS_PSRAM
-mfix-esp32-psram-cache-issue
[env:esp32-idf]
platform = espressif32
board = esp32dev
framework = espidf
monitor_speed = 115200
board_build.partitions = partitions.csv
STM32 Examples
[env:nucleo_f446re]
platform = ststm32
board = nucleo_f446re
framework = arduino
upload_protocol = stlink
[env:bluepill]
platform = ststm32
board = bluepill_f103c8
framework = arduino
upload_protocol = stlink
RP2040 Examples
[env:pico]
platform = raspberrypi
board = pico
framework = arduino
[env:picow]
platform = raspberrypi
board = rpipicow
framework = arduino
4. CLI Commands
Building
pio run
pio run -e esp32dev
pio run -v
pio run -t clean
pio run -t cleanall
Uploading
pio run -t upload
pio run -e esp32dev -t upload
pio run -t upload --upload-port /dev/ttyUSB0
Serial Monitor
pio device monitor
pio device monitor -b 115200
pio device monitor -p /dev/ttyUSB0
pio device monitor --filter esp32_exception_decoder
Testing
pio test
pio test -e native
pio test -f test_foo
pio test -v
Device Management
pio device list
pio boards
pio boards esp32
Library Management
pio pkg search "json"
pio pkg install --library "bblanchon/ArduinoJson"
pio pkg install --library "bblanchon/ArduinoJson@^7.0.0"
pio pkg list
pio pkg update
5. Build Flags
Common Flags
build_flags =
-DDEBUG
-DVERSION=\"1.0.0\" ; String define (escaped quotes)
-I include/extra ; Additional include path
-Wall ; Enable all warnings
-Wextra ; Extra warnings
-O2 ; Optimization level
ESP32-Specific
build_flags =
-DCORE_DEBUG_LEVEL=3
-DBOARD_HAS_PSRAM
-DCONFIG_ASYNC_TCP_RUNNING_CORE=1
Conditional Flags
build_flags =
${env.build_flags}
$BUILD_FLAGS
6. Library Dependencies
Syntax
lib_deps =
bblanchon/ArduinoJson@^7.0.0
Adafruit Unified Sensor
https://github.com/me/mylib.git
https://github.com/me/mylib.git
file:///path/to/library
Version Constraints
lib_deps =
library@1.2.3
library@^1.2.3
library@~1.2.3
library@>=1.0.0
7. Multiple Environments
Shared Configuration
[env]
monitor_speed = 115200
lib_deps =
bblanchon/ArduinoJson@^7.0.0
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
[env:esp32s3]
platform = espressif32
board = esp32-s3-devkitc-1
framework = arduino
build_flags =
-DBOARD_HAS_PSRAM
Platform-Specific Overrides
[env:debug]
extends = env:esp32dev
build_type = debug
build_flags =
-DDEBUG=1
-g3
[env:release]
extends = env:esp32dev
build_type = release
build_flags =
-DNDEBUG
-O2
8. Testing
Test Structure
test/
โโโ test_main.cpp ; Native tests
โโโ test_embedded/
โ โโโ test_gpio.cpp ; On-device tests
โโโ unity.h ; Unity test framework (included)
Test File
#include <unity.h>
void setUp(void) {
}
void tearDown(void) {
}
void test_addition(void) {
TEST_ASSERT_EQUAL(4, 2 + 2);
}
void test_string(void) {
TEST_ASSERT_EQUAL_STRING("hello", "hello");
}
int main(int argc, char **argv) {
UNITY_BEGIN();
RUN_TEST(test_addition);
RUN_TEST(test_string);
UNITY_END();
}
Native Testing
[env:native]
platform = native
test_framework = unity
9. Debugging
Configuration
[env:debug]
platform = espressif32
board = esp32dev
framework = arduino
debug_tool = esp-prog
debug_init_break = tbreak setup
Debug Commands
pio debug
pio debug --interface=gdb
10. Custom Scripts
Extra Scripts
[env:esp32dev]
extra_scripts =
pre:scripts/pre_build.py
post:scripts/post_build.py
Script Example
Import("env")
def before_build(source, target, env):
print("Before build!")
env.AddPreAction("buildprog", before_build)
11. Common Issues
| Issue | Solution |
|---|
| Upload failed | Check port, try lower upload_speed |
| Library not found | Use full owner/name format |
| Build error | Clean with pio run -t clean |
| Wrong board | Verify board ID with pio boards |
Note: PlatformIO supports 1000+ boards. Use pio boards <search> to find your board.