Skip to content
Phuc's Blog
Go back

Getting Started with PlatformIO & ESP32

Edit page
πŸ“ŒSeries:Internet of Things
Part 6 of 7
View all articles in this series

Say goodbye to clunky vendor IDEs! In this guide, we will explore PlatformIO, understand why it is a game-changer for embedded development, and set up your first ESP32 project right inside Neovim.

Note

πŸ“Œ IoT Series (Part 6 of 6): This is the sixth article in our IoT series.

Getting Started with PlatformIO

Table of contents

Open Table of contents

The Embedded Dev Headache 😫

If you have ever tried embedded development, you know the pain:

That is exactly why PlatformIO was born!


What is PlatformIO? πŸš€

PlatformIO is an open-source, cross-platform ecosystem for IoT and embedded development.

Note

πŸ’‘ Analogy: Think of PlatformIO as npm for JavaScript or Cargo for Rust, but for embedded hardware microcontrollers! It manages your development environment, toolchains, board configurations, and external libraries automatically.


How Does It Work? βš™οΈ

Building a project with PlatformIO follows a clean, automated workflow:

  1. Define Your Board: Specify your target hardware in platformio.ini (e.g., board = esp32dev).
  2. Auto-Fetch Dependencies: PlatformIO automatically downloads the exact C/C++ compilers, SDKs, frameworks, and libraries required.
  3. Build & Upload: With a single command, PlatformIO compiles your code and flashes it straight to your microcontroller!

Setting Up PlatformIO in Neovim πŸ› οΈ

For Neovim enthusiasts, you do not need to leave your editor! We can use nvim-platformio.lua for a seamless setup.

1. Prerequisites

Make sure Python and PlatformIO Core are installed on your system:

pip install platformio

2. Install the Neovim Plugin

Add nvim-platformio.lua to your Neovim plugin manager (e.g., lazy.nvim):

return {
  'anurag3301/nvim-platformio.lua',
  dependencies = {
    { 'akinsho/toggleterm.nvim' },
    { 'nvim-lua/plenary.nvim' },
    { 'folke/which-key.nvim' },
    { 'nvim-treesitter/nvim-treesitter' },

    -- Choose your preferred picker (Telescope, Snacks, or Mini.Pick)
    { 'nvim-telescope/telescope.nvim' },
    { 'nvim-telescope/telescope-ui-select.nvim' },
  },
}

Initializing Your First Project 🎯

Step 1: Trigger Project Setup

Run the :Pioinit command in Neovim to start the project initialization wizard:

Initialize PlatformIO Project

Step 2: Select Your Microcontroller Board

Search and select your board (for example, ESP32 Dev Module):

Select Board in PlatformIO

Tip

The picker displays board specifications, RAM/Flash sizes, and capabilities on the side. You can also manually tweak platformio.ini later to adjust baud rates or add external libraries!

Step 3: Explore the Generated Project Structure

Once initialized, PlatformIO generates a clean workspace structure:

PlatformIO File Structure

Key directories & files:

Super clean and easy setup! Now you are ready to write code and flash your ESP32 effortlessly. πŸŽ‰


Writing Your First β€œHello World” App πŸ’»

Open src/main.cpp and paste this simple starter code:

#include <Arduino.h>

#ifndef LED_BUILTIN
#define LED_BUILTIN
#endif

void setup() {
    Serial.begin(9600);
}

void loop() {
    Serial.println("Hello, ESP32!");
    delay(1000);
}

πŸ’‘ Quick Code Breakdown:

Important

⚠️ Baud Rate Matching Rule: The baud rate set in your code (Serial.begin(9600)) MUST match the baud rate of your Serial Monitor! If they do not match (or if you use an unstandard rate like 9500), you will receive garbled text/symbols (?#@!) due to clock timing misalignment.

You can also specify the monitor baud rate in platformio.ini:

monitor_speed = 9600

PlatformIO Neovim’s plugin Commands πŸ“

The nvim-platformio.lua plugin provides convenient commands and an interactive popup menu that wrap standard PlatformIO Core (CLI) commands:

General PlatformIO Commands Menu

πŸ”„ The Standard Embedded Dev Workflow

When developing microcontroller firmware, you follow this 3-step loop:

  1. Build Code: Compile your C++ code to verify there are no syntax or compilation errors.
  2. Upload Firmware: Flash the compiled binary onto your microcontroller via USB.
  3. Monitor Output: Open the serial terminal to view Serial.println() output and debug live runtime logs.

⚑ General Plugin Commands & CLI Wrappers

Plugin ActionPurpose / DescriptionUnderlying CLI Wrapper
BuildCompiles project environments and verifies codepio run
UploadCompiles & flashes firmware directly to devicepio run --target upload
MonitorOpens live serial monitor to read device outputpio device monitor
CleanRemoves compiled object files to free cachepio run --target clean
Full CleanWipes all build artifacts and cached dependenciespio run -t fullclean
Device ListScans and lists active connected serial portspio device list

πŸ“¦ Dedicated Neovim Plugin Commands

1. :Piolib β€” Library Manager

Search Libraries with Piolib

Selecting a library displays its info, dependencies, and lets you add it straight to your platformio.ini:

Library Search Results


2. :Piolsserial β€” List Serial Ports πŸ”Œ

Execute Piolsserial Command

Output of active serial devices connected to your computer:

Connected Serial Devices List


3. :Piomon β€” Live Serial Monitor πŸ“Ί

PlatformIO Serial Monitor in Neovim


Essential PlatformIO CLI Commands ⚑

PlatformIO Core provides the pio run command to process, build, and flash your projects:

# Build all environments defined in platformio.ini
pio run

# Build and flash firmware to all connected devices
pio run --target upload

# Clean build artifacts (delete compiled objects)
pio run --target clean

# Build & upload for a specific board environment only (e.g. esp32dev)
pio run -e esp32dev -t upload

πŸ“– Series Navigation

πŸ‘ˆ Back to Part 5: Overview of Physical Works in IoT
🏠 Return to Part 1: Introduction to Internet of Things (IoT)

πŸ“š Complete IoT Getting Started Series

  1. Part 1: Introduction to Internet of Things (IoT)
  2. Part 2: Deeper Dive into Internet of Things (IoT)
  3. Part 3: Sensors and Actuators in Internet of Things (IoT)
  4. Part 4: Connect your device to the Internet
  5. Part 5: Overview of Physical Works in IoT
  6. Part 6: Getting Started with PlatformIO

Edit page
Share this post:

Previous Post
Introduction to Parallel Programming
Next Post
Some basic hardware devices in IoT

Comments