Home » Arduino » Arduino Library: How to Create Your Own

Arduino Library: How to Create Your Own

One of the strengths of the Arduino platform is the extensive library included in its IDE. Libraries make life simplier for coders; imagine how hard it would be if there's no LiquidCrystal or Servo library ready for use. In my experience, projects were done in shorter time because of the built-in and third-party Arduino library you can freely use.

The Arduino community has created a number of libraries over the years. These are the official libraries. But there are still applications that need their own libraries. This simple tutorial will outline the steps needed to create your own third party Arduino library.

Introduction to Arduino Library

Here I'll create a simple library that blinks a LED on a pin. Normally, you would code like this to blink a LED on a pin:

With my library, you will be blinking a LED on a pin like this:

Note that my library doesn't offer any advantage over the original method and was created just for demonstration.

A library needs two files to be valid: a header file and a source file. In my code above, the header file is Blink.h. It is added to the sketch by using the #include directive.


The header file consists of a descriptive comment, constructs (the one with #ifndef Blink_h and #define Blink_h), needed include files (#include <Arduino.h>), constant definitions (#define ON true, etc.) and a class that contains the functions and variables to be used in your library. Let's examine each part one by one.

Descriptive Comment

This part explains what your library is supposed to be. When creating a library, think about other people that might use it someday, whether the library is free or paid. Write it in a way that is simple to understand. Here' I've even added the methods of my library with short explanations on each.


The construct is just a simple way to prevent problems if ever the user adds the #include <Blink.h> twice on his/her code.

Include Files

You can also add a header file inside another header file if there are functions that you need from that header. Here I include Arduino.h because I will be using some of its built-in functions.

Constant Definitions

You can also give name to constants that can be used with your library. Here I have two constants ON and OFF which is equal to boolean true and false respectively.


The class contains the functions and variables to be used in your library. Note that the actual way to use the functions are not in the class (they are on the .cpp file). They are just listed here for reference.

Your functions and variables can either be public or private. Public ones are user accessible while privates ones are only used by the functions themselves.  Here I have three public methods and the constructor Blink() which is required and should have the same name as your class.

Notice that I have three methods that are named blink(). This is acceptable as long as they don't have the same parameters. The first blink() method accepts boolean value only, the second method accepts both boolean value and integer blinkLength while the third method accepts boolean value, integer blinkLength and integer blinkLoops.

Here I only have one private variable: pinNumber.

Source File

That's it for the header file. Now let's look at the source file.


The actual use of the methods and variables on the header file is on this file. Notice that it includes two headers:
The Arduino.h is needed because I need to use some of the built-in Arduino functions. Of course, I also need a reference to the header file blink.h.

The part:

is the constructor which also explains what should happen when someone creates and instance of my class. Here the user needs to specify which pin to blink.

Usable Functions:

I include three functions: one where the user can turn on a blink sequence with 1 second interval between on and off; another where the user can specify the interval in milliseconds and another where the user can also specify how many times the blink loop will occur.

Using the Library

To use the library, you need to have the two files (blink.h and blink.cpp) inside a folder (let's say blink) and then that folder must be inside the Arduino libraries folder. On my computer, the libraries can be found on C:\Users\<User Name>\Documents\Arduino\libraries. (Replace <User Name> with your own computer user name). Once transferred, the library will now appear as pictured:

Assigning Keywords

You can also tell assign keywords that are used by your library. Once they are known by the IDE, they will automatically turn orange when included in the sketch. To do this, create a keyword.txt file on the blink folder with content like this:

There are three types of keywords you can have: Datatypes (KEYWORD1), Methods and Functions (KEYWORD2) and Constants (LITERAL1). With the keyword.txt file in place, my editor changes the color of my class name (Blink) and function (blink):

Create your own Arduino library now and be part of the Arduino open source community!

Found this post useful? Please drop a comment below!

Leave a Reply

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