How do I put code in dedicated files for re-use ACROSS sketches?

Hey everyone,

I have a couple of Sketches that share a lot of common code like functions to connect to WiFi, send MQTT messages etc. To reduce maintenance effort, I would like to put this code into a libraries folder from where it can be included into all Sketches.

The only hints I found about making libraries is to put them in a libraries folder inside the Sketch (e. g. This is not what I need.

I tried C++ style relative imports like this

#include "../../libraries/mySerial/mySerial.h"

void setup() {

    printToSerial(&Serial, "Hello World!");


void loop() {


but Arduino IDE throws an error:

modularization-test:1:47: fatal error: ../../libraries/ppSerial/ppSerial.h: No such file or directory
 #include "../../libraries/mySerial/mySerial.h"
compilation terminated.
exit status 1
../../libraries/mySerial/mySerial.h: No such file or directory

The directory structure is

|- sketches
|    \- sketch_1
|         \- sketch_1.ino
\- libraries
     \- mySerial
           |- mySerial.h
           \- mySerial.cpp

The relative path itself is correct (I can do "cat ../../libraries/mySerial/mySerial.h" from the sketch directory).

What am I doing wrong?

Thanks for your help!


Regarding the relative path, what you’re missing is that before compilation the sketch is copied to a temporary build folder, but the library is not, so the relative path is broken.

You should install your library in the normal Arduino manner, to the libraries subfolder of the sketchbook folder. You can find/set the location of the sketchbook folder in the Arduino IDE at File > Preferences > Sketchbook location.

After that, your #include directive should look like this:

#include <mySerial.h>

Arduino CLI has a --libraries option that allows you to define additional libraries paths:

--libraries strings          List of custom libraries paths separated by commas. Or can be used multiple times for multiple libraries paths.

The Arduino IDE doesn’t provide this feature, though there has been discussion of adding support for this sort of thing.