DIY Projects

ESP32. Getting started coding with PlatformIO IDE and Arduino code

platformio esp32 project structure ini file

PlatformIO (PIO) is an alternative code editor to the Arduino IDE that is installed as a plugin for many more general-purpose code editors. PlatformIO allows the development of most micro-controllers on the market and in particular the ESP32 from Espressif.


Before starting

Before you begin, you must have installed the PlatformIO plugin for the favorite code editor. Here we will be using VSCode. The installation process and the tools offered are similar from one publisher to another.

If you are new to PlatformIO, start by reading this article which explains step by step the installation as well as the basics of operating the environment.

Create a new ESP32 project

PIO is very flexible. It is possible to add a development board to a current project, this is the great strength of PlatformIO, or simply create a blank project. It’s the same option that is used for everything. Select the Initialize or Update PlatformIO Project option from the menu or from the sidebar.

Select your development board. If you are using a generic ESP32 board, choose Espressive ESP32 Dev Module from the list. To add this board to an existing configuration, select it from the list. Otherwise, click Other and specify the folder where PIO should create the project tree.

Warning. Compared to the Arduino IDE, you must create the root folder. Otherwise, the tree and the files will be added to the specified level.

Finally, click Process. PlatformIO takes care of recovering and installing the Espressive SDL for the ESP32 modules. This is the magic of PlatformIO. If you have started programming with the Arduino IDE, you must understand me, otherwise read this article.

Once the SDK is installed, PIO creates the project structure as well as the platformio.ini file

Arduino code demo: flashing a Led

PIO creates the project tree and automatically creates the platformio.ini configuration file. On the other hand it is up to us to create the first Arduino file. To do this, right click on the scr folder and choose New File.

Give the file a name by giving it the .cpp extension

For the program to work, you must include the Arduino.h  library at the beginning of the program! And yes, it is necessary to indicate to PIO that the target must execute code Arduino.

Then, the programming is identical to the Arduino IDE. Here is a small program that flashes a LED connected to pin 15 of the ESP32. It is perfectly identical to the code of ESP8266 presented in the previous tutorial. The only difference is the number of the spindle. Pin No. 15 does not exist on ESP8266. It’s cool to have so many I/O!

 * Demo PlatformIO - ESP32 - ESP8266 - Blink a Led
 * Turns on an LED on for one second, then off for one second, repeatedly.
 * -

#include <Arduino.h>

#define LED_BUILTIN 15

void setup()
  // initialize LED digital pin as an output.

void loop()
  // turn the LED on (HIGH is the voltage level)
  digitalWrite(LED_BUILTIN, HIGH);
  // wait for a second
  // turn the LED off by making the voltage LOW
  digitalWrite(LED_BUILTIN, LOW);
   // wait for a second

Adapt the code to your needs and save it (CTRL + S). Then choose Build from the menu (or use the dedicated icon).

You can follow the progress of the compilation by flying over the toothed wheel in the lower right corner of Atom.

Once the compilation is complete, upload the program.

Now open the serial monitor. Directly from the menu, via the keyboard shortcut Alt + Shift + M or from the icon bar.

Select the COM port on which the board is connected and the speed

And then, we trace the course of the program for any other board as on the Arduino IDE.

Click to rate this post!
[Total: 0 Average: 0]
Exit mobile version