How to Add Libraries to Arduino/libraries

From Wiki
Revision as of 04:16, 8 December 2016 by Root (Talk | contribs) (Created page with "Add libraries Introduction When you run code, you may often encounter such problem as below (see orange part). G:\Kit Code\Sensor Kit V2.0 for Arduino\Lesson 1 Display by I2C...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Add libraries Introduction When you run code, you may often encounter such problem as below (see orange part). 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 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, struct 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 coder 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 some empty Function is found, but no main body of Function, 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 learn how to add 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 convenient when much libraries need to be imported. Step1: Open Arduino IDE, click Sketch -> Include Library -> Add .ZIP Library Step2:Find Sensor Kit V2.0 for Arduino\Library\LiquidCrystal_I2C, Click Open.

Step3: 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.


Method2 Directly copy the library to libraries/Arduino path. This method can copy all libraries and add them at a time, but the drawback is that it is difficult to find libraries/Arduino. Step1: Click File -> Preferences

Step2: The highlighted part below is the path of libraries file in. Step3: Copy all Libraries in the path of Sensor Kit V2.0 for Arduino\Library\.

Step4: Go to the path you just saw in Arduino IDE. You will see there is a Libraries file, please click it.

Step5: Paste it then, and it will be in this file. Close the IDE you just opened, and re-open it. It will be able to be compiled successfully.