|Last Modified:||October 10, 2016, at 05:00 PM|
|Platforms:||UNO, Leonardo, Nano|
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.
From the Chinese data-sheet:
I have no clear idea what the latter two mean...
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.
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 :)
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);
dht1wire sensor(11, dth::DHT11);
dht12(uint8_t _id = 0x5c);
dht12 sensor(0x5c);(0x5c is the default i2c bus address of the DHT12 sensor)
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
_storeData() function that actually does the communication with the sensor.
A very minimal sketch that reads out a DHT11 sensor every 5 seconds might look like this:
#define DHT11PIN 8
dht1wire DHT(DHT11PIN, dht::DHT11);
Serial.println(F("DHTxx TEST PROGRAM"));
dht::ReadStatus = DHT.read();
Serial.print(F("Humidity (%): "));
Serial.print(F("Temperature (°C): "));