Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 37 ยท Libraries and Projects

Installing and Managing Libraries

Find, install and manage Arduino libraries: the Library Manager, ZIP and Git installs, library folders, versions and dependencies, include errors, and choosing good ones.

You do not have to write the code to talk to a display, a sensor or a radio yourself. Someone has usually already packaged it as a library: a folder of C++ files that gives you friendly functions like dht.readTemperature() or lcd.print("Hello"). Libraries are the biggest reason Arduino is productive. They are also a common source of confusing errors: wrong versions, missing dependencies, two libraries with the same name.

This site cannot run the Arduino IDE or a compiler for boards. Menus and commands below describe the real tools; the C programs are labelled stand-ins that model version comparison and dependency resolution.

What a library contains

A library is a folder with a predictable layout:

Text
MyLibrary/
  library.properties      name, version, author, dependencies
  src/
    MyLibrary.h           declarations you #include
    MyLibrary.cpp         implementation
  examples/
    Basic/Basic.ino       sketches shown under File > Examples
  keywords.txt            syntax highlighting (optional)

When your sketch says #include <Servo.h>, the IDE finds the library with that header, compiles its .cpp files with your sketch and links them. Only the libraries you include are compiled in, so installing many does not bloat your program.

Installing from the Library Manager

The easiest way: Sketch, Include Library, Manage Libraries (or the books icon in the sidebar). Search by name, pick a version, click Install. The manager downloads the library into your sketchbook's libraries folder (for example Documents/Arduino/libraries). If a library needs others, the IDE offers to install the dependencies too: say yes to "Install all".

The same works from the command line with arduino-cli:

Text
arduino-cli lib search DHT
arduino-cli lib install "DHT sensor library"
arduino-cli lib list
arduino-cli lib upgrade

Other ways to install

  • ZIP file: Sketch, Include Library, Add .ZIP Library. Useful for libraries not in the manager (from GitHub, or a manufacturer's download).
  • Git: clone the repository into your libraries folder. You can then pull updates.
  • Manual copy: put the folder in libraries, and restart the IDE.
  • Bundled libraries: Wire, SPI, Servo, Stepper, EEPROM, SoftwareSerial and a few others are built into the board core and need no installation.

The folder name must match the library (letters, numbers, underscores); no spaces. A common mistake is unzipping into libraries/MyLib-master/MyLib-master/, one level too deep, so nothing is found.

Using a library

C++
#include <Servo.h>          // angle brackets: look in libraries and the core
#include "config.h"         // quotes: look in the sketch folder first

Servo flag;                 // create an object of the library's class

void setup() {
  flag.attach(9);           // call its methods
}

void loop() { }

After installing, look under File, Examples for the library's name. Running the bundled examples first is the quickest way to learn the API and to check that your wiring works before you add your own logic.

Versions and dependencies

Libraries follow semantic versioning: major.minor.patch. A change in the major number may break your code, a minor adds features compatibly, and a patch fixes bugs. In library.properties, version=1.4.2 is compared number by number, not as text: 1.10.0 is newer than 1.9.0, even though "1.10.0" sorts before "1.9.0" as a string.

C Stand-in: comparing library versions (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Dependencies are listed in library.properties (depends=Adafruit BusIO, Adafruit GFX Library). A library that needs another one fails to compile if it is missing, usually with fatal error: Adafruit_GFX.h: No such file or directory. The fix is always the same: install the missing library. The next stand-in resolves a dependency list the way the IDE does: it collects what is missing.

C Stand-in: finding missing dependencies (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Choosing a good library

Not every library is well made. Before adopting one, check:

  • Maintenance: recent releases and answered issues.
  • Board support: the architectures line should include yours (avr, esp32, *...).
  • Memory use: big libraries (graphics, networking) can fill the 2 KB of RAM of an Uno.
  • Blocking behavior: does it use delay() inside, freezing your sketch?
  • License: open source licenses (MIT, LGPL) are usually fine for hobby use; check for products you sell.
  • Alternatives: a ten-line function sometimes beats a 3,000-line library.

Troubleshooting

Error Likely cause
No such file or directory for a header library not installed, or installed in a wrong folder
multiple libraries were found warning two libraries contain the same header; the IDE tells which is used
code that worked now breaks an auto-updated library changed its API; pin the older version
was not declared in this scope missing #include, wrong class name, or a function that the installed version lacks
sketch too large a library is heavy; check alternatives or a bigger board

Keep a list of the library versions your project uses (the IDE shows them under Sketch, Include Library) so you can rebuild it years later. Writing a small library of your own is covered in writing your own library; using display libraries is covered in LCD and OLED displays.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where does the Library Manager put installed libraries?

    Libraries are stored as folders in the sketchbook's libraries directory and compiled with any sketch that includes them.
  2. 2. Which version is newer?

    Versions are compared number by number: minor 10 is greater than minor 9.
  3. 3. You get 'fatal error: Adafruit_GFX.h: No such file or directory' while compiling for an OLED. What now?

    The display library depends on GFX. Install the dependency through the Library Manager.
  4. 4. What is the difference between #include <Servo.h> and #include "config.h"?

    Quotes look in the sketch's own folder first, then fall back to library paths. Angle brackets go to library and core paths.

Summary

  • A library is a folder with headers, source files, examples and library.properties.
  • Install through the Library Manager, a ZIP, or Git; core libraries like Wire, SPI and Servo are bundled.
  • Versions are compared numerically; install dependencies the library lists.
  • Pick maintained, compatible, memory-friendly libraries and note which versions your project uses.

Next: LCD and OLED Displays