Open In App

How To Create Custom Arduino Library Using C++

Improve
Improve
Like Article
Like
Save
Share
Report

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-

  • 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. 

Arduino 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:

  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.

Import arduino library

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

  • 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

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-

  • 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. 

#include ""

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. 

Done compiling

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.



Last Updated : 19 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads