Does anyone know of a site or book where you can learn how to create, or for first reed, libraries. Because I think the libraries I use are sometimes unnecessary complicated, or have similar features, for example Wire and OneWire.

I think, these make my sketch unnecessary big, when I need both of them.

  • Wire and one wire are two different things. If you are looking for arduino libraries, there are tutorials on how to make your own on the arduino site, sparkfun might have something and I might be wrong here, but Arduino cookbook, might have.
    – RSM
    Commented Apr 25, 2015 at 11:10
  • 2
    Just look at the source code of a few libraries. That's the advantage of open-source.
    – Gerben
    Commented Apr 25, 2015 at 15:14
  • arduino.cc/en/Reference/APIStyleGuide - cplusplus.com
    – Majenko
    Commented Apr 25, 2015 at 19:48

2 Answers 2


Arduino has a library tutorial here: http://www.arduino.cc/en/Hacking/LibraryTutorial

and a formal specification here: https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5:-Library-specification

these explain the specifics to making arduino libraries, like the propeties files, the keywords file, and the directory structure. Most of that is unnecessary though.

Anyway, there are not many differences between the actual code in an arduino library and the normal code you write for arduino sktech. The only major ones being that

  • The arduino preprocessor doesn't generate function declarations - you have to make your own header file
  • You can't use setup or loop

Other than that, its just normal c++ code that exposes a particular interface and provides an implementation. If you want the library to work well for a lot of users, you will need to put in the extra work to make it run on all the different arduino hardware variants.

Making libraries for c++ often involves creating .a, .so, .dll etc. files of compiled code that new programs can link against, but arduino compiles everything from source for every sketch so you don't need to worry about that here.


To address your query about libraries making sketch code too big...

I have recently completed (or rather, am in the process of completing) my first library, SevenSegmentSL1255. I too was concerned about the growing size of the library, as I incorporated more and more ideas. So I decided to plan for a "Lite" version, SevenSegSL1255Lite, so that the casual user would not feel to overwhelmed and also to reduce the memory footprint, for simple sketches.

However, after reading the Arduino page on the build process, I discovered that the whole library is not necessarily included in all sketches:

The .c and .cpp files of the target are compiled and output with .o extensions to this directory, as is the main sketch file and any other .c or .cpp files in the sketch and any .c or .cpp files in any libraries which are #included in the sketch.

These .o files are then linked together into a static library and the main sketch file is linked against this library. Only the parts of the library needed for your sketch are included in the final .hex file, reducing the size of most sketches.

So you see, from a memory footprint point of view, no matter how big the library may be, the resultant compiled sketch can still be quite small, depending on the amount of the library that is called for the sketch. From my point of view, this makes the "Lite" version of my library now somewhat redundant. Although, the argument for a simpler, more user friendly library may still exist.

As an aside, the resources that I used in creating the library are listed below:

  • True, as long as it's clear that the smallest unit that may be included or excluded is an entire library module, such as an EEPROM module. In the context, 'library' means 'collection of .o files, and 'parts of the library' means individual .o files. ...
    – JRobert
    Commented Apr 27, 2015 at 20:18
  • ... If your code doesn't call any functions contained in the EEPROM library module, and none of the library modules that you DO call makes any calls to the EEPROM module, then that module will not get linked into your code. If, however, there is one call to one tiny function in that module, the whole module will be linked (generally speaking; there is currently work being done delay all or most code optimization until link-time, which may possibly yield finer-grained inclusion/exclusion). Thus writing a Lite version or writing sub-modules may well be useful to keep the linked code image small.
    – JRobert
    Commented Apr 27, 2015 at 20:19
  • @JRobert - Thanks, I may need to look into this further, as I appear to have mis-interpreted the quote in my answer. Commented Apr 28, 2015 at 1:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.