How To Create a Custom Arduino Library for ESP32 [2025]

Do you keep copying the same functions into every Arduino sketch you write? Or maybe you’ve built a neat piece of code for your ESP32 that you’d like to reuse, or even share with others? In this guide, you’ll learn how to create your own custom Arduino library for ESP32. In addition, I’ll cover how to publish and share your library with others!

Table of Contents

    Custom Arduino Library Project Setup

    For this guide, we create a simple blink library for blinking an LED connected to the ESP32. I want the users to specify a pin and a blink frequency to automatically blink an LED.

    Let’s start with setting up the project structure:

    1. Go to your Arduino libraries folder (usually found at Documents/Arduino/libraries).
    2. Create a new folder named MyLibrary.
    3. Inside that folder, create this file:
      • library.properties
    4. Then, create a new folder named src inside MyLibrary.
    5. Inside the src folder, add the following files:
      • MyLibrary.h
      • MyLibrary.cpp

    Your folder structure should now look like this:

    Arduino/
    |-> libraries/
        |-> MyLibrary/
            |-> library.properties
            |-> src/
                |-> MyLibrary.h
                |-> MyLibrary.cpp

    Writing the Library’s Header File

    The header file of your library contains the class definition and function declarations. That is the basic structure of your library’s code. Let’s create a simple class that can blink an LED on the ESP32.

    MyLibrary/src/MyLibrary.h
    #ifndef MYLIBRARY_H
    #define MYLIBRARY_H
    
    #include <Arduino.h>
    
    class MyLibrary {
      public:
        MyLibrary(int pin);        // Constructor
        void blink(int delayMs);
      private:
        int _pin;
    };
    
    #endif

    Note that we use the constructor to set the LED pin. Later, in the .cpp file, we will also initialize that LED pin in there. Also, we declare a function called blink() that takes a delay in milliseconds and blinks the LED.

    Always make sure to include <Arduino.h> in your header file! Otherwise, your library won’t have access to basic Arduino functions like digitalWrite().

    #ifndef, #define, and #endif act as an include guard, preventing the file from being included multiple times, and should always be added to your header file.

    Writing the Implementation of the Library

    Now, let’s implement the constructor and blink functions. This is done inside the MyLibrary.cpp file, which contains all the implementations of the MyLibrary.h file.

    MyLibrary/src/MyLibrary.cpp
    #include "MyLibrary.h"
    
    MyLibrary::MyLibrary(int pin) {
      _pin = pin;
      pinMode(_pin, OUTPUT);
    }
    
    void MyLibrary::blink(int delayMs) {
      digitalWrite(_pin, HIGH);
      delay(delayMs);
      digitalWrite(_pin, LOW);
      delay(delayMs);
    }

    As you can see, in the constructor, we save the pin parameter in a private attribute _pin of the MyLibrary class. In addition, we initialize the pin with pinMode() to be an output.

    Next, we define the blink function to blink the LED with the given delay (delayMs).

    The library.properties file

    This file tells the Arduino IDE what your library is about. Add the following content to your library.properties file and adjust it so it fits your library.

    MyLibrary/library.properties
    name=MyLibrary
    version=1.0.0
    author=Your Name
    maintainer=Your Name <you@example.com>
    sentence=A simple Arduino library for ESP32 blinking an LED.
    paragraph=This is a beginner example to demonstrate how to create your own Arduino library.
    category=Other
    url=https://coolplaydev.com/custom-arduino-library-for-esp32
    architectures=esp32

    Make sure that the name parameter matches the name of your library.

    Currently, the library would only support ESP32 boards (see architectures). If you want it to work on all boards, replace “esp32” with an asterisk (*). You can also separate specific architectures with a comma:

    architectures=esp32,esp8266

    Adding an Example Sketch

    If you’d like, you can also include an example sketch in your library that users can use to quickly familiarize themselves with the basics of how your library works.

    To do so, inside your MyLibrary projects folder, create a new folder called examples. Then, inside that folder, create another folder that has the name of your example sketch, and add a .ino file with the same name inside that folder.

    Here’s a basic example sketch that makes use of our library to blink an LED:

    MyLibrary/examples/SimpleBlink/SimpleBlink.ino
    #include <MyLibrary.h>  // include your library
    
    MyLibrary led(2);      // create led object
    
    void setup() {
    }
    
    void loop() {
      led.blink(500);      // blink LED
    }

    Testing your Library for ESP32

    Let’s try and use our own library from within the Arduino IDE!

    To get started, restart the Arduino IDE so it reloads the library files.

    Next, we want to load the example sketch we created in the section above. Therefore, click on File > Examples and search for your example sketch under Examples from Custom Libraries > MyLibrary.

    ESP Custom Arduino Library - Open Example Sketch 1
    ESP32 Custom Arduino Library - Open Example Sketch 2

    You should now see the example sketch you created earlier:

    Custom Library Example Sketch

    Connect your ESP32 board and upload the sketch to see your custom Arduino library in action!

    Publishing & Sharing Your Custom Library

    If you want to share your library with others, you can upload it to GitHub and create a release version of your library.

    To get your custom library into the Arduino Library Manager as well, you need to create a new issue on the official Arduino GitHub repo, where you kindly ask the Arduino team to include your library in the Library Manager.
    Make sure you include a link to your library’s repo in the issue!

    Wrapping Up

    Creating your own Arduino library for ESP32 is easier than it looks. With just a few lines of code, you can organize your code into reusable modules, keep your sketches clean, and even share your work with others!

    This was a simple blinking LED example, but you can go much further. For example, you can wrap your ESP-NOW logic to handle sensor data in your IoT projects.


    Feel free to share your library ideas in the comments below.

    Thanks for reading! Happy coding!

    Links marked with an asterisk (*) are affiliate links which means we may receive a commission for purchases made through these links at no extra cost to you. Read more on our Affiliate Disclosure Page.

    Share this article

    Leave a Reply

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