What are Arduino Libraries?
- Libraries are pre-written pieces of code that extend the functionality of your Arduino sketches.
- They help you work with sensors, displays, motors, communication modules, and other hardware easily—without writing everything from scratch.
- Libraries include functions, classes, and examples that simplify coding.
✅ Example: To control an LCD display, instead of writing low-level code, you can use theLiquidCrystal library.
🔹 2. Types of Arduino Libraries
Type | Description | Examples |
Built-in Libraries | Pre-installed with Arduino IDE | Servo.h,LiquidCrystal.h |
Contributed Libraries | Installed by users from Library Manager | Adafruit_Sensor.h,DHT.h |
Custom Libraries | User-created libraries | Self-made or downloaded from GitHub |
🔹 3. How to Use a Library
✅ Step 1: Include the Library in Your Sketch
Use#include <LibraryName.h> at the beginning of your code.
#include <Servo.h> // Include the Servo motor library
✅ Step 2: Use the Functions Provided
Example using the Servo library:
#include <Servo.h>
Servo myServo;
void setup() {
myServo.attach(9); // Connect servo to pin 9
}
void loop() {
myServo.write(90); // Rotate to 90 degrees
delay(1000);
myServo.write(0); // Rotate to 0 degrees
delay(1000);
}
🔹 4. How to Install a Library
There are 3 common methods to install libraries in Arduino IDE:
✅ Method 1: Using Library Manager (Recommended)
🔧 Steps:
- Open Arduino IDE
- Go to Sketch > Include Library > Manage Libraries
- The Library Manager window will open
- Search for the desired library (e.g., DHT sensor)
- Click Install
📌 Example: Search forDHT sensor library and install it to use DHT11/DHT22 sensors.
✅ Method 2: Installing ZIP Library
If you have a.zip file of the library (from GitHub or other sources):
🔧 Steps:
- Download the.zip file of the library
- Open Arduino IDE
- Go to Sketch > Include Library > Add .ZIP Library
- Select the downloaded.zip file
- Library will be added automatically
✅ Method 3: Manual Installation
If you want to install a library manually:
🔧 Steps:
- Download and extract the library folder (should contain.h and.cpp files)
- Move it to:
Documents > Arduino > libraries
- Restart Arduino IDE
✅ After restart, go to Sketch > Include Library, and you should see your library listed.
🔹 5. How to Upload a Sketch Using a Library
Once a library is included:
- Just write your code using the library functions
- Connect your Arduino board
- Click Verify✅ to check for errors
- Click Upload🔼 to send the code to the board
🔹 6. Commonly Used Libraries in Arduino
Library Name | Used For |
Servo.h | Controlling servo motors |
LiquidCrystal.h | LCD display |
Wire.h | I2C communication |
SPI.h | SPI communication |
DHT.h | DHT11/DHT22 humidity sensors |
Adafruit_GFX.h | Graphics for displays |
Ultrasonic.h | Ultrasonic sensors |
🔹 7. Troubleshooting Library Issues
Problem | Solution |
Library not found | Check if installed in the correct folder |
Conflicting libraries | Remove duplicate or older versions |
Error on include line | Check spelling, or reinstall library |
Functions not working | Read examples and check pin connections |
Real-life Example: DHT11 Sensor Code Using Library
#include "DHT.h" // Include the DHT sensor library
#define DHTPIN 2 // Sensor data pin connected to Arduino pin 2
#define DHTTYPE DHT11 // DHT11 sensor type
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin(); // Start the sensor
}
void loop() {
float h = dht.readHumidity(); // Read humidity
float t = dht.readTemperature(); // Read temperature
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("% | Temperature: ");
Serial.print(t);
Serial.println("°C");
delay(2000);
}
Summary
Step | Action |
Include Library | #include <LibraryName.h> |
Install via Library Manager | Sketch > Include Library > Manage Libs |
Install ZIP File | Sketch > Include Library > Add ZIP |
Use Library Functions | As per documentation/examples |