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

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



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 ?

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?


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.



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

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

HI Marcello,

This line of code sets a 1 bit on a specific bit position (the cnt'th bit).

As the bits are preset to 0 on all bit positions they do not need to be explicitly cleared, e.g. like the code below would do.

if ((micros() - t) > 40) bitset(bits[idx], cnt);
else bitclear(bits[idx], cnt)

The timeout is guarded by the loops above this line with the variable loopCnt.

I have thought about a version of the DHT lib where timing and timeout is replaced by a proper call to micros(); That way the protocol would work on faster or slower CPU's. Problem with micros() however is that for a 16Mhz Arduino it has a granularity of 4 uSec. An 8MHZ version would have a micros() granularity of 8 or maybe even more. That would make it hard to do the short timing. (Imagine a tiny85 working on 1 MHz => would not work) For the faster processors like the DUE, the lib would work probably better with micros() for timekeeping.

HI Rob:

Of course.

I was looking at the code again. Get that. Going to change my question.

But you go too fast. :astonished:

you will pick up speed as long as you keep asking questions! (no there ain't no bad questions, only bad answers)

Updated the playground to version 0.1.09 + reduced the foot print with 30 bytes. - predecrement in timeoutloops - use of mask to build up bytes.

Recently confirmed to work on: Digistump Digix 84 MHz Clock Speed too. - -

I've copy+paste the code for the dht.h and dht.cpp from github. But if i use it for my scetch i have this strange "\stray" errors.

.../dht.h:1: error: stray '\357' in program .../dht.h:1: error: stray '\273' in program .../dht.h:1: error: stray '\277' in program

I think it means there is an error in the dht.h file. Isn't it? With the older revision of the library everything was fine.

cannot replicate your error.

Can you zip the dht.h that fails and attach it in a post here ? Are you on windows mac unix? What editor (settings) are you using? ==> is the file changed to UTF-8 format instead of ASCII?

link to the previous version 1.0.8 - -

Hi Rob, thanks for reply. You had the right instinct. :) There was the same 3 characters in both files too much. The files was changed to UTF-8 and everything seems ok.

Many thanks . Joerg