What is PlatformIO and What Are Its Benefits?

PlatformIO is an open-source cross-platform IoT development ecosystem designed to simplify the development process for embedded systems. It provides a comprehensive set of tools and frameworks, including an integrated development environment (IDE), cross-platform build systems, a library manager, and debuggers, making development simpler, more efficient, and scalable. PlatformIO supports multiple hardware platforms and development boards such as Arduino, ESP8266, ESP32, STM32, etc., and is compatible with various development frameworks and microcontrollers. Compared to Arduino IDE, PlatformIO offers faster compilation and code upload speeds and leverages VSCode's rich plugins to enhance coding efficiency.

How to Install and Use PlatformIO

Install VSCode

Download and Install VSCode

Install Python

Download and Install Python
Note: Check the box to add the path during installation.

Install PlatformIO Plugin

  1. Open VSCode.
  2. Click the "Extensions" button, search for "PlatformIO" in the search bar, and click "Install".

  1. Restart VSCode after installation.

Create a Project

  1. Click "PlatformIO" and then "New Project".

  1. Enter the project name, search for and select the corresponding development board, then click "Finish".

    Currently natively supported boards:
    DFR0868 Beetle ESP32-C3
    DFR0654 Firebeetle 2 ESP32-E
    DFR0654-F Firebeetle 2 ESP32-E with Header
    DFR0975 Firebeetle 2 ESP32-S3
    DFR0975-U Firebeetle 2 ESP32-S3-U
    DFR0478 Firebeetle ESP32

    Note: The first-time project creation requires downloading the environment, which is very slow and usually takes more than half an hour.

  1. File Structure:
    • .pio: Files generated by project compilation
    • .vscode: VSCode configuration files
    • include: Header files
    • lib: Library files
    • src: Source code files
    • test: Test files
    • .gitignore: Git repository files
    • platformio.ini: Project configuration file

Compile and Upload the Project

  1. After creating the project, find the main file in the "src" directory to write code.

  1. Before compiling and uploading the code, configure the print output and serial baud rate in the platformio.ini file:

    ; Avoid duplicate definition of "ARDUINO_USB_MODE" during compilation.
    build_unflags = 
        -DARDUINO_USB_MODE=1
    ; Define serial printing to "USB CDC".
    build_flags = 
        -DARDUINO_USB_CDC_ON_BOOT=1
        -DARDUINO_USB_MODE=0
    ; Set the serial monitor baud rate to 115200.
    monitor_speed = 115200
    

  1. Compile and upload after completing the code:
    • 1: Compile the project
    • 2: Upload the code
    • 3: Open the serial monitor

  • Compilation:

  • Upload:

  • After uploading the code, click the serial monitor button and reset the development board to view serial printing.

Add Library Files

  1. Add the following code to the platformio.ini file and save it (replace the URL with the link to the library you need to add):
    lib_deps =
        https://github.com/DFRobot/DFRobot_SHT3x.git
    

The git link can be found in the library file: https://github.com/DFRobot/DFRobot_SHT3x

  1. Return to the main.cpp file to use the library (downloaded library files are under .pio->libdeps).