Because Arduino boards have a quite limited amount of memory, you’ll find it worthwhile to only include code that will actually be used in what ends up on the board. One way to achieve this is by using libraries. In Arduino, and for that matter in C in general, a library is a collection of use- ful functions.

So, for example, the Arduino IDE includes a library for using an LCD display. This uses about 1.5 kB of program memory. There is no point in this library being included unless you are using an LCD display, so such libraries are “included” when needed.

You accomplish this using the #include directive at the beginning of your sketch. You can add an include statement for any libraries that the Arduino IDE has installed using the Sketch | Import Library… menu option.

The Arduino IDE Library Manager
The Arduino IDE Library Manager

The Arduino IDE comes with a large selection of “official” libraries, including:

  • EEPROM For storing data in EEPROM memory
  • Firmata The serial communications standard for Arduino to computer
  • LiquidCrystal For alphanumeric LCD displays
  • SD For reading and writing SD flash memory cards
  • Servo For controlling servo motors
  • SPI The Arduino to peripheral communication bus
  • Software Serial For serial communication using nonserial pins
  • Stepper For controlling stepper motors
  • Wire For I2C communication with peripherals Some libraries are specific to a type of Arduino board:
  • Keyboard USB keyboard emulation (Leonardo, Due, and Micro)
  • Mouse USB mouse emulation (Leonardo, Due, and Micro)
  • Audio Audio playing utilities (Due only)

Finally, there are a huge number of libraries that other Arduino users have written that can be downloaded from the Internet. Some of the more popular ones are

  • OneWire For reading data from Dallas Semiconductor’s range of digital devices using the 1-Wire bus interface
  • Xbee For wireless serial communication
  • GFX A graphics library for many different types of display from Adafruit
  • Capacitive Sensing For proximity detection
  • FFT Frequency analysis library

New libraries appear all the time and you may find them on the official Arduino site (http://arduino.cc/en/Reference/Libraries) or you may find them with an Internet search. If you want to use one of these last categories of libraries, then you need to install it by one of two methods: using the Library Manager (see Figure 1-19) or by adding the library as a downloaded ZIP file.

The Library Manager is accessed from the Arduino IDE’s “Sketch” menu by selecting “Include Library” and then “Manage Libraries.” The Library Manager allows you to search for, add, update, and remove librar- ies, but only libraries that the IDE is aware of. For lesser known libraries that you might find on, say, GitHub, you will need to add the library as a ZIP file.

To add a library from a ZIP file: from the Arduino IDE “Sketch” menu, select “Include Library” and then “Add .ZIP Library” and navigate to the ZIP file you downloaded.

Leave a Reply