Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Using Libraries

Introduction:

Libraries in Arduino provide ready-made code that simplifies working with various hardware modules and sensors. Instead of writing low-level code for each component, libraries offer functions for quick implementation, making development faster and more efficient.

Objective:

Learn how to install and use a library by displaying temperature and humidity on an LCD using the DHT11 temperature sensor and LiquidCrystal library.

Required Components:

  1. Arduino Uno
  2. DHT11 temperature and humidity sensor
  3. 16×2 LCD display
  4. 10k potentiometer (for LCD contrast)
  5. 220-ohm resistor (for LCD backlight)
  6. Jumper wires and breadboard

Libraries Used:

  1. DHT: For reading temperature and humidity from the DHT11 sensor.
  2. LiquidCrystal: For controlling a 16×2 LCD display.

Step 1: Installing Libraries

  1. Open the Arduino IDE.
  2. Go to Sketch > Include Library > Manage Libraries.
  3. In the Library Manager, search for DHT sensor library by Adafruit and LiquidCrystal.
  4. Install both libraries by clicking on the Install button next to each.

Step 2: Circuit Setup

  1. DHT11 Sensor:
    • Connect the VCC pin of the DHT11 to 5V on the Arduino.
    • Connect the GND pin to GND on the Arduino.
    • Connect the DATA pin to digital pin 2 on the Arduino.
  2. 16×2 LCD Display:
    • Pins 1 & 5: Connect GND.
    • Pin 2: Connect to 5V.
    • Pin 3: Connect to the middle pin of a 10k potentiometer (the outer pins of the potentiometer go to 5V and GND) to control the display contrast.
    • Pin 4 (RS): Connect to digital pin 7 on the Arduino.
    • Pin 6 (EN): Connect to digital pin 8.
    • Pins 11-14 (D4-D7): Connect to pins 9-12 on the Arduino.
    • Pin 15 (LED+): Connect to 5V through a 220-ohm resistor.
    • Pin 16 (LED-): Connect to GND.

Step 3: Writing the Code

This code initializes both the DHT11 sensor and the LCD display, reads temperature and humidity values, and displays them on the LCD.

// Include the libraries
#include <DHT.h>
#include <LiquidCrystal.h>

// Define pins and constants
#define DHTPIN 2            // DHT sensor connected to digital pin 2
#define DHTTYPE DHT11       // Define DHT11 sensor type
DHT dht(DHTPIN, DHTTYPE);   // Initialize the DHT sensor

// Initialize the LCD (RS, EN, D4, D5, D6, D7)
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup() {
  // Start the LCD and specify its size (16 columns, 2 rows)
  lcd.begin(16, 2);
  
  // Start the DHT sensor
  dht.begin();
  
  // Print initial message
  lcd.print("Temp & Humidity");
}

void loop() {
  // Read temperature and humidity from the DHT sensor
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  // Check if data is valid
  if (isnan(humidity) || isnan(temperature)) {
    lcd.setCursor(0, 1);  // Move to the second row
    lcd.print("Error reading ");
    return;
  }

  // Display temperature and humidity
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(temperature);
  lcd.print(" C");

  lcd.setCursor(0, 1);
  lcd.print("Humidity: ");
  lcd.print(humidity);
  lcd.print(" %");

  // Delay before refreshing the data
  delay(2000);
}

Code Explanation

  1. Library Inclusions:
    • #include <DHT.h> and #include <LiquidCrystal.h> import the libraries for controlling the DHT sensor and LCD.
  2. Definitions and Initializations:
    • #define DHTPIN 2 and #define DHTTYPE DHT11 define the data pin and type of the DHT sensor.
    • DHT dht(DHTPIN, DHTTYPE); initializes the DHT sensor with the specified pin and sensor type.
    • LiquidCrystal lcd(7, 8, 9, 10, 11, 12); initializes the LCD with the appropriate pin configuration for RS, EN, D4, D5, D6, and D7.
  3. Setup Function:
    • lcd.begin(16, 2); initializes the LCD to 16 columns and 2 rows.
    • dht.begin(); starts the DHT sensor to begin reading data.
  4. Loop Function:
    • float humidity = dht.readHumidity(); reads the humidity value.
    • float temperature = dht.readTemperature(); reads the temperature in Celsius.
    • isnan(humidity) || isnan(temperature) checks for valid readings.
    • The temperature and humidity are displayed on the LCD, with lcd.clear(); clearing previous data before each update.

How to View Results

  1. Upload the Code to the Arduino board.
  2. The LCD will display “Temp & Humidity” initially, then show the current temperature and humidity, refreshing every 2 seconds.

Result:

The temperature and humidity values are read from the DHT11 sensor and displayed on the LCD. This example demonstrates how to use libraries to simplify code and interact with common hardware modules easily.

Using libraries like DHT and LiquidCrystal makes it simple to access and control sensors and displays without needing to write detailed, low-level code.

    Leave a Reply

    Your email address will not be published.

    Need Help?