Tutorial – How to Create a Library for Arduino IDE

Tutorial – How to Create a Library for Arduino IDE

Hi! In this article we will explain how to create and write a Library for Arduino IDE. In that way you will use your own libraries for your projects!

First of all, what is a library?

What is a Library?

A library is a collection of function and “routines”.

Libraries are particularly useful for storing frequently used routines. That’s because you don’t have to write the same code more times, but you just have to write it ones and put it in your library.

So in that way you can use all the functions in the library just including it and to use a function you have to call it by using its name.

2 main parts: myLib.cpp – myLib.h

A Library has always 2 parts, a .cpp file and a .h file.

In the .cpp file you are going to write all the functions? implementations.

In th .h file you write a list of the functions that you wrote in .cpp file.

So now we will write our very simple first library, of course that’s just an example and the function that we will implement here is not really usefull.

But it’s very simple and it’s okay to learn how to make your own library!

myLib.h File

Now we will write the code for the myLib.h file.

Create a new text file, I use notepad++ to create this files, and name it myLib.h .

#ifndef MYLIB_H
#define MYLIB_H

#include <Arduino.h>

int intSum(int x, int y);


As you can see in this file we just put the function’s name intSum(int x, int y).

Moreover you can also read this: #ifndef, #define and #endif.

You always have to put this 3 rows in your .h file. Those are very important because they are used to don’t include more times the same libraries!

As you can see I had to include the Arduino.h library, of course this library will be included by a lot of other libraries. So how can I avoid that a Library is included more then once?

Using the 3 rows as before, but now we will explian each row:

  • #ifndef MYLIB_H means: if MYLIB_H is not yet defined, go on. If it’s already defined it skips to the #endif
  • #define MYLIB_H : if MYLIB_H is not yet defined it defines it. It’s just a tag.
  • #endif : it’s always put at the end of the .h file

myLib.cpp File

Now we have to write the functions’ implementations.

We will do it in the .cpp file. Create a new file and name it myLib.cpp .

#include "myLib.h"
int intSum(int x, int y)
  return x + y;

As you can see it is a very simple function, but when you will understand how to create your own library I’m sure that you will write very usefull libraries!

Now we have to add the library to the Arduino IDE.

How to add a library to Arduino IDE

Put the 2 files in a folder with the same name, in that case myLib.

Then we can add our libraries to the Arduino IDE in 2 different ways:

  • Create the .zip file of the myLib folder and add it to Arduino IDE following -> this simple tutorial from our website.
  • The second method is about copying manually the files in the Arduino’s libraries folder. Now we will explain how to do it.

Second method

So now you have the 2 files : myLib.h and myLib.cpp in the folder myLib.

Copy the myLib folder in the Arduino libraries folder. Usually the path is: C:\Users\davide\Documents\Arduino\libraries .

After that you have to restart the Arduino IDE and then you can include your library in your projects!

Usage example

We will show you an example that you can copy and paste in the Arduino IDE:

#include "myLib.h"
void setup() 
  int sum = intSum(1,2);
void loop()

You will read the sum in the serial monitor.

That is how to create a Library for Arduino IDE! Thanks for reading!

If you want to know when we publish new articles just enable notifications! Then you can also follow us on Instagram!

Davide Busco

Davide Busco is an engineering student at Federico II University of Naples. He uses his passion and studies to create his projects. He loves be a maker and spread his knowledge. Moreover he has gained the second place in a national makers-tournament between Italian's Universities.

Leave a Reply

Your email address will not be published. Required fields are marked *