Introduction: A Way to Run Arduino Sketch With VSCode PlatformIO Directly

Instead of using Arduino IDE for developing microcontroller programs (aka sketches), honestly, I frequently use VSCode with PlatformIO plugin for the task, mostly due to the fact that VSCode is a very good development environment with many desirable features for serious coding needs.

Moreover, with PlatformIO plugin, VSCode can be made almost as user-friendly for Arduino sketch development as the original Arduino IDE itself. (I can understand that Arduino IDE is still more user-friendly for many.)

Yes, when I say that the microcontroller programs are sketches, I really mean the program files with extension ino.

I am aware that Platform IO doesn't appear to directly support ino sketch, for whatever reason you might have, PlatformIO can be "tricked" to do so with a little bit of twisting as I will describe in this post.

Here, I will assume that you have VSCode with PlatformIO plugin installed.

For illustration purposes, I will describe the steps to build and upload a fairly complex sketch as mentioned in my previous post Trying Out TensorFlow Lite Hello World Model With ESP32 and DumbDisplay, which will be using two libraries, including the non-trivial TensorFlow Lite ESP32 library.

Based on my experience, VSCode with PlatformIO builds the sketch somewhat faster than Arduino IDE.

Step 1: Create PlatformIO Project

Run VSCode; activate PlatformIO; on the Projects page, click the Create New Project button and enter the following in the Project Wizard dialog

  • Name: ESP32Sketch -- the name doesn't really matter; it is up to you
  • Board: Espressif ESP32 Dev Model -- you actually use the dropdown list fox to select the desired board
  • Framework: Arduino Framework -- this is the default for ESP32

Wait a bit for the project's skeleton with the desired structure to be created -- more precisely, the folder ESP32Sketch is created with initial directories and files.

Step 2: Modify the PlatformIO INI File

Sorry, I will need to describe modifying the Platform INI file, rather than solely relying on the PlatformIO's UI for the configurations, since in some cases, it is actually easier to modify config files than to juggle with UI switches.

One of the initial files created is platformio.ini. Open it and add the following to the very end of the file

monitor_speed = 115200
board_build.partitions = huge_app.csv
lib_deps =
  https://github.com/trevorwslee/Arduino-DumbDisplay
  tanakamasayuki/TensorFlowLite_ESP32@^1.0.0
build_flags =
  -DFOR_ESP32
  • monitor_speed = 115200 -- the serial monitor baud rate to use
  • board_build.partitions = huge_app.csv -- unless you experience issues, suggest always specify that the app is huge
  • lib_deps = ... -- the libraries to use 1) DumbDisplay; and 2) TensorFlow Lite for ESP32
  • build_flags = ... -- can be used to define macros to customize the code; here -DFOR_ESP32 defines the macro FOR_ESP32

The resulting platform.ini will be like

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino

monitor_speed = 115200
board_build.partitions = huge_app.csv
lib_deps =
  https://github.com/trevorwslee/Arduino-DumbDisplay
  tanakamasayuki/TensorFlowLite_ESP32@^1.0.0
build_flags =
  -DFOR_ESP32

Step 3: Modify the Main CPP File

The generated main.cpp in the directory src is the entry point of the microcontroller program.

#include <Arduino.h>

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

void loop() {
  // put your main code here, to run repeatedly:
}

However, for the purpose of building a sketch, you will replace the setup() and loop() with a #include line like

#include <Arduino.h>

#define WIFI_SSID           "<your-wifi-ssid>"
#define WIFI_PASSWORD       "<your-wifi-password>"
#define BLUETOOTH "ESP32BT"

#include "projects/esp32tensorflowlite/ddhelloworld/ddhelloworld.ino"

The sketch ddhelloworld.ino actually comes with the DumbDisplay library; you don't need to explicitly download it. If you ctrl-click the file name, VSCode will open the file for you, and you can see where the file is located.

Step 4: Build and Update the Sketch

That is it!

Even if it isn't really any fancy "trick", it should just do it.

Click the Upload button to build the program and upload it to your ESP32.

Step 5: Use You Own Sketch

I believe you get the idea.

To use your own sketch, just put the sketch directory, say my_sketch, in the src directory.

void setup() {
  Serial.begin(115200);
  Serial.println();  
  Serial.println("*** started ***");  
  Serial.println();  
}
void loop() {
  Serial.println("... looping ...");
  delay(2000);
}

As for main.cpp, it can be as simple as

#include <Arduino.h>

#include "my_sketch/my_sketch.ino"

Now click the Upload button to have the sketch built and uploaded to your ESP32.

Then click the Serial Monitor button to see the sketch outputs to Serial.

Step 6: Enjoy!

For whatever reason you need to develop a sketch with PlatformIO directly, this "trick" should come in handy. Enjoy!


Peace be with you. Jesus loves you. May God bless you!