Sub topic 13
Adding Libraries with the Library Manager
Prerequisites
You should already have the Arduino IDE installed, a sketch open, and a board selected. You should also know how to compile and upload a basic sketch. This chapter uses the Arduino IDE 2.x interface and an Arduino-compatible board connected by USB.
A library is a collection of reusable code. It gives your sketch functions for a device or task, such as reading a sensor, controlling a display, or formatting data. A board package helps the IDE understand a board. A library helps your sketch perform a specific task. They are different parts of the Arduino software system.
Find and install a library
The Library Manager downloads libraries and places them in the Arduino sketchbook. To install one:
- Open the Library Manager by selecting Tools > Manage Libraries.
- Enter a library name or function in the search box.
- Select the library you want to use.
- Choose a version if the IDE offers a version selector.
- Click Install.
For this example, search for ArduinoJson and install the library published by Benoit Blanchon. ArduinoJson creates and reads JSON data. JSON is a text format often used when programs exchange structured data.
The Library Manager may show several libraries with similar names. Check the publisher and description before installing. A library with the wrong name can cause missing functions or incompatible examples.
After installation, the library becomes available to sketches on that computer. It is normally stored in a libraries folder inside the Arduino sketchbook.
Include and use the library
Installing a library does not automatically add it to every sketch. Your sketch must include the library's header file. A header file declares the names and types that the compiler needs.
This sketch creates a JSON document and prints it through the Serial Monitor:
#include <ArduinoJson.h>
void setup() {
Serial.begin(9600);
JsonDocument document;
document["board"] = "Arduino";
document["status"] = "ready";
serializeJson(document, Serial);
Serial.println();
}
void loop() {
}The angle brackets in #include <ArduinoJson.h> tell the compiler to search the installed library locations. The header file name is not always identical to the Library Manager name, so read the library's examples or documentation when you are unsure.
Upload the sketch, open the Serial Monitor, and set its speed to 9600 baud. You should see a line similar to this:
{"board":"Arduino","status":"ready"}The exact formatting can vary between library versions.
Use examples and manage versions
Installed libraries often provide examples under File > Examples. These examples are useful because they show the correct include statement, initialization steps, and function names for that library. Start with the smallest example that matches your hardware.
A library can have dependencies. A dependency is another library required by the first library. The Library Manager may install dependencies automatically. If compilation reports that a header file cannot be found, check whether the required dependency is installed.
You can update or remove a library from the Library Manager. Updating can add features, but it can also change function names or behavior. If an existing sketch stops compiling after an update, select an earlier version when one is available.
Common library problems
| Problem | Likely cause | What to check |
|---|---|---|
| `No such file or directory` | The library is not installed or the header name is wrong | Search Library Manager and inspect an example |
| A function is not declared | The wrong library or version is installed | Check the publisher and required version |
| `multiple libraries were found` | Duplicate library folders exist | Keep the intended copy and remove the duplicate |
| The sketch compiles but the device fails | Wiring or initialization is wrong | Follow the library's hardware example |
Exercise
You installed a library, but the compiler reports that its header file cannot be found. What should you check first?
Show answer
Confirm that the correct library is installed in Library Manager, then compare the #include name with one of the library's official examples.