Skip to content

PlatformIO IDE Setup

PlatformIO IDE Setup

This section introduces PlatformIO, a professional development environment for ESP32 and other embedded platforms. By the end of this section, you will be able to:

  • Install PlatformIO as a VS Code extension
  • Create and configure an ESP32 project
  • Understand the platformio.ini configuration file
  • Add libraries and manage dependencies
  • Compile and upload firmware to ESP32
  • Visual Studio Code installed
  • An ESP32 development board and USB cable
  • USB-UART driver installed (CP2102 or CH340)
FeaturePlatformIOArduino IDE
EditorVS Code (professional)Built-in (basic)
Compilation Speed2-4x faster (parallel compilation)Sequential
Library Managementplatformio.ini declarativeLibrary Manager (GUI)
Multiple Platforms40+ platforms (ESP32, STM32, RP2040, etc.)Arduino boards only
DebuggingGDB, OpenOCD, JTAGSerial monitor only
IntelliSenseFull code completion, lintingMinimal
Project StructureMulti-file, modularSingle sketch file
Compiler StrictnessStricter (requires function prototypes)More permissive

A PlatformIO project for ESP32 follows this structure:

my_project/
├── platformio.ini # Project configuration (required)
├── src/
│ ├── main.cpp # Main sketch (required)
│ ├── wifi_mqtt.h # Header file with code
│ └── credentials.h # Wi-Fi credentials
├── include/ # Header files (optional)
├── lib/ # Private libraries (optional)
├── test/ # Unit tests (optional)
└── .pio/ # Build artifacts (auto-generated)

Why PlatformIO is Preferred for This Course

Section titled “Why PlatformIO is Preferred for This Course”
  • Faster compilation: Saves significant time during iterative development
  • Better code organization: Multi-file projects with .h and .cpp separation
  • Library management: Libraries declared in platformio.ini are auto-downloaded
  • Cross-platform: Same project works on Windows, macOS, Linux
  • Arduino IDE compatibility: Code from PlatformIO can be ported to Arduino IDE (see 01-16)
  1. Open Visual Studio Code
  2. Go to the Extensions view (Ctrl+Shift+X or Cmd+Shift+X)
  3. Search for “PlatformIO IDE”
  4. Click Install (the extension is ~200 MB and includes the core toolchain)
  5. Wait for installation to complete, then restart VS Code
  6. The PlatformIO home page should open automatically (an ant icon appears in the activity bar)
  1. Click the PlatformIO icon (ant) in the VS Code activity bar
  2. Click Open under “PIO Home”
  3. Go to the Projects tab
  4. Click Create New Project
  5. Configure:
    • Name: esp32-basic-sketch
    • Board: Search for and select your board (e.g., “Espressif ESP32 Dev Module”)
    • Framework: Select “Arduino”
    • Location: Choose a folder (default is fine)
  6. Click Finish

The project is created with the standard structure. PlatformIO will automatically download the ESP32 toolchain and Arduino framework (first-time setup may take 2-5 minutes depending on internet speed).

The platformio.ini file is the heart of your project. It declares everything PlatformIO needs to know.

Basic Configuration:

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
upload_speed = 921600

With Libraries:

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
upload_speed = 921600
lib_deps =
knolleary/PubSubClient @ ^2.8
bblanchon/ArduinoJson @ ^7.0
adafruit/DHT sensor library @ ^1.4
adafruit/Adafruit Unified Sensor @ ^1.1

Explanation of Key Fields:

FieldDescriptionExample
platformTarget platform (SoC family)espressif32
boardSpecific board modelesp32dev, lolin32, m5stack-core2
frameworkSoftware frameworkarduino, espidf
monitor_speedSerial monitor baud rate115200, 9600
upload_speedFirmware upload baud rate921600, 115200
lib_depsLibrary dependenciesAuto-downloads from PlatformIO registry

Replace the contents of src/main.cpp with:

#include <Arduino.h>
#include <WiFi.h>
const char* ssid = "YourWiFiSSID";
const char* password = "YourWiFiPassword";
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("PlatformIO ESP32 Test");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWi-Fi Connected!");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
}
void loop() {
Serial.printf("Uptime: %d seconds\n", millis() / 1000);
delay(5000);
}

Note on Compiler Strictness: PlatformIO uses a stricter C++ compiler than Arduino IDE. You must declare function prototypes before calling them. For example:

// Arduino IDE allows this:
void setup() {
myFunction();
}
void myFunction() { }
// PlatformIO requires this (prototype before first call):
void myFunction(); // <-- prototype
void setup() {
myFunction();
}
void myFunction() { }

Build Only:

  1. Click the Checkmark icon in the PlatformIO status bar (bottom blue bar)
  2. Or use the terminal: pio run
  3. Watch for “SUCCESS” in the output

Build and Upload:

  1. Click the Right Arrow icon in the PlatformIO status bar
  2. Or use the terminal: pio run --target upload
  3. PlatformIO will automatically handle the flash mode (no need to press BOOT button on most boards)

Monitor Serial Output:

  1. Click the Plug icon (Serial Monitor) in the PlatformIO status bar
  2. Or use the terminal: pio device monitor
  3. Baud rate is automatically set from monitor_speed in platformio.ini
Terminal window
# Clean build artifacts
pio run --target clean
# Full rebuild
pio run --target clean && pio run
  • Project compiles without errors (“SUCCESS” in terminal output)
  • Upload completes to 100%
  • Serial Monitor shows Wi-Fi connection and IP address
  • Libraries declared in lib_deps are automatically downloaded
  • Modifying code and re-uploading works (iterative development)

”fatal error: SomeLibrary.h: No such file or directory”

Section titled “”fatal error: SomeLibrary.h: No such file or directory””

Causes:

  • Library not declared in lib_deps
  • Library name mismatch

Solutions:

  1. Verify the library name in platformio.ini matches the PlatformIO registry
  2. Run pio pkg install to force library download
  3. If using a local library, place it in the lib/ folder

Upload fails: “A fatal error occurred: Failed to connect to ESP32”

Section titled “Upload fails: “A fatal error occurred: Failed to connect to ESP32””

Causes:

  • Wrong board selected in platformio.ini
  • Board not in flash mode
  • Wrong port

Solutions:

  1. Verify board = esp32dev matches your hardware
  2. Hold BOOT and press EN, then try uploading again
  3. Check the port: pio device list shows available ports
  4. Add upload_port = /dev/cu.usbserial-XXXX (macOS) or upload_port = COM3 (Windows) to platformio.ini

Causes:

  • Serial Monitor baud rate differs from Serial.begin() value

Solutions:

  1. Ensure monitor_speed in platformio.ini matches Serial.begin(115200) in your code
  2. Both should typically be 115200
  • Use lib_deps with version pinning: knolleary/PubSubClient @ ^2.8 ensures reproducible builds
  • Separate credentials into a header file: Create credentials.h with SSID/password (add to .gitignore)
  • Use .gitignore for PlatformIO projects:
    .pio/
    .vscode/.browse.c_cpp.db*
    .vscode/c_cpp_properties.json
    .vscode/launch.json
    .vscode/ipch
  • Add build_flags for custom defines: build_flags = -DLED_BUILTIN=2
  • Enable build cache: Add board_build.build_caching = yes to speed up repeated builds
  1. PlatformIO is a professional embedded development environment integrated with VS Code
  2. The platformio.ini file declaratively manages board selection, framework, libraries, and build settings
  3. PlatformIO’s compiler is stricter than Arduino IDE — function prototypes are required
  4. Library management is automated via lib_deps — no manual download needed
  5. Build/upload/monitor are one-click operations from the PlatformIO status bar