Difference between revisions of "How to Add Libraries to Arduino/libraries"

From Wiki
Jump to: navigation, search
(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...")
 
Line 1: Line 1:
Add libraries
+
==Introduction==
Introduction
+
When you run the code, you may often encounter such problem as below.
When you run code, you may often encounter such problem as below (see orange part).
+
<pre>
 
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
 
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
  
Line 7: Line 7:
 
                               ^
 
                               ^
 
compilation terminated.
 
compilation terminated.
 +
</pre>
  
 
Using library Wire at version 1.0 in folder: C:\Program Files\Arduino\hardware\arduino\avr\libraries\Wire  
 
Using library Wire at version 1.0 in folder: C:\Program Files\Arduino\hardware\arduino\avr\libraries\Wire  
exit status 1
+
exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.
+
Error compiling for board Arduino/Genuino Mega or Mega 2560.
 +
<br>[[File:How-1.png]]<br>
 +
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. <br>
 +
 
 +
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.
 +
<br>
 +
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.
 +
<br>[[File:How-2.png]]<br>
 +
==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''' 
 +
<br>[[File:How-3.png]]<br>
 +
'''Step 2:''' Find Sensor Kit V2.0 for Arduino\Library\LiquidCrystal_I2C, click '''Open'''.
 +
<br>[[File:How-4.png]]<br>
 +
'''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.
 +
<br>[[File:How-5.png]]<br>
 
   
 
   
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.
+
===Method 2===
What is Library?
+
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.
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.  
+
'''Step 1:''' Click '''File''' -> '''Preferences'''
 +
<br>[[File:How-6.png]]<br>
 +
'''Step 2:''' The highlighted part below is the path where the libraries file lies.  
 +
<br>[[File:How-7.png]]<br>
 +
'''Step 3:''' Copy all libraries in the path of Sensor Kit V2.0 for Arduino\Library\.
 +
<br>[[File:How-8.png]]<br>
  
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.
+
'''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.
 
+
<br>[[File:How-9.png]]<br>
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. 
+
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.
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.
+

Revision as of 08:37, 9 December 2016

Introduction

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.


How-1.png
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.
How-2.png

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
How-3.png
Step 2: Find Sensor Kit V2.0 for Arduino\Library\LiquidCrystal_I2C, click Open.
How-4.png
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.
How-5.png

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
How-6.png
Step 2: The highlighted part below is the path where the libraries file lies.
How-7.png
Step 3: Copy all libraries in the path of Sensor Kit V2.0 for Arduino\Library\.
How-8.png

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.
How-9.png
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.