Temperature and humidity (DHT22/AM2302) library

DSC_4218

I wanted to include ambient temperature and humidity measurement in a project with the DHT22 sensor, I started by checking a couple of libraries, but they where not what I wanted.

Contents
    Temperature and humidity (DHT22/AM2302) library
        Requirements
        Connections
    The library functions
        Includes
        All functions
            Constructor
            Ready to do a conversion
            Get measurements
            Errors
        Caveat
        Example
    Conclusion
    Notes and download


Requirements

My requirement was fairly simple for this library.


Connections

DSC_4219



The library functions

Includes

Code:
#include <DTH22.h>
The library is named after one of the supported sensors


All functions

Constructor

Code:
      DHT22(byte pin);
The constructor must know what pin the data pin of the sensor is connected to. It will also add a delay before a conversion can be done (See ready()),


Ready to do a conversion

Code:
  boolean ready();
When the library reports ready the measure function can be called. The ready function is based on time done with millis(), not any check of the sensor. It is possible to do a new reading each 2.2 seconds.



Get measurements

This will fetch the latest conversion and start a new conversion.

Code:
  boolean measure(int &temperature, int &humidity);// Return values must be divided by 10
  boolean measure(float &temperature, float &humidity) {
The temperature and humidity is read with the same function, it will return true when it has read a value and false if the reading fails for some reason.
Fails can be either due to an error or calling before the sensor is ready.
The integer values are 10 times to high, i.e. 25.1 degree is return as 251, same for humidity.
The float routine will do the division, returning ready to use values.



Errors

Code:
    boolean isError();
If the communication with the sensor fails isError() will return true, called the measure function premature will not set an error.



Caveat

The library do not disable interrupts, that would make millis() go wrong, but it need a fairly good timing. This means interrupt routines may prevent the library from working reliable because it needs about 5ms for reading data from sensor.
Any interrupt routine taking much above 20us will disturb the communication.



Example

Code:
#include 

#define DHT22_PIN 14


DHT22 dht(DHT22_PIN);

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

void loop() {
  if (dht.ready()) {
    float temperature;
    float humidity;
     if (dht.measure(temperature, humidity)) {
       Serial.print("Temperature: ");
       Serial.print(temperature,1);
       Serial.print("*C   Humidity: ");
       Serial.print(humidity,1);
       Serial.println("%RH");
     }
  }
  if (dht.isError()) {
    Serial.println("Error in communication with DHT22 sensor");
    delay(3000);
  }
}
There is not needed much code to use the library.



Conclusion

I believe I succeed in making a simple library that do not block the processor, but the library is fairly sensitive to how interrupts are used.




Notes and download

Download library