Arduino - DHT22 AM2302 example code



This is very simple project that will Arduino read data from DHT22 to show humidity and temperature.

As specification datasheet, DHT22 use lowe power 3-5VDC, 2.5mA max (when data sending). Measuring range is 0-100%RH (accuracy ±2-5%), -40 to 80°C (accuracy ±0.5°C). However, it is quiet slow response. It needs "delay" time at each time getting data so called "sampling time" in 2 seconds.

Hardware need:
1. Arduino Pro Mini
2. Sensor DHT22
3. Some jumper cables



Step 1. Making circuit
The circuit is simple, when power source can take from Arduino Pro Mini, data cable can connect to any pin (let's say pin no. 7)


Step 2. Code works
In order to use DHT22 sensor, library for it need to be installed

It also need to copy file Adafruit_sensor.h into Library directory. The file can be download here (Google share link)
Note that: lack of Adafruit_Sensor.h will trigger the error:
C:\Users\xxxx\Documents\Arduino\libraries\DHT_sensor_library\DHT_U.h:25:29: fatal error: Adafruit_Sensor.h: No such file or directory

That's it. Enough lib and file, the code is compiled successful:
#include <DHT.h>;

//Constants
#define DHT_PIN 7           // Output of DHT22 to pin 7 Arduino
#define DHT_TYPE DHT22      // DHT22 (AM2302)
DHT dht(DHT_PIN, DHT_TYPE); // Initialize DHT sensor for Arduino 16MHz


//Variables
int chk;
float hum;  //Stores humidity value
float temp; //Stores temperature value

void setup()
{
  Serial.begin(9600);
  dht.begin();
}

void loop()
{
    //Read value of Humidity and Temperature
    hum = dht.readHumidity();
    temp= dht.readTemperature();
    
    //Print out value of Humidity and Temperature
    Serial.print("Humidity is: ");
    Serial.print(hum);
    Serial.print("% --- Temp is: ");
    Serial.print(temp);
    Serial.println("dgC");
    delay(2000); //Delay 2 sec.
}
Result screen:
As checking with another temperature sensor, the temperature from DHT22 is near with the sensor. However, with humidity, we don't have test tool to test it.





3 comments:


  1. hello if you can put in your application and sensor application SI 7021 !!

    ReplyDelete
  2. hello, is THIS CALIBRATED DHT22 FULL FAILURE THAT IT WILL MAKE THIS PARAMETERS

    ReplyDelete
    Replies
    1. every sensor has its own parameter, and it will need standard humidity to find the parameters.

      Delete