Skip to content
Shop

ESP32 MCU: PlatformIO Setup

Set up with your favorite AI

Gemini & other AI

Copy the setup prompt into your AI. Work through each step together, from checking your kit to getting it running.

Open Gemini

Copy the prompt, then paste it into Gemini’s message box.

View prompt

Use PlatformIO IDE to set up the development environment for the AP02 ESP32 MCU leaf (ESP32-WROOM-32).

Visual Studio Code (VS Code) is Microsoft’s lightweight source-code editor for Windows, macOS, and Linux. PlatformIO IDE is a VS Code extension with fast builds and per-project management of library and board versions.

  • ESP32 Wi-Fi Kit 2
  • A Windows, macOS, or Linux computer

PlatformIO IDE requires Python. Follow the Python installation guide (Japanese) to install it.

  1. Install VS Code.
  2. Open Extensions in the left sidebar and install the following extensions:
    • Japanese Language Pack for Visual Studio Code (for the Japanese interface shown in the source guide)
    • PlatformIO IDE
    • Serial Monitor
    • Teleplot

This Hello World example introduces the PlatformIO IDE workflow.

  1. Start VS Code and open File → New Window.
  2. Select PlatformIO in the left sidebar to open PLATFORMIO QUICK ACCESS.
  3. Select PIO Home → Open.
  4. Select New Project and fill in the Project Wizard, then select Finish. The first setup for a board may take several minutes to download its files.
    • Name: ESP32_Hello_World_Pjt
    • Board: Espressif ESP32 Dev Module
    • Framework: Arduino
  5. Open platformio.ini and check the board and framework settings:
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
  1. The ESP32 MCU upload workflow detects the serial port automatically, so a COM port number does not need to be specified.
  2. Add the serial monitor baud rate to platformio.ini:
monitor_speed = 115200
  1. Open src/main.cpp and paste the following code. PlatformIO requires #include <Arduino.h> at the beginning of an Arduino sketch.
#include <Arduino.h>
void setup() {
Serial.begin(115200);
delay(10);
}
void loop(){
Serial.println("Hello World");
delay(1000);
}

Source code: ESP32_Hello_World_Pjt

  1. Use Project Environment Switcher in the bottom PlatformIO toolbar to select the correct active project, then select Build.

  1. Connect Leafony to the computer as shown in Connecting to PC.
  2. Select Upload to upload the program to the MCU.
  3. Press the MCU reset button to run the program.
  4. Open Serial Monitor and check that Hello World appears.

Add library dependencies with lib_deps = at the end of platformio.ini. You can specify a GitHub URL or select a library through Libraries.

The library references for each leaf are listed below.

Item lib_deps Tags Description
BLE https://github.com/Leafony/TBGLib Bluetooth library
4-Sensors adafruit/Adafruit Unified Sensor@^1.1.6 Unified sensor driver
adafruit/Adafruit BusIO@^1.14.1 Bus IO library
https://github.com/ameltech/sme-hts221-library Temperature and humidity sensor library
closedcube/ClosedCube OPT3001@^1.1.2 Illuminance sensor library
adafruit/Adafruit LIS3DH@1.1.2 1.1.2 Accelerometer library
LCD tomozh/ST7032@0.0.0-alpha+sha.501bf64fe6 LCD library
AVR MCU paulstoffregen/MsTimer2@^1.1 Timer interrupt library
RTC&MicroSD adafruit/RTClib@^2.1.1 RTC library
STM32 MCU stm32duino/STM32duino RTC @1.2.0 1.2.0 STM32RTC library
stm32duino/STM32duino Low Power@^1.2.2 STM32LowPower library
LTE-M https://github.com/Leafony/LteLeafV4 LTE-M library

To plot serial data as in Arduino IDE, use the Teleplot for VSCode extension. Download and try the sample below.

  1. Download ESP32_Teleplot_Example_1 from GitHub and save it in your project folder.1
  2. Run the program.
  3. Select teleplot at the bottom of the window, choose the COM port, and select Open to display the graph.
  4. Teleplot plots serial messages in the form >varName:1234\n. See the Teleplot documentation for details.
// Plot a sinus
Serial.print(">sin:");
Serial.println(sin(i));
// Plot a cosinus
Serial.print(">cos:");
Serial.println(cos(i));

Download the sample projects from GitHub, save them in your project folder,1 and run them in PlatformIO IDE.

ItemSource code
Temperature and humidity sensorESP32_Thermo-Hygrometer_Pjt
Illuminance sensorESP32_Illuminance_Meter_Pjt
AccelerometerESP32_Accelerometer_Pjt
TypeLeafPlatformBoardUpload
AP01AVR MCUatmelavrArduino Pro or Pro Mini ATmega328(3.3v,8Mhz)Automatic COM port detection
AP02ESP32 MCUEspressif 32Espressif ESP32 Dev ModuleAutomatic COM port detection
AP03STM32 MCUststm32LEAFONY_AP03COM port cannot be detected automatically
  1. Add #include <Arduino.h> at the beginning of the program. Arduino IDE includes basic type definitions and commonly used headers automatically; PlatformIO requires the explicit include.
  2. Define custom functions before they are called, or add a prototype declaration. Arduino IDE prepares declarations automatically, while PlatformIO follows normal C++ rules.

The following example fails to build because the function is defined after its first use:

void setup() {
// put your setup code here, to run once:
func();
}
void func() {
;
}

This example builds because the function is defined before its use:

void func() {
;
}
void setup() {
// put your setup code here, to run once:
func();
}

This example also builds because it includes a prototype:

// Prototypes
void func(void);
void setup() {
// put your setup code here, to run once:
func();
}
void func() {
;
}

New Project in the Project Wizard creates a folder under ~/Documents/PlatformIO/Projects by default. Rename or delete that folder directly in your file manager.

Configure the location from the terminal:

  1. Start VS Code and open File → New Window.
  2. Select PlatformIO in the sidebar to open PLATFORMIO QUICK ACCESS.
  3. Select PIO Home → Open.
  4. Run pio settings set projects_dir <absolute-path> with your desired folder’s absolute path.
  5. Restart VS Code.
  1. The default Windows folder is C:\Users\<username>\Documents\PlatformIO\Projects. 2