AVR 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.
Copy the prompt, then paste it into Gemini’s message box.
View prompt
Use PlatformIO IDE to set up the development environment for the AP01 AVR MCU leaf (ATmega328P, 3.3 V, 8 MHz).
Introduction
Section titled “Introduction”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.
What to prepare
Section titled “What to prepare”- Basic Kit 2
- A Windows, macOS, or Linux computer
Advance preparation
Section titled “Advance preparation”Install Python
Section titled “Install Python”PlatformIO IDE requires Python. Follow the Python installation guide (Japanese) to install it.
Install PlatformIO IDE
Section titled “Install PlatformIO IDE”- Install VS Code.
- 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 IDESerial MonitorTeleplot
Getting Started
Section titled “Getting Started”Create a new project
Section titled “Create a new project”This Hello World example introduces the PlatformIO IDE workflow.
- Start VS Code and open File → New Window.
- Select PlatformIO in the left sidebar to open PLATFORMIO QUICK ACCESS.
- Select PIO Home → Open.
- 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:
Hello_World_Pjt - Board:
Arduino Pro or Pro Mini ATmega328(3.3v,8Mhz) - Framework:
Arduino
- Name:
- Open
platformio.iniand check the board and framework settings:
[env:pro8MHzatmega328]platform = atmelavrboard = pro8MHzatmega328framework = arduino- The AVR MCU upload workflow detects the serial port automatically, so a COM port number does not need to be specified.
- Add the serial monitor baud rate to
platformio.ini:
monitor_speed = 115200- Open
src/main.cppand 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: Hello_World_Pjt
- Use Project Environment Switcher in the bottom PlatformIO toolbar to select the correct active project, then select Build.

- Connect Leafony to the computer as shown in Connecting to PC.
- Select Upload to upload the program to the MCU.
- Press the MCU reset button to run the program.
- Open Serial Monitor and check that
Hello Worldappears.

Install the library
Section titled “Install the library”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 |
Serial plotter
Section titled “Serial plotter”To plot serial data as in Arduino IDE, use the Teleplot for VSCode extension. Download and try the sample below.
- Download Teleplot_Example_1 from GitHub and save it in your project folder.1
- Run the program.
- Select teleplot at the bottom of the window, choose the COM port, and select Open to display the graph.
- 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));AVR Beginner Examples
Section titled “AVR Beginner Examples”Download the sample projects from GitHub, save them in your project folder,1 and run them in PlatformIO IDE.
| Item | Source code |
|---|---|
| Temperature and humidity sensor | Thermo-Hygrometer_Pjt |
| Illuminance sensor | Illuminance_Meter_Pjt |
| Accelerometer | Accelerometer_Pjt |
MCU and board settings
Section titled “MCU and board settings”| Type | Leaf | Platform | Board | Upload |
|---|---|---|---|---|
| AP01 | AVR MCU | atmelavr | Arduino Pro or Pro Mini ATmega328(3.3v,8Mhz) | Automatic COM port detection |
| AP02 | ESP32 MCU | Espressif 32 | Espressif ESP32 Dev Module | Automatic COM port detection |
| AP03 | STM32 MCU | ststm32 | LEAFONY_AP03 | COM port cannot be detected automatically |
Differences from Arduino IDE sketches
Section titled “Differences from Arduino IDE sketches”- 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. - 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:
// Prototypesvoid func(void);
void setup() { // put your setup code here, to run once: func();
}
void func() { ;}Rename or delete a project folder
Section titled “Rename or delete a project folder”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.
Change the project folder location
Section titled “Change the project folder location”Configure the location from the terminal:
- Start VS Code and open File → New Window.
- Select PlatformIO in the sidebar to open PLATFORMIO QUICK ACCESS.
- Select PIO Home → Open.
- Run
pio settings set projects_dir <absolute-path>with your desired folder’s absolute path. - Restart VS Code.