Class for DHT11, DHT21 and DHT22 (temperature & humidity)

Rob Tillaart suggested that I created a another thread for the new library, so I did: http://forum.arduino.cc/index.php?topic=171677.0. This way we keep this thread focussed on Rob's great work.

I reworked my library and just commited all my changes for some more features.

Hello Rob, I tried this library on Arduino Due and it seems since its SAM series processor the includes for AVR wont work (avr/io.h etc). Can you suggest changes to make this library compile and work on Arduino Due boards .

Thanks for the help

Added support for the DHT21 (AM2301) today - http://playground.arduino.cc/Main/DHTLib -

datasheet DHT21: - http://www.electrodragon.com/w/images/6/6f/DHT21.pdf - Seen at: - http://www.electrodragon.com/?product=am2301-dht21-digital-temperature-humidity-sensor -

The DHT21 has the same handshake as the DHT11 and DHT22. The translation to real values (TEMP/RH) is different again. As I do not have the sensor yet I have not tested it, so all feedback is welcome.

As always remarks and feedback is welcome.

Sorry for the late reaction, I missed it somehow.

sanhardik: Hello Rob, I tried this library on Arduino Due and it seems since its SAM series processor the includes for AVR wont work (avr/io.h etc). Can you suggest changes to make this library compile and work on Arduino Due boards .

Thanks for the help

I have no Due operational, but you should check the private read() function. It uses a #define TIMEOUT 10000 which is tuned for the 16Mhz UNO. so it will trigger too fast on an DUE. As the Due is 84 MHz ==> 5.25 x as fast, you should adjust the TIMEOUT to 60000 (at least)

A more permanent solution should derive a clock dependant timeout like this

#define TIMEOUT (F_CPU/1600)

please give it a try.

update: Got mail today, DHT21 testing is in progress, the code in the lib seems not to decode right.

The decoding was based upon this hint from the datasheet: "DATA=8 bit integral RH data+8 bit decimal RH data+8 bit integral T data+8 bit decimal T data+8 bit check-sum " HEX dumps of received bytes have a correct CRC. A new decoding scheme is derived from the dumps and currently under test.

I have some problems with the library. I have an arduino due and an am2301 sensor (DHT21). The temperature at the moment that I tested the sensor was more or less 22 degrees. I set up: #define TIMEOUT 52500 in dht.cpp

Data showned in dht_test.ino is: Humidity 2.3 Temperature 0.9

Any idea?? Thanks!

wifialex: I have some problems with the library. I have an arduino due and an am2301 sensor (DHT21). The temperature at the moment that I tested the sensor was more or less 22 degrees. I set up: #define TIMEOUT 52500 in dht.cpp

Data showned in dht_test.ino is: Humidity 2.3 Temperature 0.9

Any idea?? Thanks!

Yes, the code for the support of the DHT21 is experimental, based on hints in a datasheet (link see a few pages above) and just does not work yet.

Can you please access the DHT21 as if it was a DHT22 so use read22(pin) instead of read21(pin)?

I have strong indications that the formula for DHT21 and DHT22 are identical although the datasheet hinted otherwise. I have no confirmation yet and maybe you can confirm (as I do not have the DHT21 myself).

When my assumption is confirmed I can update the code.

I have just checked it using dht22 read. The measures are closed to real but there are errors: Humidity is lower than real Temperature is higher than real

Can you give exact figures, lower and higher are too subjective.

Low humidity is often mentioned as problem with the DHT sensors, temperature can depend on air flow but should be within 3C. In your code you can add an offset to adjust the temp to the real value. For humidity this will probably not work.

First of all, sorry for my delayed answer, but I was very busy. The first one pic sensor was calibrated two months ago, so it should be very accurate. The second one pic is DHT21 data

20130920_174238.jpg|2448x1836

20130920_174228.jpg|2448x1836

This proves that the DHT21 uses the same conversion as the DHT22. I will update the playground and the lib. The delta in temperature is within acceptable errors. You might add an offset to adjust.

Thanks for testing,

You are welcome, I added an offset -2º in temperature data Thanks!

How did this library compare with the popular Adafruit one ? https://github.com/adafruit/DHT-sensor-library

In any case, maybe you add their lib to the other "Library list".

wifialex: You are welcome, I added an offset -2º in temperature data Thanks!

Do you (or others) think the library should support for a corrective offset?

e.g.

void setTempOffset(float delta);
float getTempOffset();
void setHumOffset(float delta);
float getHumOffset();

or in the constructor...

will work better if the library uses an object per sensor iso one object for all sensors. need a rewrite... something like * *DHT22 sensor1(pin, offset);

Updated the playground to version 0.1.08 major change - support for DUE (adaptive TIMEOUT).

Greetings, Thanks for the work. I am having problems with compiling the code. I have an UDOO and I am attempting to execute the code on the DUE side. I get the following error though. Any help would be greatly appreciated.

V/r

Derrick

DHT/dht.cpp.o: In function dht::read(unsigned char)': /home/ubuntu/Arduino/libraries/DHT/dht.cpp:116: multiple definition ofdht::read(unsigned char)' dht.cpp.o:dht.cpp:116: first defined here DHT/dht.cpp.o: In function dht::read22(unsigned char)': /home/ubuntu/Arduino/libraries/DHT/dht.cpp:78: multiple definition ofdht::read22(unsigned char)' dht.cpp.o:dht.cpp:78: first defined here DHT/dht.cpp.o: In function dht::read21(unsigned char)': /home/ubuntu/Arduino/libraries/DHT/dht.cpp:70: multiple definition ofdht::read21(unsigned char)' dht.cpp.o:dht.cpp:70: first defined here DHT/dht.cpp.o: In function dht::read11(unsigned char)': /home/ubuntu/Arduino/libraries/DHT/dht.cpp:42: multiple definition ofdht::read11(unsigned char)' dht.cpp.o:dht.cpp:42: first defined here /opt/arduino-1.5.4/hardware/tools/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib/thumb/libc.a(lib_a-sbrkr.o): In function _sbrk_r': sbrkr.c:(.text._sbrk_r+0xc): warning: undefined reference to_sbrk' collect2: ld returned 1 exit status

do you have the lib included twice? copied it to 2 locations? (sketch folder , library folder, arduino lib folder?)

Thanks for the reply, Apparently, it was Arduino IDE issue. Initially, I placed the sketch in the same place as I did the libs. Once I changed that the sketched worked. I tried the latetst lib and code for the DHT22, and I noticed that it did not execute at all. Is it suppose to work with the DUE? IT compiled fine however, there is no output from the Arduino serial monitor even though there are serial print functions listed. Even if something was wrong with the libs, those statements should print. Not sure if it is a UDOO, DUE, or library thing. V/r Derrick

Can you post the code you used?

Do you check the return values the library gives for read?

If you use DHT11 in your code and sample a DHT22 (HW) you should get some values back. Can you try that?

Hi Rob:

Thank you for your easy and great code. I´m learning a lot.

By the way: Doesnt if ((micros() - t) > 40) {} require something like else return DHTLIB_ERROR_TIMEOUT; ?

Also, isnt bits() a Byte?

Regards: Marcelo from Buenos Aires