Welcome back to our programming tutorial using the Arduino IDE. Today we will explain how to use libraries.
You can take a look at the previous chapters of the course here:
- Arduino IDE: functions and RGB LEDs at work #9.1
- Arduino IDE: knowing and using functions #9
- Arduino IDE: composing songs with an array #8.1
- Arduino IDE: what is an array or a vector #8
- Arduino IDE: RGB LED, for, while, do while loops #7
- Arduino IDE: for loops against while / do while #6
- Arduino IDE: while and do while loops #5
- Arduino IDE: turn on LEDs using a button (if) #4.1
- Arduino IDE: the conditional construction IF #4
- Arduino IDE: arithmetic and logical operators #3
- Arduino IDE: variables, constants and macros #2
- How can I use Arduino IDE to write a program? #1
- Arduino IDE: what it is and how it works #0
In computer jargon, a library is a collection of various instructions and functions. Libraries can be used within the codes and sketches created by programmers.
The advantage of using libraries is to avoid having to rewrite the same instructions, algorithms or functions (with attached definitions, declarations, etc.) each time, in order to facilitate development and maintenance options.
Many programming languages include standardized libraries. It is a set of functions and / or data structures that allow solving the most common programming problems.
A library is made up of a pre-compiled code and a header file (with the extension .h) which represents its interface for users. A library collects a set of instructions, functions and procedures related to a solution related to that particular problem. The programmer can create new libraries.
The syntax for using a library is as follows:
1 |
#include <library_name.h> |
Furthermore, the use of a library makes the code clearer and leaner, since instead of tens or hundreds of lines of code, the source has references to the functions of the library, accessed through a public interface. Libraries often contain very complex codes, and it would be difficult and inefficient to write that particular piece of code each time.
As soon as the library is defined, the modification is immediately operative for all the programs and sketches that call it. For this reason, maintenance work is also facilitated.
The Arduino IDE has an automatic library management system: just enter a keyword to indicate what you need to work on, and the IDE returns all the libraries present in the repository and capable of managing that given situation. As an example, simply follow what is shown in the figure, and after choosing “Manage Libraries” (or “Library Management”) enter the keyword “DHT11”: all the libraries capable of working with such sensor will be offered, and the user will choose (and possibly install automatically) the most appropriate one.
In the next article we will talk about the most famous libraries used in Arduino.
Follow us to keep yourself updated with the news!