Section outline

  • Libraries – Using Powerful Pre-Built Code

    As projects grow more complex, we often need to do things like read temperature from a sensor, display text on an LCD, or connect via Bluetooth. Instead of writing everything from scratch, we can use libraries.

    • 📚 What is a Library?

      A library in Arduino is a set of pre-written code that adds new capabilities to your sketch. Libraries are written by other developers to make your life easier.

      Examples of Popular Arduino Libraries:
      • LiquidCrystal – for controlling LCD displays
      • Servo – for controlling servo motors
      • DHT – for temperature and humidity sensors
    • 🔧 How to Use a Library?

      1. Install the library: In Arduino IDE, go to Sketch > Include Library > Manage Libraries
      2. Search and install: Type the name (e.g., “DHT”) and install it
      3. Include in your sketch: Use #include at the top of your code
      Example: Using the Servo Library
      #include <Servo.h>
      
      Servo myServo;
      
      void setup() {
        myServo.attach(9);  // Connect servo to pin 9
      }
      
      void loop() {
        myServo.write(0);    // Move to 0 degrees
        delay(1000);
        myServo.write(90);   // Move to 90 degrees
        delay(1000);
      }
    • 🎯 Why Use Libraries?

      • Save time by avoiding complex coding from scratch
      • Work with popular sensors and modules easily
      • Code is cleaner, shorter, and easier to maintain

      🛠 Tips When Using Libraries

      • Always check the example sketches that come with libraries
      • Not all libraries support every board — check documentation
      • Keep libraries updated via Library Manager

      Libraries unlock powerful capabilities without needing deep code knowledge. In the next section, we'll see how to combine inputs from multiple sensors — a skill called Sensor Fusion.