How to measure Humidity and Temperature using DHT22 and Arduino

In this project we will learn to measure Humidity and temperature using DHT22 and Arduino.

There are 2 version of the sensor DHT11 and DHT22.

DHT22 sensor is more expensive and obviously has better performance and accuracy. Its temperature measure range is -40 to 80 degree Celsius with +-0.5 degree accuracy. Also the DHT22 sensor has better humidity measuring range, from 0 to 100% with 2-5% accuracy. Operating voltage is 3v-5v.

Circuit and Schematics

Download the DHT library for Arduino from location.

Https://GitHub.com/adafruit/DHT-sensor-library

  • Connect Pin 1 of DHT22 to Arduino 5v
  • Connect Pin 2 of DHT22 to Data pin of Arduino ( eg: Pin 2 or Pin3)
  • Connect Pin 3 of DHT22 to Ground pin of Arduino

DHT22 Schematics

DHT22Code

Code v1 – without LCD

#include <DHT.h>
#define DHTPIN 2    //Defines the pin number to which the sensor is connected.
#define DHTTYPE DHT22

DHT dht(DHTPIN,DHTTYPE); //creates a DHT object
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
float t = dht.readTemperature(); // Gets the values of the temperature
float h = dht.readHumidity(); // Gets the values of the humidity
if(isnan(t) || isnan(h))
{
Serial.println(“Failed to read from DHT sensor!”);
return;
}
// Printing the results on the serial monitor
Serial.print(“Temperature = “);
Serial.print(t);
Serial.print(” *C “);
Serial.print(” Humidity = “);
Serial.print(h);
Serial.println(” % “);
delay(2000); // Delays 2 secods, as the DHT22 sampling rate is 0.5Hz
}

DHT22LCD-code

Code v2 – With LCD display

Add LCD to the above code. Use my other post on how to connect LCD to Arduino for more details.

#include <DHT.h>
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
#define DHTPIN 3    //Defines the pin number to which the sensor is connected.
#define DHTTYPE DHT22
LiquidCrystal lcd(1, 2, 5, 6, 7,8); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
DHT dht(DHTPIN,DHTTYPE); //creates a DHT object

void setup() {
// put your setup code here, to run once:
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
lcd.print(“Digitalab.org”);
}
void loop() {
// put your main code here, to run repeatedly:
float t = dht.readTemperature(); // Gets the values of the temperature
float h = dht.readHumidity(); // Gets the values of the humidity
if(isnan(t) || isnan(h))
{
Serial.println(“Failed to read from DHT sensor!”);
return;
}
// Printing the results on the serial monitor
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print(“Temp.: “); // Prints string “Temp.” on the LCD
lcd.print(t); // Prints the temperature value from the sensor
lcd.print(” C”);
lcd.setCursor(0,1);
lcd.print(“Humi.: “);
lcd.print(h);
lcd.print(” %”);
delay(2000); // Delays 2 secods, as the DHT22 sampling rate is 0.5Hz
}

Completed Versions

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.