Possible to share a Config.h file between multiple sketches?

I have an arduino bot with a touchscreen, controlled by a bot_control.ino sketch.

I also have a calibration sketch, callibration.ino that should be run to obtain the correct touchscreen calibration values used in the bot sketch.

Here is my project code layout:

My_Project/
-- bot_control/
-- -- bot_control.ino
-- calibration/
-- -- calibration.ino

Since both sketches utilize the same pin definitions, I want to create a config.h file to store these definitions, which can be loaded by each sketch.

Is this possible? If so, where could this config.h file exist within the directory structure.

I am using the Arduino IDE.

One idea off the top of my head, create a directory under libraries, give your header file a good unique name and put it in there. Then #include <good_unique_header_file.h>. I think you can get away without a library.properties file.

I'm not actually creating a library, just organizing my sketches outside of the default sketchbook

Hi @rhammell.

Libraries are the mechanism by which code is shared between multiple sketches in the Arduino world.

The location of the sketches is completely irrelevant to your goal.

The sketchbook is a convenient place to store sketches, and Arduino IDE does make it easier for the user to access these sketches via the File > Sketchbook menu and the "Sketchbook" panel. However, when it comes to compiling the sketch, there is absolutely no technical difference between a sketch stored under the sketchbook folder and one stored outside the sketchbook folder.

1 Like

Thanks, so you would suggest creating a library for this use case of making a config file acessible to both scripts?

This just seems off, as the typical use case for libraries is to make code extenally availble to other users, correct?

Yes. The alternative would be to use a symbolic link (or the equivalent alternatives) to make the shared files be present under each of the sketch folders.

It is a matter of perspective. You could also say that the typical use case for libraries is to share code between multiple sketch projects. It might be that those multiple sketch projects happen to be created by different people, or maybe they are all by the same person.

So my updated structure would look something like this?:

My_Project/
├── libraries/
│   └── RobotConfig/
│       ├── src/
│       │   ├── config.h
│       │   ├── config.cpp (if needed)
│       │   └── RobotConfig.h (main library header)
├── bot_control/
│   ├── bot_control.ino
└── calibration/
    └── calibration.ino

And i would include #include <RobotConfig.h> in both sketches

Curious about opinions - is this overhead worth it, or would just copying the pin definitions into each sketch be better and not bother with a shared config file?

Only If My_Project/ is the sketchbook folder. Libraries must be installed under the libraries subfolder of the Arduino sketchbook folder. Unlike sketches, the sketchbook folder does have technical significance for libraries.

If RobotConfig.h provides the code you want to include in your sketch, and that file is present in a library installed under the libraries subfolder of the sketchbook folder.

The first thing to understand is that creating a library is not some onerous task. You only need to create a folder under the libraries subfolder of the sketchbook folder and move your header file into that folder:

<sketchbook location>/
├── libraries/
│   └── RobotConfig/
│       ├── RobotConfig.h
...     ...

A .cpp file is not required. Although it is best practices to give the header file and the library folder the same names, this is not absolutely mandatory.

You can put the library code files under the src subfolder of the library (the "1.5 Arduino library format") if you like, but if you do that you must also add a library.properties metadata file to the library if you choose to do that. If you instead put the code files in the root of the library folder (as shown in the diagram above), then the metadata file is not required and the library can consist of as little as a single header file.


So the effort required to make a library like this is utterly trivial and not worth considering as "overhead".

The main drawback of using a library as compared to just putting a copy of the file in each sketch is that the library approach is more complex. If you want to back up or distribute your project, you must remember the library dependency in addition to the sketches.

The drawback of putting a copy of the file in each sketch as compared to using a library is that you will need to maintain two copies of the same code.

So I think the "worth it" determination will depend on how often/likely the content of the header file will need to be modified over time.

there is no "overhead".
Just put your common files in a dedicated folder within your sketchbook/libraries/ and include them.
For example I'm using one central config.h with my WiFi credentials and include it in in all my Wifi releated sketches.

This is the 'overhead' i'm talking about. Indeed, there is a tradeoff with going with the other option of copying the config file, or even just skipping the config file all together and keeping pin definitions within each file.

I have something similar, and since every touchscreen is slightly different i decided to run the calibration sketch and store the obtained values in EEPROM for the other sketch to use. Works like a charm only requires that you store the values in a location in EEPROM that is known to both.

Somehow there was some conflict on using the first 20 EEPROM addresses which may have been a conflict with the library i used (mcuFriend)