Open In App

How To Create Custom Arduino Library Using C++

What is the Arduino library?

Libraries are a collection of precompiled, reusable code or routines which are used by developers to reduce the development time. Arduino libraries are written in C or C++. These libraries provide us with a convenient way to share code. Arduino IDE already consists of a set of standard libraries, one can use these libraries for commonly used functionalities. Apart from standard libraries, one can also create our own library. 
This article focuses on discussing how to create a custom Arduino library.



Why create an Arduino library?

Though there are standard libraries present in Arduino, developers have to create their own library based on their project requirements. Following are some of the benefits of creating and using our library.



  1. Custom libraries provide extra functionalities.
  2. Combines the similar functionalities together.
  3. Reduced code size.
  4. Easy to share (useful in group projects).
  5. Reduced complexity of code.

Steps to write the code and create the Arduino library using C++

This article discusses creating the library related to the DC Motor. The name of the library is “DC_Motor”. Follow the steps below for creating the DC_Motor library-

Our DC_Motor library is created successfully. 

Package your Arduino library:

Follow the steps below to place the custom Arduino library inside the Arduino library folder.

Note:
Standard libraries have one more folder named examples. This folder consists of Arduino code examples using that particular library. While creating our own library, it is good to add sample examples demonstrating the use of the library but it is not mandatory. In this tutorial, I have not added an example folder to make it simple for beginners.

Share your Arduino library

There are two steps to share the Arduino library:

  1. Export the library: To export and share your library, create an archive of the DC_Motor/ folder, located into the Arduino/libraries/. This archive file can be easily shared with other people online. One can easily find websites to host your library archives.
  2. Importing the Arduino library: These steps will work when a .zip archive (not .rar or other extensions) is created when exporting the library.
    • Step 1: Open the Arduino IDE.
    • Step 2: Click on Sketch->Include Library->Add .ZIP Library… and browse to find your .zip archive.

The Arduino IDE will extract the archive and place the imported library under the Arduino/libraries/ folder and will update itself. There is no need to restart it.

How to include the created Arduino library into code

There are two ways to include the Arduino library into the code:

  1. Using Include Library 
  2. Using the #include “” 

Method 1: Using Include Library Option
Let’s follow the steps below to include the Arduino library

 

Now our library is successfully included in the current project. 

Method 2: Using #include “”
There is also another way for including libraries in the project. Follow the steps below to include the library-

How to use the created Arduino library

This section discusses how to use created library. Follow the steps below-




// This is not a C file, This is
// an arduino file
// Make sure your file name is
// "file_name.ino"
  
// Include the header file
// my_library.h
#include <my_library.h>
  
void setup()
{
    // Put your setup code
    // here, to run once:
}
  
DCMotor m(9, 10, 200);
  
void loop()
{
    // Put your main code here,
    // to run repeatedly:
    m.antiClockwise();
    m.motorDelay();
    m.clockwise();
    m.motorDelay();
}

In this way, one can create own library and use it as per our requirements in the projects. It will not only saves them time but also reduce testing cost. One can share own libraries with friends, teachers, and other developers.


Article Tags :