How do you write your own Arduino library?

ampheoampheo
3 min read

Creating a custom Arduino library allows you to organize reusable code for sensors, actuators, or algorithms. Here's a step-by-step guide to building your own library from scratch.


1. Library Folder Structure

A basic Arduino library requires:

text

MyLibrary/
├── MyLibrary.h    (Header file - declarations)
├── MyLibrary.cpp  (Source file - implementations)
├── examples/      (Optional: Example sketches)
│   └── BasicDemo/
│       └── BasicDemo.ino
├── keywords.txt   (Optional: Syntax highlighting)
└── README.md      (Optional: Documentation)

2. Create the Header File (MyLibrary.h)

This file defines your library's interface.

cpp

// MyLibrary.h
#ifndef MyLibrary_h  // Prevent duplicate inclusion
#define MyLibrary_h

#include "Arduino.h"  // Required for Arduino functions

class MyLibrary {
  public:
    // Constructor (optional)
    MyLibrary(int pin);  

    // Public methods
    void begin();
    int readValue();
    void blink(int times);

  private:
    // Private variables/functions
    int _pin;  
    void _privateHelper();
};

#endif

3. Implement the Code (MyLibrary.cpp)

This file contains the actual functionality.

cpp

// MyLibrary.cpp
#include "MyLibrary.h"

// Constructor (initialize variables)
MyLibrary::MyLibrary(int pin) {
  _pin = pin;
}

// Initialize the library
void MyLibrary::begin() {
  pinMode(_pin, OUTPUT);
}

// Read a sensor value
int MyLibrary::readValue() {
  return analogRead(_pin);
}

// Blink an LED
void MyLibrary::blink(int times) {
  for (int i=0; i<times; i++) {
    digitalWrite(_pin, HIGH);
    delay(500);
    digitalWrite(_pin, LOW);
    delay(500);
  }
}

// Private function (only accessible within class)
void MyLibrary::_privateHelper() {
  // Internal logic here
}

4. Add Examples (Optional)

Create an example sketch in examples/BasicDemo/BasicDemo.ino:

cpp

#include <MyLibrary.h>

MyLibrary myLib(13);  // Use pin 13

void setup() {
  myLib.begin();
}

void loop() {
  myLib.blink(3);     // Blink 3 times
  delay(1000);
}

5. Add Syntax Highlighting (keywords.txt)

To enable Arduino IDE coloring:

text

#######################################
# Syntax coloring for MyLibrary
#######################################

MyLibrary   KEYWORD1
begin       KEYWORD2
readValue   KEYWORD2
blink       KEYWORD2

6. Install Your Library

  1. Manual Installation:

    • Zip the MyLibrary folder.

    • In Arduino IDE: Sketch > Include Library > Add .ZIP Library.

  2. Folder Installation:

    • Copy MyLibrary/ to Documents/Arduino/libraries/.
  3. Restart the Arduino IDE.


7. Using Your Library

cpp

#include <MyLibrary.h>  // Your library is now available!

MyLibrary customDevice(8);

void setup() {
  customDevice.begin();
}

void loop() {
  int val = customDevice.readValue();
  customDevice.blink(2);
}

Best Practices

  1. Keep It Modular: Split complex libraries into multiple files.

  2. Documentation: Add comments and a README.md.

  3. Error Handling: Validate inputs in public methods.

  4. Version Control: Use GitHub to share your library.


Advanced Features

  • Support for Multiple Boards: Use #ifdef for platform-specific code.

    cpp

      #ifdef ESP32
        // ESP32-specific code
      #endif
    
  • EEPROM Storage: Add methods to save/load settings.

  • Interrupts: Handle hardware interrupts within the library.


Publishing to Arduino Library Manager

  1. Follow Arduino's library specification.

  2. Submit a pull request to the Arduino Library Registry.


Now you can create professional Arduino libraries!

0
Subscribe to my newsletter

Read articles from ampheo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

ampheo
ampheo