How To Create Custom Arduino Library Using C++
Last Updated :
19 Dec, 2021
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.
- Custom libraries provide extra functionalities.
- Combines the similar functionalities together.
- Reduced code size.
- Easy to share (useful in group projects).
- 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-
- Step 1: Create a folder and give the name to that folder as DC_Motor.
- Step 2: Inside the DC_Motor folder, create two files. One is "my_library.h" and another one is "my_library.cpp".
- my_library.h- It is the header file. The header file should consist of only the declarations associated with the library.
C++
// Header file
// Make Sure your file name
// should be my_library.h
#ifndef MY_LIBRARY_H
#define MY_LIBRARY_H
#include <Arduino.h>
class DCMotor {
private:
byte pin1;
byte pin2;
int speed;
public:
DCMotor(byte, byte, int);
void clockwise();
void antiClockwise();
void stop();
void motorDelay();
};
#endif
- my_library.cpp- It is the C++ file. C++ file contains all the definitions of the functions which are declared within my_library.h
C++
// C++ program to include the
// custom header file
// Include statement to include
// custom header file
#include "my_library.h"
DCMotor::DCMotor(byte pin1, byte pin2, int speed)
{
this->pin1 = pin1;
this->pin2 = pin2;
this->speed = speed;
}
// Function to rotate DC motor
// anti-clockwise
void DCMotor::clockwise()
{
analogWrite(pin1, speed);
analogWrite(pin2, 0);
}
// Function to rotate DC motor
// clockwise
void DCMotor::antiClockwise()
{
analogWrite(pin1, 0);
analogWrite(pin2, speed);
}
// Function to stop DC motor
void DCMotor::stop()
{
analogWrite(pin1, 0);
analogWrite(pin2, 0);
}
void DCMotor::motorDelay() { delay(1000); }
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.
- Step 1: The next step is to make a copy of the library folder DC_Motor under the library folder of the Arduino. Open the Arduino IDE path where the Arduino software is installed. In this case, it is "C:\Program Files (x86)\Arduino\libraries".
- Step 2: In this libraries folder, all the standard libraries present in Arduino are located. Copy the DC_Motor folder and paste it here in the libraries 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:
- 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.
- 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:
- Using Include Library
- Using the #include ""
Method 1: Using Include Library Option
Let's follow the steps below to include the Arduino library
- Step 1: Open Arduino IDE.
- Step 2: Create a new sketch.
- Step 3: Click on the Sketch menu inside the menu bar.
- Step 4: Select the Include Library option from the dropdown.
- Step 5: One can see all the libraries present in Arduino IDE. User-installed libraries are present under Contributed libraries heading and then choose DC_Motor 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-
- Step 1: Create an Arduino sketch inside the library folder DC_Motor itself.
- Step 2: Instead of #include <my_library.h> write #include "my_library.h" (including ""). Here, we are specifying the path of the header file. There is one restriction that the Arduino project file must be inside the DC_Motor directory.

How to use the created Arduino library
This section discusses how to use created library. Follow the steps below-
- Step 1: Open an Arduino IDE.
- Step 2: Create a new sketch.
- Step 3: Import the required library as mentioned in the above section.
- Step 4: Write an Arduino code. The extension of the Arduino sketches is ".ino".
C
// 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();
}
- Step 5: Verify the Arduino sketch by clicking the correct-tick icon in the menu bar in an IDE. If your code as well as is correct then the message Done Compiling will be displayed else an error message will be displayed.

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.
Similar Reads
How to Create a Dynamic Library in C++?
In C++, dynamic libraries also known as shared libraries are a powerful way to modularize your code. They allow the users to build libraries that can be loaded at runtime by multiple applications at once. This approach promotes code reuse, reduces code duplication, and simplifies maintenance of the
4 min read
How to Manipulate cout Object using C++ IOS Library?
C++ ios_base class has its aspects to format cout object to display different formatting features. For example, the following ios_base class can format cout object to display trailing decimal points, add + before positive numbers, and several other formatting features using class scope static consta
5 min read
Strings in C++ and How to Create them?
Strings in C++ are used to store text or sequences of characters. In C++ strings can be stored in one of the two following ways: C-style string (using characters)String class Each of the above methods is discussed below: 1. C style string: In C, strings are defined as an array of characters. The dif
2 min read
How to Parse an Array of Objects in C++ Using RapidJson?
RapidJSON is an open-source C++ library for parsing and serializing JSON (JavaScript Object Notation) data. It is designed to be fast and efficient, with a focus on simplicity and ease of use. It is widely used in a variety of applications and is known for its fast performance and low memory overhea
4 min read
How to add "graphics.h" C/C++ library to gcc compiler in Linux
While trying c graphic programming on Ubuntu, I figured out that graphic.h is not a standard C library and it is not supported by gcc compiler. So I am writing this article to explain the process.If you want to use graphics.h on Ubuntu platform you need to compile and install libgraph. It is the imp
2 min read
How To Compile And Run a C/C++ Code In Linux
C Programming Language is mainly developed as a system programming language to write kernels or write an operating system. C++ Programming Language is used to develop games, desktop apps, operating systems, browsers, and so on because of its performance. In this article, we will be compiling and exe
4 min read
Difference between user defined function and library function in C/C++
Library Functions These functions are the built-in functions i.e., they are predefined in the library of the C. These are used to perform the most common operations like calculations, updation, etc. Some of the library functions are printf, scanf, sqrt, etc. To use these functions in the program the
3 min read
How to Install C++ Boost Libraries on Windows?
Boost library is a set of a popular collection of peer-reviewed, free, open-source C++ libraries. It supports a number of tasks such as unit testing, image processing, multithreading, and mathematical aspects such as linear algebra and regular expressions. You can also store, numbers that are out of
2 min read
Difference Between STL and Standard Library in C++
In C++, the term "Standard Library" and "Standard Template Library" are often misinterpreted as the same. Although they sound same with only a single word difference, they refer to the different part of the C++ programming language. In this article, we will learn what's the difference between the C+
3 min read
Which C++ libraries are useful for competitive programming?
C++ is one of the most recommended languages in competitive programming (please refer our previous article for the reason) C++ STL contains lots of containers which are useful for different purposes. In this article, we are going to focus on the most important containers from competitive programming
3 min read