hello,
I am new to platformio, I need to use it for an arduino library I am working on. I was wondering which is the best way to work on an arduino project/library because the platformio structure differ a lot to the arduino library
example of an arduino lib:
├───examples
│ ├───advanced_working
│ └───basic_working
├───extras
│ └───…
└───src
Where the source code are in ‘src’ and in examples there are some .ino sketches
What do you suggest me to have a clean as possible folder structure as the arduinio one? i need it because I am pubblishing it to github and to the arduino library manager
1 Like
After a lot of googling i found how to procede, i will write a short guide in the case someone will have the same problem:
- go to the root folder (where there is the library.properties file) of the library which you want to develop and run
pio init
-
Open Project
with your favourite ide
- in
platformio.ini
write:
[platformio]
src_dir = examples/your_sketch_name
lib_dir = src ;yes it appear to be wrong but is right
- enjoy platformio!
I also opened a issue on the docs repo tutorial about how to develop arduino libraries · Issue #46 · platformio/platformio-docs · GitHub hoping @ivankravets or @valeros would see it
Hi @aster94,
It does not work for me…
This is my platformio.ini file:
[platformio]
src_dir = examples/my_example
lib_dir = src
[env:xmc1100_xmc2go]
platform = infineonxmc
board = xmc1100_xmc2go
framework = arduino
Also would it be possible to have several targets for several “examples” in the platformio.ini file? To compile several examples with several platforms-boards?
Thanks!
The idea is basically to do as the “platformio ci …” command for different SRC, but included already in the platformio.ini file.
That would be great for Arduino development with VSCode or Atom.
@ivankravets is there any example of such platformio.ini configuration with src/ and exapmples/ dir in the arduino lib folder structure as platformio project?
Thanks a lot in advance 
BR,
jaenrig-ifx
It looks correct
First of all try to compile the sketch in the arduino ide just to be sure everything is all right
If it work it is possible that the problem is due to the included libraries, at this time pio has some problems with it On multi environment project all libraries are included/Compiled · Issue #1696 · platformio/platformio-core · GitHub
Thanks @aster94. There is no other library dependency in my code… and the example is working after installing the Arduino library in the Arduino IDE.
I am using visual studio code and this PlatformIO Versions:
Home 2.0.2 + Core 4.0.0a13
In the command line, using platformio ci it works perfectly
platformio ci --lib="." --board=xmc1100_xmc2go examples/my_example
Okay. Now it works 
[platformio]
src_dir = examples/my_example
lib_dir = .
[env:xmc1100_xmc2go]
platform = infineonxmc
board = xmc1100_xmc2go
framework = arduino
The idea is now to be able to compile diffrent “examples” for each environment in the platformio.ini file…
I guess your header and cpp file where in the root folder 
You may want to move them to src even if not needed (Arduino IDE 1.5: Library specification · arduino/Arduino Wiki · GitHub)
About the different examples there is workaround: you can define a flag for each board and then with some #ifdef… #endif you could choose which part of the code to upload to which target
Actually I have the lib in src:
src/
|_ class.h
|_ class.cpp
|_ dir1/
I guess I preferably use ci for compiling multiple src and environments. During development it is enough to be able to change the src example folder.
Thanks for your support!
There are a lot of ways how to organize examples and test them with PlatformIO.
Yes, the easiest way is to use Redirecting...
Also, other option is to place platformio.ini
into the each arduino example folder and override src_dir as
[platformio]
src_dir = .
[env:uno]
....
board = uno
lib_deps = ../../relative path to lib
Later, you can process these examples with pio run
command.
1 Like
Has this implementation changed - it’s not working for me.
Please see the following library:
I have the following in my platformio.ini:
[platformio]
src_dir = examples/SerialExample
[env:d1_mini]
platform = espressif8266 ;@1.6.0
board = d1_mini
framework = arduino
upload_port = /dev/cu.wchusbserial14130
lib_ldf_mode = deep+
lib_extra_dirs = …
lib_deps =
ArduinoJson
On compiling, I get the following error:
src/IPGeolocation.cpp:4:25: fatal error: ArduinoJson.h: No such file or directory
hi @jaenrig-ifx looks like that since i wrote the first time something changed and I also had to use a platformio.ini
like yours. Unfortunately i can’t edit so i am writing here all the possible solutions:
#1 work from the root folder of your arduino lib
[platformio]
src_dir = examples/my_example
lib_dir = .
#2 work from an example of your arduino lib
[platformio]
src_dir = .
[env]
lib_deps = ../../relative path to lib
1 Like