0
0
AutocadHow-ToBeginner · 4 min read

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., atmelavr for 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 board in platformio.ini, causing upload failures.
  • Placing code files outside the src folder, 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 run and pio 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

CommandDescription
pio init --board unoCreate new PlatformIO project for Arduino Uno
pio runBuild the project
pio run --target uploadUpload the code to Arduino board
pio device monitorOpen serial monitor to see Arduino output
platformio.iniProject 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.