ATmega328P/Libraries/13. Install from the Library Manager
Libraries · 13 of 33

Install from the Library Manager

The Library Manager is a searchable list of more than ten thousand libraries built into the Arduino IDE. Find the name, check the author, press Install, and the library lands in your sketchbook where the IDE looks first. Here it installs Bounce2 and uses it to turn a bouncing wire into one clean touch.

Your board

The list

Arduino keeps an index of published libraries, each with its name, author, versions and a link to its source. The Library Manager in Arduino IDE 2 is a search box over that index. Open it with the books icon on the left edge of the window, with Ctrl+Shift+I (Cmd+Shift+I on a Mac), or from Sketch > Include Library > Manage Libraries.

The library this chapter installs is Bounce2, by Thomas O Fredericks. It cleans up a switch or a wire that bounces, and the jumper wire is all it needs to show it working.

Seven steps

Bounce2 from the Library Manager, step by step
ready
Library
Bounce2
Version
2.71.0
Shortcut
Ctrl Shift I
Seven steps, one of which is waiting. Press Install it to watch the Library Manager fetch Bounce2 and put it where the IDE looks first.

Type Bounce2 in the box. A few cards come up, because other libraries mention bouncing; the one you want says Bounce2 by Thomas O Fredericks. The card shows the newest version, 2.71.0 when this was written, and an Install button. The version list beside it holds the older releases, for a sketch that was written against one of them.

Some libraries need others to work. When one does, the IDE asks before installing, with Install All and Install without dependencies. Choose Install All. Bounce2 needs nothing else, so it goes straight in.

When it finishes, a notification says Successfully installed library Bounce2:2.71.0, the card says 2.71.0 installed, and a new folder libraries/Bounce2 sits in your sketchbook. The card's … menu now offers the examples, other versions and Remove.

The example first

File > Examples > Examples from Custom Libraries > Bounce2 > bounce_basic opens the author's own example, with the input on pin 2 and the lamp on LED_BUILTIN, which is 13 on the Uno and the Nano alike, so it runs on either board as it is. It is a good habit to upload a library's example before writing anything of your own. If the example works, the install is right, and anything that breaks later is in your code.

The sketch at the top of this page does the same job and counts what it is hiding. Every call into the library is one of three:

  • wire.attach(2, INPUT_PULLUP) sets the pin up, pull-up and all.
  • wire.update() reads the pin. Call it on every pass of loop(); the library only knows what it has been shown.
  • wire.fell() is true on the one pass after the pin has gone from HIGH to LOW and stayed there for the interval, 10 ms here.

Tap the wire on GND a few times and watch the two counts in the Serial Monitor. touches goes up by one per tap. raw falling edges often goes up by two or three, because the metal bounces as it lands. The library is the difference between those two numbers. Bounce, and how to ignore it does the same thing without a library, in about ten lines.

Updates

The Library Manager notices when a newer version of an installed library is published, and the card offers Update. Updating replaces the folder. Most updates are fixes; a few change how a library is called, which is when a sketch that compiled last month stops compiling today. Versions, and "multiple libraries found" covers going back to the old one.

The code

Bounce2_Toggle.ino

Uses Bounce2 to count touches of a jumper wire from D2 to GND and toggles the pin-13 lamp on each. It also counts the raw falling edges digitalRead sees, so the Serial Monitor (9600 baud) shows what the library is hiding.

// Bounce2_Toggle.ino — a touch of a wire on D2 toggles lamp 13
// Tools > Board: Arduino Uno, or Arduino Nano with
//   Processor ATmega328P (Old Bootloader if the upload says
//   "not in sync")
// Tools > Port: the CH340 port
// Library: Bounce2 by Thomas O Fredericks (Library Manager)
// Wiring: one jumper wire from 2 to GND. D2 is INPUT_PULLUP,
//   so touching the wire's free end to GND is safe.

#include <Bounce2.h>

const uint8_t WIRE_PIN = 2;

Bounce wire = Bounce();
unsigned int touches = 0;   // what Bounce2 reports
unsigned int rawEdges = 0;  // what digitalRead sees
bool lastRaw = HIGH;

void setup() {
  Serial.begin(9600);
  wire.attach(WIRE_PIN, INPUT_PULLUP);
  wire.interval(10);             // ms the pin must hold still
  pinMode(LED_BUILTIN, OUTPUT);  // the lamp on 13, both boards
}

void loop() {
  bool raw = digitalRead(WIRE_PIN);
  if (lastRaw == HIGH && raw == LOW) rawEdges++;
  lastRaw = raw;

  wire.update();
  if (wire.fell()) {
    touches++;
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
    Serial.print(F("touches: "));
    Serial.print(touches);
    Serial.print(F("   raw falling edges: "));
    Serial.println(rawEdges);
  }
}

Put one end of the wire in the 2 hole and tap the other into a GND hole: on the UNO the one next to 13 on the top strip, on the Nano the one right beside D2. D2 is INPUT_PULLUP, so the wire is safe. touches goes up by one each tap; raw falling edges often goes up by more.

When it does not work

The search finds nothing, or the list never loads

The Library Manager downloads its list from Arduino's servers, so it needs an internet connection the first time and whenever the list is refreshed. Check the connection, then close and reopen the panel. A proxy or a firewall that blocks downloads.arduino.cc has the same effect.

Bounce2.h: No such file or directory after installing

Check the card says 2.71.0 installed, or whatever version you chose. If it does, the sketch is probably including a different spelling: the header is Bounce2.h, with a capital B and the 2. If the card offers Install again, the install did not finish; try once more and read the Output panel for the reason.

Installed a library with a similar name instead

Several libraries mention bounce or debounce. Open the card's More info link or read the author: the one this book uses is Bounce2 by Thomas O Fredericks. Remove the wrong one from its card's menu so the two do not compete for the same header.

The IDE asked about dependencies and I chose without

Some libraries need others to compile. Install without dependencies leaves those out, and the sketch then fails on an #include inside the library you never wrote. Install the library again and choose Install All, or install the missing ones by name.

Where this goes next

For a library the Library Manager does not list, and the folder shape that works.

Install a ZIP, or by hand →

Edit this page — content/books/atmega328p/the-library-manager.mdx

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum →