Class for DHTxx sensors (xx = 11-12-21-22-33-44).

Last Modified: October 10, 2016, at 05:00 PM
By: Daniel Plasa
Platforms: UNO, Leonardo, Nano

Download

Latest version on github

Intro

Looking for a cheap DHT11-like sensor with better resolution and range I came across the DHT12 sensor which is a cheap ebay buy (1$/1€). It features 0.1 resolution in temperature and humidity readings and its data format is compatible to the other DHTxx sensors.
The DHT12 looks pin-compatible, however it is not! Data is exchanged via i2c instead of 1-wire. It took me quite some time to figure that out as information on the internet is scarce... All searches lead me to DHT11 resources and I wrongfully assumed also having 4 pins means pin-compatibility. Kudos to this guy https://codeload.github.com/Bobadas/DHT12_library_Arduino who managed to provide a data-sheet (in Chinese) as well as some descriptions including a pin description of the DHT12 and a wiring diagram.

DHT12 Features

From the Chinese data-sheet:

  • Relative Humidity
    • Resolution: 0.1%RH
    • Repeatability: ±1%RH
    • Accuracy: ±5%RH (@25°C)
    • Long-term Stability:<±0.5%RH/yr
    • Response Time: 1/e(63%)25°C 20s 1m/s air 6s
    • Sluggish:<±0.3%RH

I have no clear idea what the latter two mean...

  • Temperature
    • Resolution: 0.1°C
    • Repeatability: ±0.2°C
    • Accuracy: ±0.5°C (@25°C)
    • Response Time:1/e(63%)20s

Again, I have no clear idea what the latter means... I would assume the sensor does internally some kind of integration or moving average and these time would be the times until changes in sensed data is reflected to the readout.

  • Electrical Characteristics
    • Power Supply: DC 2.7-5.5V
    • Current: 1mA
    • Standby: 60uA
    • Sampling Period: >2s
    • Pin Definition 1 - VDD 2 - SDA 3 - GND 4 - SCL

References

Standing on the shoulder of giants... I kludged together my library from DHTLib and DHT11Lib and extended it for the DHT12 sensor. You should definitely check out these two pages, it is a good read around using the DHT sensors. My library basically uses this great pieces of work but is blended with my personal OO preferences :)

The library

Since DHT11 sensors provide a integer resolution and the other DHTxx sensors provide 0.1 resolution, I decided (avoiding floating point arithmetic) to return all values (temperature and humidity) in x10 scaled format. So reading a 21.2°C temperature my library would yield 210 on a DHT11 or 212 on a better, say DHT12 sensor.

The library provides two classes:

  • dht1wire(uint8_t _pin, dhtmodels _model);
    Use this class for any 1-wire sensor, instantiate with e.g. dht1wire sensor(11, dth::DHT11);
  • dht12(uint8_t _id = 0x5c);
    Use this class for the i2c DHT12 sensor, instantiate e.g. with dht12 sensor(0x5c); (0x5c is the default i2c bus address of the DHT12 sensor)
    Please note that due to a shortcoming of the Arduino build system you would also have to manually include the Wire library, as the build system fails to retrieve dependencies from libraries itself :(

Both classes derive from a common base class dht, which does all the common stuff like data conversion and provides the enums for model names (e.g. dht::DHT11) and return values of the read() function. I also kludged in the sloppy and quick dew point temperature function (using floats - there is no reasonable way avoiding this). dht1wire and dht12 implement the specific _readSensor() and _storeData() function that actually does the communication with the sensor.

Example

A very minimal sketch that reads out a DHT11 sensor every 5 seconds might look like this:


#include <Wire.h>
#include <dht.h>

#define DHT11PIN 8
dht1wire DHT(DHT11PIN, dht::DHT11);

void setup()
{
  Serial.begin(9600);
  Serial.println(F("DHTxx TEST PROGRAM"));
}

void loop()
{
  dht::ReadStatus = DHT.read();

  Serial.print(F("Humidity (%): "));
  Serial.println(DHT.getHumidity());

  Serial.print(F("Temperature (°C): "));
  Serial.println(DHT.getTemperature());

  delay(5000);
}

Share