[solved] Define different pins for different boards--platform.ini?

Hi, I would like to write one program to run on different boards. How do I declare the pin numbers for each board. For example if on one board my led is pin 5 and on the other board it is pin 13 (maybe there is a third or fourth board with different pinout), where can I put this information so it is built properly. Can it go in platform.ini?

1 Like

Sure. I am assuming you are working with C/C++.

In your main C/CPP file use the preprocessor directive.

#ifdef BOARD1
//Pin definition
#endif

#ifdef BOARD2
//Pin definition
#endif

to define pin definitions for various boards. Then in your platformio.ini file
use the build_flags for each environment to define which board pin definitions you want to use

[env:TYPE1]
board = type_1
platform = whatever_platform
framework = whatever_framework
build_flags = -DBOARD1

[env:TYPE2]
board = type_2 
platform = whatever_platform
framework = whatever_framework
build_flags = -DBOARD2

Now depending on which board you wish to build the project for, only that particular board pin definitions will be compiled and built. Of course, any code where you are accessing the pins also needs to be wrapped in the same #ifdef directives throughout the source.

Look at some of the examples in the platformio. They are build using the same way. Hope this helps! :slight_smile:

6 Likes

That’s exactly what I was looking for. Thanks @krishna_chaitanya.

1 Like

You are welcome! :slight_smile:
Glad I could be of some help.