This sensor is used to measure the temperature and humidity. The feature of this sensor is that we do not need any components to measure temperature and humidity.
We have two types of DHT sensors. DHT11 and DHT22 here dht-22 is more expensive than dht11. When dht22 is expensive than obviously it has more accurate than dht11. DHT11 temperature range is from 0°C to 50°C with +-2 degrees accuracy. DHT-22’s temperature measuring range is from -40°C to +125°C with +-0.5 degrees accuracy.
Operating Voltage | 3 to 5V | 3 to 5V |
Max Operating Current | 2.5mA max | 2.5mA max |
Humidity Range | 20-80% / 5% | 0-100% / 2-5% |
Temperature Range | 0-50°C / ± 2°C | -40 to 80°C / ± 0.5°C |
Sampling Rate | 1 Hz (reading every second) | 0.5 Hz (reading every 2 seconds) |
Body size | 15.5mm x 12mm x 5.5mm | 15.1mm x 25mm x 7.7mm |
Advantage | Ultra low cost | More Accurate |
DHT22 Pinouts
DHT-22 has four pins. GND, Vcc, Do (digital input).
First of all Vcc and GND goes to 5V and GND as well. Data pin of sensor goes to the digital pin o Arduino.
Arduino code for Temperature and Humidity:
This sensor have wire protocol to communicate with Arduino. But we fortunately we don’t have to worry about this protocol because we are going to use DHT library which takes care of everything.
DHT zip file here.
To install it, open the Arduino IDE, go to Sketch > Include Library > Add .ZIP Library, and then select the DHTlib ZIP file that you just downloaded.
//code start from here.
#include <dht.h>
#define dataPin 8 // define sensor pin
dht DHT; creating DHT object
void setup()
{
Serial.begin(9600);
}
void loop()
{
//Uncomment whatever type you're using!
int senseData = DHT.read22(dataPin); // DHT22/AM2302
//int senseData = DHT.read11(dataPin); // DHT11
float t = DHT.temperature; // Gets the values of the temperature
float h = DHT.humidity; // Gets the values of the humidity
// Printing the results on the serial monitor
Serial.print("Temperature = ");
Serial.print(t);
Serial.print(" ");
Serial.print((char)176);//shows degrees character
Serial.print("C | ");
Serial.print((t * 9.0) / 5.0 + 32.0);//print the temperature in Fahrenheit
Serial.print(" ");
Serial.print((char)176);//shows degrees character
Serial.println("F ");
Serial.print("Humidity = ");
Serial.print(h);
Serial.println(" % ");
Serial.println("");
delay(2000); // Delays 2 secods
}
After uploading code open serial monitor to see your results.
Code explanation
First of all we have to add Arduino library to use its function. Then define DHT data pin that where to connect. Make an object to use special function related to library.
Now in void setup, we have to begin serial where we can communicate or see our results. Now use read22 () to read temperature from the sensor using digital pin of Arduino. The gives temperature in Celsius. We can convert it in Fahrenheit by using formula:
T(°F) = T(°C) × 9/5 + 32