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

Why would you make the 'invalid value' 0? How do you measure an 0 degrees temperature then? (it's less of a issue with humidity)

The invalid value in 0.1.14 = DHTLIB_INVALID_VALUE = -999

in version 0.1.17 the DHTLIB_INVALID_VALUE is removed as sometimes the values are good and there is only a bit-error in the CRC. So the return value of the read() functions must be checked.

Also note that DHT11 use only integer values. That gives rounding errors by definition:)

Do you have an idea why some sensors might be way off.

Check the datasheets, the DHT sensors are not that accurate (esp the DHT11).
See detailed tests here - sensors:temp-hum_sensors_compared [Boxtec Playground] -

My application will use long cables (10-30m). What are the risks associated with this?

The signal is digital so it either fails or succeeds, it will not drift. You might need to use a smaller pull up resistor. I always put them near Arduino as there 5Volt == 5Volt.

Question for Rob - have you had any experience with the DHT series sensors and very low RH values?
I seem to have an issue with RH values less than 15% being very very inaccurate and just looking to see what other users have found!

I've used the DHT's mostly at room temperature (say 5-40 degrees) and humidity between 35-75 %
Once I did use it in a room about 60-70 degrees but I do not recall what humidity was.

never did look at the humidity extremes.

Thanks Rob - down here in Australia Summer daytime RH can get down to 5% (or less!) and I'm having trouble being able to measure it which is an issue for my application :-/

Anyone out there got any experience with the DHT sensors at these ultra low RH values?

I have finally found time to work on a fix for the DHT11 sensor in the DHTlib.
A beta version of the 0.1.18 lib is attached.

Could anyone with a DHT11 on UNO/MEGA confirm it works?
Note It won't work on any ARM/DUE.

0.1.18beta.zip (3.52 KB)

Finally got my new DHT11 and some time to test the 0.1.18(beta) version to see if the fix works.
And I'm happy to announce it does :slight_smile:

So I pushed the 0.1.18 version to - Arduino/libraries/DHTlib at master · RobTillaart/Arduino · GitHub -

Please note that 0.1.18 does not work on ARM processors (e.g. Due), please use the 0.1.13 version.

Hey,

I've been playing around with DHT11 sensor from ebay, and found that on all of the libraries that i've tested, sensor reads about 5 C high..

i bought 5 of those sensors on the same order and all of them seems to have a similar offset. I can't messure if the humidity has offset too because i don't have a refrence humidity meter to hand.

also if i breath on sensor humidity shoots up to about 63% and then shows checksum error..

have anyone had similar problems?

arduino Nano at 16MHz, stable 5V power supply, 10k signal pull-up, 5cm from the board, along side there is DS18B20 (it works just fine)(no change if i disconnect it).

sorry for my english and thanks.

Hi KiloWatt

The DHT11 is not an accurate sensor and you can compensate for the temperature error in software quite easily. HUmidity is another story...

Check this comparison - sensors:temp-hum_sensors_compared [Boxtec Playground] -

which version of the library are you using?

Hi Rob,
I am fairly new to arduino, so my apologies for my ignorance. For my second "serious" project, I want to be able to monitor temperatures in a crawl space, and activate a heat tape in the event of freeze conditions. To that end I acquired a DHT21

Hi Rob,
Great work! I am fairly new to arduino, so my apologies for my ignorance. For my second "serious" project, I want to be able to monitor temperatures in a crawl space, and activate a heat tape in the event of freeze conditions. To that end I acquired a DHT21 sensor. Looking at the documentation am I correct thinking it is similar to the DHT22 other than pin out? I am assuming that your 0.1.18 library covers this sensor as well, so I downloaded from the link here. However when I tried to import it to the IDE I get an error that that seems to deal with naming conventions. So I renamed it and it presumably loaded, but if I try to include it into a sketch, nothing is there. I'm quite sure I'm doing something wrong, but not sure what. Working this project in stages, at this point I simply want to read data from the sensor, and display it on a 1602 display, any steering in this direction would be greatly appreciated.

DHT21 and 22 are interface identical, you're right on that.

the library folder should be named dht with in it dht.cpp and dht.h and an examples subfolder
then it should work...

Thanks for the reply Rob, forgive my brain slip. I did get the library installed, uploaded to my UNO, and the test sketch ran well. I temporarily "rem'd" out the items for the DHT11, and 22, so I could simply read my DHT21 for testing purposes. Information flowed fine to the serial monitor. Although I was interested to see the readings wander a bit. I understand that sensor is not extremely precise, but I'd have thought if it were +or- .X it would stay at that point. Instead it would read, for example 71.2.....71.0....71.0....71.2. Curious.

I wanted then to display the data on a 1602 type display, rather than via the serial monitor. I used the lcd library, and combined it with what I needed to read the DHT21. There was more characters than would fit or needed on the display, so I "rem'd" a few out again, :blush: leaving just the humidity and temp data sent to the display. Interestingly the Humidity displayed with 10 decimal points ie: 8.6000993810 while the temperature displayed as 69.4 ( I converted the output to F before the lcd.print function) with only one decimal. I'm not sure I get why that is happening. Ideally one decimal on both would be better. Any suggestions always appreciated.
Thanks again for great work and assistance.

with respect to the accuracy, read the datasheet.
You can do some averaging

Post your sketch and I'll have a look

Thanks Rob, I think I found it. First part was an oversite on my part. Both readings were going out 10 decimal places. Since the temperature was sent to the display second, it merely truncated. Secondly, to send data to the display I used the statement "lcd.print (DHT.temperature, DEC). When I remove the optional 2nd parameter (DEC) it only displays 2 decimal places. Since this is only a testing/learning phase I wasn't too concerned with the mathmatics of rounding etc. So at this point this works out fine. Thanks again for having a newbie's back :stuck_out_tongue: . I hope at some point I'll have learned enough to help as many people as you have. Cheers!

Hi again Rob,
I am happy to say I have a working prototype for my project. Using your library, which probably has more than I need being it supports 3 different sensors, I Rem'd out some, and deleted some statements in the main body of the sketch and changed serial outputs to the 16X2 lcd display. All worked fine. I even added a long 3 conductor cable to the sensor and placed out doors for testing. Sure enough when it cooled down to 0C the control relay operated :smiley: . While hooking up my "extended" wiring, I left the sketch running. As i removed and attached each lead to the sensor, it appeared that the last reading was held in DHT.temp. This could be an issue if in practice communications are lost to the sensor and it is not known. I am testing DHT.temperature in an if statement to control the ouput to a relay. If I see it correctly, if a temperature above freezing is read, and the communication is lost the last reading would be held. If temperatures then drop below freezing it would not be "seen" and heat tape would not be activated. Any thoughts on that appreciated. As always thanks for helping us newbies along.

you should check the return value of the read() function.

If it equals DHTLIB_OK there is a new reading

If it equals DHTLIC_ERROR_CHECKSUM, all bits are read but there are error(s), the temperature/humidity might be right or not. Comparing with a previous value might bring a clue. e.g. you do not expect T or H to drop a large amount between two adjacent readings.

DHTLIB_TIMEOUT: not all bits are read so the values are probably not right but might be right. Here too comparing with a previous value might bring some clue.

DHTLIB_ERROR_CONNECT, DHTLIB_ERROR_ACK_L and DHTLIB_ERROR_ACK_H: no new values read.

  int chk = DHT.read22(DHT22_PIN);
  switch (chk)
  {
    case DHTLIB_OK:  
		Serial.print("OK,\t"); 
		break;
    case DHTLIB_ERROR_CHECKSUM: 
		Serial.print("Checksum error,\t"); 
		break;
    case DHTLIB_ERROR_TIMEOUT: 
		Serial.print("Time out error,\t"); 
		break;
    case DHTLIB_ERROR_CONNECT:
        Serial.print("Connect error,\t");
        break;
    case DHTLIB_ERROR_ACK_L:
        Serial.print("Ack Low error,\t");
        break;
    case DHTLIB_ERROR_ACK_H:
        Serial.print("Ack High error,\t");
        break;
    default: 
		Serial.print("Unknown error,\t"); 
		break;
  }

Ah, great I can work with that, thank you thank you!

The DHT11 code had a masking bug (Thanks Richard for pointing this out and providing the fix)

Pushed the 0.1.19 version - Arduino/libraries/DHTlib at master · RobTillaart/Arduino · GitHub -

Please note that 0.1.19 does not work on ARM processors (e.g. Due), please use the 0.1.13 version.

Still remarks and comments are welcome.

So much better than the Adafruit library, thankyou!

Regards,

Graham

+1

Hi,
I've purchased an Aosong AM2301 DHT21 sensor to use with a MEGA 2560 board. I've installed Rob Tillaart's latest library and example sketch but am having problems. The humidity reading is always 99.9% although the temperature reading is correct. Status OK.
This is happening on two sensors so is there something in the software or am I unlucky with two faulty sensors. I'm using pin 18 on the board.
Any help/advice would be appreciated.
Stephen