0
0
Arduinoprogramming~5 mins

Using multiple libraries together in Arduino

Choose your learning style9 modes available
Introduction

Using multiple libraries lets you add different features to your Arduino project easily. It helps you combine tools to make your project do more things.

You want to control an LED and read a sensor at the same time.
You need to connect to WiFi and also display information on a screen.
You want to use a motor driver and a temperature sensor together.
You are building a robot that needs several parts working together.
Syntax
Arduino
#include <LibraryOne.h>
#include <LibraryTwo.h>

void setup() {
  // initialize libraries
  LibraryOne.begin();
  LibraryTwo.begin();
}

void loop() {
  // use functions from both libraries
  LibraryOne.doSomething();
  LibraryTwo.doSomethingElse();
}
Always include each library at the top with #include.
Initialize each library in setup() before using them.
Examples
This example uses the Wire library for I2C communication and the LiquidCrystal_I2C library to control an LCD screen.
Arduino
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Wire.begin();
  lcd.init();
  lcd.backlight();
}

void loop() {
  lcd.print("Hello!");
}
This example uses the Servo library to control a servo motor and the DHT library to read temperature from a sensor.
Arduino
#include <Servo.h>
#include <DHT.h>

Servo myServo;
DHT dht(2, DHT11);

void setup() {
  myServo.attach(9);
  dht.begin();
}

void loop() {
  float temp = dht.readTemperature();
  if (!isnan(temp)) {
    if (temp > 25) {
      myServo.write(90);
    } else {
      myServo.write(0);
    }
  }
  delay(2000);
}
Sample Program

This program uses two libraries: Servo to control a servo motor and DHT to read temperature. It moves the servo based on the temperature reading and prints info to the Serial Monitor.

Arduino
#include <Servo.h>
#include <DHT.h>

Servo myServo;
DHT dht(2, DHT11);

void setup() {
  Serial.begin(9600);
  myServo.attach(9);
  dht.begin();
  Serial.println("Starting...");
}

void loop() {
  float temp = dht.readTemperature();
  if (isnan(temp)) {
    Serial.println("Failed to read temperature");
  } else {
    Serial.print("Temperature: ");
    Serial.print(temp);
    Serial.println(" C");
    if (temp > 25) {
      myServo.write(90); // Move servo to 90 degrees
      Serial.println("Servo moved to 90 degrees");
    } else {
      myServo.write(0); // Move servo to 0 degrees
      Serial.println("Servo moved to 0 degrees");
    }
  }
  delay(3000);
}
OutputSuccess
Important Notes

Make sure the libraries you use do not conflict with each other, like using the same pins.

Check each library's documentation for setup and usage details.

Test each library separately before combining them to find problems easily.

Summary

Use #include to add each library at the top of your sketch.

Initialize and use each library in setup() and loop() as needed.

Combining libraries lets your Arduino do many things at once.