How to Use Arduino with PlatformIO: Step-by-Step Guide
To use
Arduino with PlatformIO, install PlatformIO IDE or CLI, create a new project selecting your Arduino board, then write and upload your code using PlatformIO commands. PlatformIO manages libraries and builds, making Arduino development easier and more flexible.Syntax
PlatformIO uses a platformio.ini file to configure your Arduino project. Key parts include:
- platform: specifies the hardware platform, e.g.,
atmelavrfor Arduino Uno. - board: selects the exact Arduino board, e.g.,
uno. - framework: sets the software framework, usually
arduino.
Your main code goes in src/main.cpp using standard Arduino functions like setup() and loop().
ini
[env:uno] platform = atmelavr board = uno framework = arduino
Example
This example shows a simple Arduino blink program using PlatformIO. It blinks the built-in LED on pin 13 every second.
cpp
#include <Arduino.h> void setup() { pinMode(LED_BUILTIN, OUTPUT); } void loop() { digitalWrite(LED_BUILTIN, HIGH); delay(1000); digitalWrite(LED_BUILTIN, LOW); delay(1000); }
Output
The built-in LED on the Arduino board blinks on and off every second.
Common Pitfalls
Common mistakes when using Arduino with PlatformIO include:
- Not selecting the correct
boardinplatformio.ini, causing upload failures. - Placing code files outside the
srcfolder, so PlatformIO does not compile them. - Forgetting to install PlatformIO libraries if your code depends on external libraries.
- Trying to use Arduino IDE commands instead of PlatformIO commands like
pio runandpio run --target upload.
Wrong: Uploading with Arduino IDE while using PlatformIO project files.
Right: Use pio run --target upload in the PlatformIO terminal.
Quick Reference
| Command | Description |
|---|---|
| pio init --board uno | Create new PlatformIO project for Arduino Uno |
| pio run | Build the project |
| pio run --target upload | Upload the code to Arduino board |
| pio device monitor | Open serial monitor to see Arduino output |
| platformio.ini | Project config file to set board and framework |
Key Takeaways
Always set the correct board and framework in platformio.ini for Arduino projects.
Write Arduino code in src/main.cpp using standard setup() and loop() functions.
Use PlatformIO commands like pio run and pio run --target upload to build and upload.
Keep code files inside the src folder for PlatformIO to compile them.
Use pio device monitor to view serial output from your Arduino board.