Humidity- Temperature Sensor(DHT11)

About The Project

In this project, we are demonstrating how to measure temperature and humidity using the DHT11 Sensor.

Temperature and Humidity Sensor (DHT11)

The DHT11 sensor operates by sensing temperature and humidity through its integrated resistive elements. It uses a capacitive humidity sensor and a thermistor for temperature measurement. The sensor converts these analog measurements into digital signals that can be read by a microcontroller or other digital device, providing accurate readings of temperature and humidity in real-time.

DHT Library

It’s essential to ensure the DHT Library Installed: Download and install the DHT sensor library if you haven’t already. This library provides functions to easily interact with the DHT11 sensor.

To install the DHT sensor library in your Arduino IDE:

  1. Open the Arduino IDE.
  2. Go to Sketch > Include Library > Manage Libraries… This opens the Library Manager.
  3. In the Library Manager, type “DHT” into the search bar.
  4. Find the “DHT sensor library” by Adafruit or another reputable source.
  5. Click on the library, then click “Install”.
  6. Wait for the installation to complete.

Circuit Wiring

Program Code

C
// www.matthewtechub.com
// measure temperature and humidity
#include "DHT.h"
#define DHTPIN 2   
  // Pin which is connected to the DHT sensor
#define DHTTYPE DHT11   
// Define the type of DHT sensor used (DHT11)
DHT dht(DHTPIN, DHTTYPE);
void setup() {
  Serial.begin(9600);
  dht.begin();
}
void loop() {
  // Wait a few seconds between measurements
  delay(2000);
  // Reading temperature or humidity takes about 250 milliseconds!
  float humidity = dht.readHumidity();
  float temperatureC = dht.readTemperature();
  float temperatureF = dht.readTemperature(true); 
// Read temperature in Fahrenheit

  // Check if any reads failed and exit early (to try again).
  if (isnan(humidity) || isnan(temperatureC) || isnan(temperatureF)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");
  Serial.print("Temperature: ");
  Serial.print(temperatureC);
  Serial.print(" *C / ");
  Serial.print(temperatureF);
  Serial.println(" *F");
}

This Arduino program reads the humidity and temperature (in both Celsius and Fahrenheit) from a DHT11 sensor every 2 seconds and prints the values to the Serial Monitor. The program initializes the sensor and serial communication in the setup function and repeatedly takes readings and prints them in the loop function. If a reading fails, it prints an error message and retries after 2 seconds.

Code Explanation

C
#include "DHT.h" 

#include “DHT.h”: This line includes the DHT library, which provides functions to interact with the DHT sensor. You need to have this library installed in your Arduino IDE.

C
#define DHTPIN 2    
#define DHTTYPE DHT11  
DHT dht(DHTPIN, DHTTYPE); 
  • #define DHTPIN 2: Defines the Arduino pin number (2) to which the DHT sensor data pin is connected.
  • #define DHTTYPE DHT11: Defines the type of DHT sensor being used (DHT11 in this case).
  • DHT dht(DHTPIN, DHTTYPE);: Creates an instance of the DHT sensor object, specifying the pin and sensor type.
C
void setup() {
  Serial.begin(9600); 
  dht.begin(); 
}
  • Serial.begin(9600);: Initializes serial communication with a baud rate of 9600 bits per second, allowing data to be sent to the Serial Monitor.
  • dht.begin();: Initializes the DHT sensor so it is ready to take readings.
C
void loop() {
  delay(2000); 
  // Read humidity and temperature
  float humidity = dht.readHumidity();
  float temperatureC = dht.readTemperature(); 
  float temperatureF = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(humidity) || isnan(temperatureC) || isnan(temperatureF)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Print the values to the Serial Monitor
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");
  Serial.print("Temperature: ");
  Serial.print(temperatureC);
  Serial.print(" *C / ");
  Serial.print(temperatureF);
  Serial.println(" *F");
}
  • delay(2000);: Pauses the program for 2 seconds (2000 milliseconds) between each reading to avoid excessive polling of the sensor.
  • float humidity = dht.readHumidity();: Reads the humidity value from the DHT sensor and stores it in the variable humidity.
  • float temperatureC = dht.readTemperature();: Reads the temperature in Celsius from the DHT sensor and stores it in the variable temperatureC.
  • float temperatureF = dht.readTemperature(true);: Reads the temperature in Fahrenheit from the DHT sensor and stores it in the variable temperatureF. The true parameter indicates that the reading should be in Fahrenheit.
  • if (isnan(humidity) || isnan(temperatureC) || isnan(temperatureF)): Checks if any of the readings are NaN (Not a Number), indicating a failed read. If any reading failed, it prints an error message and exits the loop early.
  • Serial.print(“Humidity: “);: Prints the text “Humidity: ” to the Serial Monitor.
  • Serial.print(humidity);: Prints the humidity value to the Serial Monitor.
  • Serial.println(” %”);: Prints the percent symbol and a newline character to the Serial Monitor.
  • Serial.print(“Temperature: “);: Prints the text “Temperature: ” to the Serial Monitor.
  • Serial.print(temperatureC);: Prints the temperature in Celsius to the Serial Monitor.
  • Serial.print(” *C / “);: Prints the text ” *C / ” to the Serial Monitor.
  • Serial.print(temperatureF);: Prints the temperature in Fahrenheit to the Serial Monitor.
  • Serial.println(” *F”);: Prints the text ” *F” and a newline character to the Serial Monitor.

Modify Yourself

  1. Place a filament bulb inside a cardboard box, switch it on, and then measure the humidity and temperature inside the box.
  2. Place a glass of water inside the cardboard box and measure the humidity and temperature again.

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!