How to Add Libraries to Arduino/libraries

From Wiki
Jump to: navigation, search


When you run the code, you may often encounter such problem as below.

G:\Kit Code\Sensor Kit V2.0 for Arduino\Lesson 1 Display by I2C LCD1602\code\I2C_LCD1602\I2C_LCD1602.ino:3:31:
 fatal error: LiquidCrystal_I2C.h: No such file or directory

 #include <LiquidCrystal_I2C.h>
compilation terminated.

Using library Wire at version 1.0 in folder: C:\Program Files\Arduino\hardware\arduino\avr\libraries\Wire

exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.

It may be the LiquidCrystal_I2C.h, but it also may be other .h files. “LiquidCrystal_I2C.h: No such file or directory” means this LiquidCrystal_I2C library can't be found when the Arduino IDE is running. Then, you need to add this library yourself.

What is Library?

Library, gathering some Function definitions and Header files, usually contains two files: .h (header file, including function statement, Macro definition, structure definition, etc.) and .cpp (execution file, with function implementation, variable definition, etc.). When you are in need of using a function in Library, you just need to add header file (#include <LiquidCrystal_I2C.h>), and then call that function. This can make your code more concise.

If you don't want to use Library, you can write that function's definition in the code directly. However, such code will be too long and inconvenient to read.
When you want to know some function's functionality, you can search it in all code by Ctrl+F. If only an empty function is found, with no main body, it means that this function was called from a library. Then you can search it in library.

Add libraries

We have learned library's functions above. Now let's move on to how to add a library. There are two methods to add the library.

Method 1

Directly import library in Arduino IDE (take LiquidCrystal_I2C as an example below). Advantage for this method is easy to understand and operate, but its disadvantage is that only one library can be imported at a time. So it is not so convenient when multiple libraries need to be imported. Step 1: Open Arduino IDE, click Sketch -> Include Library -> Add .ZIP Library
Step 2: Find Sensor Kit V2.0 for Arduino\Library\LiquidCrystal_I2C, click Open.
Step 3: When you see the highlighted message below, it means you have added the library successfully. Please use the same method to add other libraries then.

Method 2

Directly copy the library to libraries/Arduino path. This method can enable you to copy all libraries and add them at a time, but the drawback is that it is difficult to find libraries/Arduino. Step 1: Click File -> Preferences
Step 2: The highlighted part below is the path where the libraries file lies.
Step 3: Copy all libraries in the path of Sensor Kit V2.0 for Arduino\Library\.

Step 4: Go to the path you just saw in Arduino IDE. You will see there is a Libraries file. Please click to open it.
Step 5 Paste them then, and they will be in this file. Close the IDE you just opened, and re-open it. It will be able to be compiled successfully.