Lesson P10 – Introduction to Libraries in Arduino
🎯 Learning Objectives
After completing this lesson, students will be able to:
✅ Understand what libraries are
✅ Understand why libraries are used
✅ Install libraries in Arduino IDE
✅ Include libraries in programs
✅ Use library examples
✅ Understand the benefits of libraries
✅ Work with sensors and modules more efficiently
1. Introduction
Imagine you want to connect:
- DHT11 Sensor
- Servo Motor
- LCD Display
- RFID Module
- Ultrasonic Sensor
Without libraries, you would have to write hundreds or even thousands of lines of code to communicate with these devices.
This would be difficult and time-consuming.
To solve this problem, Arduino provides:
Libraries
Libraries make programming easier by providing ready-made code for common devices and functions.
2. What is a Library?
A library is a collection of pre-written code that helps Arduino perform specific tasks.
Think of a library as a toolbox.
Instead of making every tool yourself, you simply use the tools already available.
Similarly:
Instead of writing complex code from scratch, you use a library.
Real-Life Example
Imagine building a house.
Would you manufacture:
- Bricks
- Cement
- Doors
- Windows
yourself?
No.
You use ready-made materials.
Libraries work the same way.
They save time and effort.
3. Why Do We Need Libraries?
Without libraries:
❌ Programs become very long
❌ Development becomes slow
❌ More chances of errors
❌ Difficult for beginners
With libraries:
✅ Faster development
✅ Easier programming
✅ Less code
✅ Better reliability
4. Examples of Popular Arduino Libraries
| Library | Purpose |
|---|---|
| Servo.h | Servo Motor Control |
| LiquidCrystal_I2C.h | I2C LCD Display |
| DHT.h | Temperature & Humidity Sensor |
| WiFi.h | WiFi Communication |
| BlynkSimpleEsp32.h | Blynk IoT Platform |
| SPI.h | SPI Communication |
| Wire.h | I2C Communication |
| EEPROM.h | Data Storage |
5. How Libraries Work
Suppose you want to rotate a servo motor.
Without a library:
You would need to generate accurate PWM signals manually.
This requires advanced programming.
With the Servo Library:
Servo myServo;
myServo.write(90);
The servo rotates to 90°.
Much easier.
6. Including a Library
Before using a library, it must be included.
Syntax:
#include <LibraryName.h>
Example:
#include <Servo.h>
Now Arduino can access all Servo Library functions.
What Does #include Mean?
The preprocessor command:
#include
tells Arduino:
“Add the contents of this library before compiling.”
7. Built-in Libraries
Arduino IDE already contains several libraries.
Examples:
Servo Library
#include <Servo.h>
EEPROM Library
#include <EEPROM.h>
SPI Library
#include <SPI.h>
Wire Library
#include <Wire.h>
No installation required.
8. External Libraries
Some sensors require additional libraries.
Examples:
- DHT11
- Blynk
- MFRC522 RFID
- Adafruit Sensors
These libraries must be installed manually.
9. Installing Libraries
Arduino IDE provides:
Library Manager
for easy installation.
Steps
Step 1
Open Arduino IDE
Step 2
Click:
Sketch
↓
Include Library
↓
Manage Libraries
Step 3
Search library name.
Example:
DHT Sensor Library
Step 4
Click Install.
Step 5
Wait for installation.
Library is now ready to use.
10. Installing DHT11 Library Example
Search:
DHT sensor library
Install:
DHT Sensor Library by Adafruit
After installation:
#include <DHT.h>
can be used.
11. Using Library Examples
Most libraries provide example programs.
This is one of the biggest advantages of Arduino.
Accessing Examples
File
↓
Examples
↓
Library Name
↓
Example Program
Example
File
↓
Examples
↓
Servo
↓
Sweep
This example automatically controls a servo motor.
Why Use Examples?
Examples help:
✅ Learn faster
✅ Understand library usage
✅ Save development time
✅ Reduce errors
12. Servo Library Example
#include <Servo.h>
Servo myServo;
void setup()
{
myServo.attach(9);
}
void loop()
{
myServo.write(0);
delay(1000);
myServo.write(90);
delay(1000);
}
Result:
Servo rotates between 0° and 90°.
13. DHT11 Library Example
#include <DHT.h>
The library provides ready-made functions such as:
dht.readTemperature();
and
dht.readHumidity();
Without the library, sensor communication would be much harder.
14. LCD Library Example
#include <LiquidCrystal_I2C.h>
Library functions:
lcd.print("Hello");
lcd.clear();
lcd.setCursor(0,0);
These simplify LCD programming.
15. Understanding Documentation
Every library includes documentation.
Documentation explains:
- Installation
- Wiring
- Functions
- Examples
- Limitations
Good programmers always read documentation.
Example Information Found in Documentation
Required Pins
Supported Boards
Available Functions
Sample Code
16. Commonly Used Beginner Libraries
For this course, students will frequently use:
Servo.h
Servo Motor
LiquidCrystal_I2C.h
LCD Display
DHT.h
Temperature Sensor
Wire.h
I2C Communication
EEPROM.h
Data Storage
17. Library Dependencies
Some libraries require other libraries.
Example:
A sensor library may require:
Adafruit Unified Sensor
library.
When installing through Library Manager, dependencies are usually installed automatically.
18. Common Beginner Mistakes
Mistake 1
Forgetting to include the library.
Wrong:
Servo myServo;
without:
#include <Servo.h>
Mistake 2
Incorrect library name.
Wrong:
#include <servo.h>
Correct:
#include <Servo.h>
Mistake 3
Library not installed.
Result:
Compilation error.
Mistake 4
Ignoring example programs.
Examples often provide the fastest solution.
19. Best Practices
✅ Install libraries from trusted sources
✅ Read documentation
✅ Test examples first
✅ Keep libraries updated
✅ Avoid installing duplicate libraries
20. Real-World Applications
Libraries are used in:
IoT Systems
WiFi and Blynk libraries
Robotics
Servo and motor control libraries
Smart Homes
Sensor libraries
RFID Attendance Systems
RFID libraries
Weather Stations
DHT libraries
Industrial Monitoring
Communication libraries
Libraries make modern embedded systems development much faster and more reliable.
📊 Summary
In this lesson, we learned:
✅ What libraries are
✅ Why libraries are important
✅ Built-in libraries
✅ External libraries
✅ Library installation
✅ Library Manager
✅ Example programs
✅ Documentation
Libraries allow programmers to use complex hardware and software features through simple, ready-made functions.
📖 Key Terms
Library
A collection of pre-written code.
Library Manager
Tool used to install libraries.
Documentation
Guide explaining library usage.
Dependency
A library required by another library.
Include
Adding a library to a program using #include.
🎯 Quiz
1. What is a library?
A. Sensor
B. Collection of pre-written code ✅
C. Motor
D. Variable
2. Which symbol is used to include a library?
A. import
B. include
C. #include ✅
D. using
3. Where can libraries be installed?
A. Serial Monitor
B. Library Manager ✅
C. Task Manager
D. Device Manager
4. Which library is commonly used for servo motors?
A. DHT.h
B. Servo.h ✅
C. Wire.h
D. EEPROM.h
5. Why are libraries useful?
A. Reduce coding effort ✅
B. Increase wiring
C. Replace Arduino
D. Generate power
🏠 Assignment
Task 1
Open Arduino IDE and explore the Library Manager.
Task 2
Install the DHT Sensor Library.
Task 3
Find three example programs from the Servo Library.
Task 4
Create a list of five Arduino libraries and their purposes.
Task 5
Research the difference between built-in libraries and external libraries.