How to install, use and also create an Arduino library?

Introduction

The library is a list of functions that simplify the using of a sensor or a component.
When an Arduino program begins by #include, then it is called a library.

What a library is used for?

A library is not essential in your program. Nevertheless, the library will help you to manipulate your component or to reduce your program.  It’s simplest to use the library than re-write the functionalities in your program.

When install a library?

During the compilation of the Arduino program, the interface verify that the library is available.If the library is not included in the program, you can see the following message : “Librairie.h: No such file or directory”.

If you see other error message, you can resolve it thanks to our course about the problem of upload or compiling.

How to install an Arduino Library

Method 1

You click on “manage libraries”:

 

In the manager of Library, you can enter the library you want and download it:

Method 2 : Import a library .zip

 

You can import directly a library from your computer by clicking on the file manager:

Where to find the .zip libraries?

There are many ways to find a library for a component. First when you find a program on internet, it is possible that the library is given with it, like in our courses for example.

Nevertheless, if you want to do your own program, there is a website with all the libraries.

Some useful libraries

Here is the list of the most useful libraries to write your program :

Component
LibraryCourse
IR RemoteIRremote.zipCourse
Temperature sensor DHT11DHT.zipCourse
Clock module DS3131DS3231.zipCourse
Distance sensor HC-SR04HC-SR04.zipCourse
KeypadKeypad.zipCourse
LCDLiquidCrystal.zipCourse
RfidRfid.zipCourse
Servo motorServo.zipCourse
Stepper motorStepper.zipCourse

If you don’t find the library for your program, you can create your own library !

Create an Arduino library

To create your own Arduino library, you have to program it in C++ language. You have to use a different editor than Arduino IDE. We advise you to install codeBlock software to program the library. Before programming the Arduino library, you have to know that it is composed of a files .h and .ccp Now we will take the program of Bluetooth IR Remote as an example:
To reduce this code thanks to a library, we need to regroup the program in functions.
Then we will se in codeBlocks how to write our library to simplify the code. We need to create two files : .h and ccp :
  • File .h : It is used to defined the functions that we will put into the library
  • File .ccp : We will write the functions here.
In our case, this is the .cpp file :
#include "Arduino Factory.h"

Arduino Factory::program() {
if (Serial.available() > 0){
c = Serial.read();
switch (c) {
//--------------button 0 pin RX-------------
case 'Q':
digitalWrite(button_0, HIGH);
break;
case 'R':
digitalWrite(button_0,LOW);
default:
break;
}
}
}

We will define the functions that will be in the file .h :

#ifndef ARDUINOFACTORY_H_INCLUDED
#define ARDUINOFACTORY_H_INCLUDED
class Arduino Factory
{
public:
void program();
};

#endif // ARDUINOFACTORY_H_INCLUDED

Once both files are done, you can zip it into a folder. This folder will be your new library. You just have to import it in Arduino IDE and call it in your program:

#include <Arduinofactory.h> // The library we have just created
#define button_0 0
char c=0;
uint16_t NA;

void setup() {
pinMode(button_0, OUTPUT);
Serial.begin(9600);
analogReference(INTERNAL);
}

void loop() {
Arduinofactory.program(); // The function that we have created in .ccp file
}