Image of eBits Academy airquality illustration

Importing Libraries - Air Quality Monitoring Station Series, Episode 9

  • March 04, 2025
  • |
  • Teofil Corad

IMPORTANT – Manual installation of the TFT_eSPI library is required

Manual installation:

  1. Download and extract the attached ZIP file.
  2. Go to your Library folder and put the TFT_eSPI folder there:
    • Linux: /home/{brugernavn}/Arduino/Libraries
    • Windows: C:\Users\{brugernavn}\Documents\Arduino\Libraries
    • MacOS: /Users/{brugernavn}/Documents/Arduino/Libraries
  3. Download the ZIP file at the bottom of the lesson (if available).

List of required components:

 

 


Lesson objectives

The purpose of this lesson is to help you understand the concept of libraries in Arduino IDE, their importance in simplifying code, and how to install and manage them correctly. This will provide the foundation for working with more complex components later in the course, such as sensors and displays, by using the libraries' functions.


What are libraries?

In Arduino programming, libraries are collections of prewritten code that make it easier to implement features without starting entirely from scratch. Instead of reinventing the wheel, libraries handle complex, low-level operations so you can focus on the logic of your program.

Examples:

  • TFT displays: A library handles communication with the screen, so you do not have to write the low-level code that interacts with the display controller yourself.
  • WiFi connections: There is also no need to write code manually to establish a network connection — WiFi libraries can automate that process.

In short, libraries help you focus on your project rather than the hardware details.


Step 1: Accessing the Library Manager in Arduino IDE

The Library Manager is an important feature in Arduino IDE. It is a repository of libraries that makes it easy to search for, install, and manage libraries for your projects.

  1. Open the Library Manager:

    • In Arduino IDE, click the Sketch menu, then Include Library, and finally Manage Libraries…. This opens the Library Manager window.
  2. Searching for libraries:

    • At the top of the Library Manager window is a search bar. For example, if you want to control a TFT display, you can search for the TFT_eSPI library.
  3. Installing libraries:

    • When you find the library you need, click on it. An Install button appears. Click the button to install the library in your Arduino environment.
    • You can then use the library in your code by including it in your sketch with #include.

This process saves you from manually downloading and placing libraries in your Arduino folder and makes your workflow more efficient.


Step 2: Importing and using libraries

Once you have installed the necessary libraries via the Library Manager, you can include them in your sketches using the #include directive at the top of the code. This tells the compiler to retrieve the library and its associated functions.

Here are some examples of how to include important libraries for this project:

#include <WiFi.h>
#include <WebServer.h>
#include <Adafruit_AHTX0.h>
#include <math.h>
#include <TFT_eSPI.h>
#include <SPI.h>

By including these libraries, you can take advantage of their prebuilt functions. For example:

  • You can use tft.fillScreen() to easily clear or change the background of the screen.
  • Functions such as tft.drawString() let you print text on the screen without worrying about low-level communication.

Example of using a library

To see how libraries simplify your work, here is a quick example using the TFT_eSPI library for a TTGO T-Display. The goal is to initialize the screen and print some text.

#include <TFT_eSPI.h> // Inkluder TFT-biblioteket

TFT_eSPI tft = TFT_eSPI(); // Opret et display-objekt

void setup() {
  tft.init(); // Initialiser displayet
  tft.fillScreen(TFT_BLACK); // Ryd skærmen med sort baggrund
  tft.setTextColor(TFT_WHITE, TFT_BLACK); // Sæt tekstfarve
  tft.drawString("Welcome!", 10, 20, 4); // Skriv tekst på koordinaterne (10, 20) med skriftstørrelse 4
}

void loop() {
  // Her kan du opdatere skærmen med nye data
}

 

With just a few lines of code, the library lets you control the screen — initialize it, set colors, and print text — without needing to understand how the display controller works internally.


Step 3: Managing and updating libraries

Proper library management ensures that your project remains functional and up to date. Here are some useful tips:

  1. Updating libraries:

    • Occasionally, you may need to update libraries to ensure compatibility with new versions of Arduino IDE or to obtain new features. In the Library Manager, you can see whether updates are available for installed libraries and update them with a single click.
  2. Uninstalling libraries:

    • If you need to remove a library, you can uninstall it via the Library Manager. Find the library and select Uninstall.
  3. Adding libraries manually:

    • Sometimes you need to install a library manually if it is not available in the Library Manager. In those cases, you can download the library as a ZIP file and use Sketch > Include Library > Add .ZIP Library… to install it.

Step 4: Common library problems and troubleshooting

  1. Library Not Found Error:

    • If you get an error saying a library cannot be found, check that it is installed correctly via the Library Manager and that your #include statement is spelled correctly.
  2. Library conflicts:

    • Sometimes multiple versions of the same library can cause conflicts. You can resolve this by uninstalling older versions or specifying which version to use in the Library Manager.
  3. Version compatibility:

    • Some libraries depend on specific board versions or Arduino IDE versions. Always check the documentation to ensure that the library version is compatible with your setup.

Summary

In this lesson, you have been introduced to the concept of libraries in Arduino IDE. Libraries are powerful tools that save time and reduce complexity by providing prewritten functions for hardware components and much more. You have now learned how to search for, install, and include libraries in your sketches using Arduino IDE's Library Manager.

In future lessons, we will take a closer look at how to use specific libraries to work with hardware such as sensors and displays. Understanding how to manage libraries will make it easier for you to add more advanced features to your IoT projects.

About the measurements: The station in this series measures temperature and relative humidity using the AHT10. These are indoor climate readings, not direct measurements of CO2, VOCs, or particles. The series name “air quality monitoring” should be understood with this limitation.

Leave a comment

Please note, comments need to be approved before they are published.