Sub topic 13
Adding libraries with the Library Manager
This chapter assumes you have installed the Arduino IDE, can select a board and port, and have uploaded Blink at least once. You should also know what setup() and loop() do. Here you will learn what a library is, how the Library Manager installs one, how to use it in a sketch, and which errors tend to appear.
What a library is and why you need one
A library is a bundle of ready-made code that adds a capability to your sketches. The Arduino core already gives you pinMode(), digitalWrite(), delay() and Serial. It does not know how to talk to an LCD, a temperature sensor, a servo, or an LED strip. A library supplies that code so you do not have to write the low-level protocol yourself.
A library normally contains:
- Header files (
.h) that declare the classes and functions you call. - Source files (
.cpp) that implement them. - Example sketches that show typical use.
- A
library.propertiesfile that records the name, version, author, supported architectures, and dependencies.
Some libraries ship with the IDE or with a board package. Wire (I2C), SPI, and EEPROM are examples. Everything else you install yourself.
The working model: where libraries live
When you compile, the IDE builds a list of folders to search for headers. Knowing the order explains most library problems.
- Your sketch folder, for headers you placed next to the
.inofile. - Libraries bundled with the selected board's core.
- Libraries in your sketchbook
librariesfolder. This is where the Library Manager installs. - Libraries bundled with the IDE.
The IDE scans your sketch for #include lines. It matches each header name to a library, then compiles only the libraries you actually include. That is why installing a library does not make your sketch larger until you include it.
The sketchbook location is set in File > Preferences, under "Sketchbook location". On many systems the default is Documents/Arduino, so libraries land in Documents/Arduino/libraries.
Installing a library with the Library Manager
The Library Manager is a built-in catalog. It downloads from the Arduino library index, so you need an internet connection.
- Open the Library Manager. Use Sketch > Include Library > Manage Libraries, or click the library icon in the left sidebar. The keyboard shortcut is Ctrl+Shift+I (Cmd+Shift+I on macOS).
- Type a name or keyword in the search box, for example
Servo. - Find the correct entry. Check the author and the description, because several libraries can have similar names.
- Choose a version from the drop-down if you need a specific one. Otherwise the latest version is selected.
- Click Install.
- If the library depends on others, the IDE asks whether to install them too. Choose "Install all" unless you have a reason not to.
When the install finishes, the entry shows "INSTALLED". The same panel lets you update or remove the library later.
The filter drop-downs narrow the list. "Type" can show only Installed, Updatable, or Recommended entries. "Topic" narrows by category such as Communication or Sensors.
Using a library in a sketch
Installing a library does not change your sketch. You must include its header and then use its classes.
You can insert the include line by choosing Sketch > Include Library and picking the library name. Typing it yourself works equally well.
The built-in Servo library is a good test because it needs only a board and, optionally, a servo. This sketch sweeps a servo on pin 9 back and forth.
#include <Servo.h>
Servo myServo;
void setup() {
myServo.attach(9);
}
void loop() {
for (int angle = 0; angle <= 180; angle += 1) {
myServo.write(angle);
delay(15);
}
for (int angle = 180; angle >= 0; angle -= 1) {
myServo.write(angle);
delay(15);
}
}The parts follow a common pattern across most libraries:
#include <Servo.h>makes the library's declarations visible.Servo myServo;creates an object. Many libraries are used through objects.myServo.attach(9)is an initialization call, normally made insetup().myServo.write(angle)is a regular call used inloop().
Servo ships with the standard AVR core and appears in the manager as a built-in. If you have an Uno-class board, it is usually already available. Libraries you fetch from the manager follow the same include-then-object pattern.
Angle brackets or quotes
You will see both #include <Servo.h> and #include "MyHeader.h". Angle brackets tell the compiler to search the library and system paths. Double quotes tell it to look in the sketch folder first, then fall back to the library paths. Use quotes for headers you wrote yourself and angle brackets for installed libraries.
Learning from library examples
Each well-made library includes example sketches. After installing, open File > Examples and scroll to the library's name. Built-in examples appear first, and installed libraries appear under "Examples from Custom Libraries" or by name.
Opening an example creates a read-only copy in a temporary location if the library folder is not writable. Use File > Save As to keep your edits in your sketchbook. Reading the example is the fastest way to learn which begin() or attach() call the library expects.
Versions, updates, and dependencies
The manager shows an "Updatable" label when a newer version exists. Updating is usually safe, but a major version change can rename functions. If a sketch compiled yesterday and fails today after an update, the library version is a prime suspect.
Practical rules:
- Note the version in a comment at the top of any sketch you intend to keep working.
- When you share a project, list the library names and versions you used.
- To go back, open the manager, choose the older version in the drop-down, and install it again. The old version replaces the new one.
- Keep dependencies installed. If a library requires
Adafruit BusIO, a missing copy produces a "No such file or directory" error for a header you never typed.
Architecture compatibility
Each library declares which processor architectures it supports in library.properties, for example architectures=avr or architectures=*. A library written for AVR boards may use registers that do not exist on a board with a different chip. The IDE may still list the library, and the compile then fails or the IDE prints a warning that the library may be incompatible with your board.
The manager lists libraries for all boards. It does not filter by your current board selection. Check the description and the supported boards before you install.
Installing from other sources
The manager covers libraries in the official index. Two other methods exist for everything else.
- Add a
.ZIPlibrary: choose Sketch > Include Library > Add .ZIP Library and select the downloaded archive. The ZIP must contain a folder with the library files at its top level, not extra nested folders. - Manual install: unpack the library folder into the sketchbook
librariesfolder yourself, then restart the IDE.
Libraries from outside the index have not been reviewed by the Arduino maintainers. Read the source or the project page before you add code to a board that controls anything important.
Common errors and how to read them
| Message | Likely cause | Fix |
|---|---|---|
| `fatal error: Foo.h: No such file or directory` | The library is not installed, or the header name differs from the library name | Search for the library, install it, and check the exact header in its examples |
| Same error for a header you did not write | A dependency of an installed library is missing | Reinstall the library and accept "Install all" |
| `'Servo' does not name a type` | The `#include` line is missing or misspelled | Add `#include <Servo.h>` at the top |
| `Multiple libraries were found for "X.h"` | Two libraries provide the same header | Remove one, or read which one the IDE says it is using |
| `WARNING: library X claims to run on avr architecture(s) and may be incompatible with your current board` | The library is not declared for your chip | Pick a library that supports your board, or test carefully |
| Compile worked before, fails now | An update changed the interface | Install the previous version |
Header names are case sensitive on Linux, so servo.h fails there even if it works on Windows.
The "Multiple libraries were found" message appears in the output panel as a note, not necessarily an error. The line "Used:" shows the library the compiler chose, and "Not used:" shows the others. If your sketch behaves oddly, make sure the used one is the one you intended.
Removing a library
The Library Manager in newer IDE versions offers a Remove button on installed entries. In every version you can also delete the library's folder from the sketchbook libraries folder while the IDE is closed. Libraries that came from the board package are removed by removing the board package.
Check your understanding
Exercise
You install a library from the Library Manager and add #include <Adafruit_Sensor.h> to your sketch. The compile fails with "fatal error: AdafruitBusIORegister.h: No such file or directory", a header you never wrote. What is the most likely cause, and how do you fix it?
Show answer
A library the sketch uses depends on another library (Adafruit BusIO) that was not installed, probably because "Install all" was not chosen when the dependency prompt appeared. Open the Library Manager, search for Adafruit BusIO, and install it. Then compile again.
Exercise
A sketch that compiled last week now fails after you clicked Update on its library. Name two steps you can take to get the sketch working again.
Show answer
Open the Library Manager, select the previous version from the version drop-down for that library, and install it, which replaces the newer version. Alternatively, read the library's release notes and change the sketch to match the new interface. Recording the library version in a comment avoids guessing next time.