PlatformIO Community

How to use multiple target boards with same project

I am using PlatformIO with VScode. Mostly on Arduino and ESP family. I recently learned how to have several designs in same project and easily connect to GitHub. Cool ! I still don’t know how to use same design to different target. Let’s say I have a design that blinks a LED. How can I use same project in VScode & PlatformIO, compile and upload to Arduino, and then to ESP32? I assume I need to change the board somehow in Platformio.ini, and also perhaps change the pins definition in the C code, because the GPIOs may not be exactly the same. Any simple way to do that?

Whenever I get a new board I add it to my blink project. You might find it useful as an example: https://github.com/blackketter/blink

I put defines for the LED pins in the source code, but you could add them to a build_flags line in your platformio.ini file.

By “Arduino” you mean like an Arduino Uno here?

Read through the documentation at https://docs.platformio.org/en/latest/projectconf/index.html.

You can have multiple available environments with the same code base. But you can also differentiate the code based on the target. E.g.

[platformio]
;default_envs = uno, esp32

[env:uno]
platform = atmelavr
board = uno
framework = arduino

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

will give you two environments to work with. You can compile each seperately or all of them (the default environment, which is by default “all”, but also selectible via default_envs).

In the code you can work with #ifdef <macro> to check wether it’s compiling for a specific target. You can put the macros in yourself via build_flags (see docs) or rely on pre-included macros like ARDUINO_ARCH_ESP32 etc.

so in C++ code we can do

#include <Arduino.h>

#ifdef ARDUINO_ARCH_ESP32
#define BLINKY_PIN 2
#else
#define BLINKY_PIN 13
#endif 

void setup() {
   pinMode(BLINKY_PIN, OUTPUT);
}

void loop() {
   //...
}

Thanks! This is exactly what I needed. yet I miss something. Let’s assume I have one ESP8266, one ESP32, and one WEMOS device. The code is written with the #define. Now I want to compile and upload to the ESP8266. I need to guide the system somehow to use the 8266 option from the platformIO.ini, and also to set the right #define, to let the pre-processor to use the 8266 portion of the code.
Later on, another device is to be selected, and so on.
How to manager the “I am working now on board xxx”?

See docs (https://docs.platformio.org/en/latest/integration/ide/vscode.html#project-tasks).

Once you have that example platformio.ini above the task explorer will show you all available environments.

grafik

grafik

Build and upload and everything can be access separately for each environment.

However, code completion in VSCode will be deduced from the first active environment. (VSCode cannot sensibly give you code completion for all environments at once). As said above, that can also be selected by the default_envs section, documented in the links above.

Since my first env is uno here, the environment will be picked up and automatically e.g. contain ARDUINO_ARCH_AVR. Thus IntelliSense correctly says

grafik

If I activated default_envs = esp32 I would get the intellisense for the ESP32 environment. See docs.

From the commandline you can also run pio run -e <environment name> -t <target>.

1 Like