Pages: 1 2 3 [4] 5 6   Go Down
Author Topic: Class for DHT11, DHT21 and DHT22 (temperature & humidity)  (Read 40345 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just a few questions while checking both DHT versions:

Rob:
- Has a delay of 20msec for both DHT11 and DHT22.
- Uses a counter in the while to break out of the while in case the pin does not change

Mark:
- Has a delay of 18msec for the DHT11 and 800usec for the DHT22.
- Uses an endless while loop, so if the pin does not change --> Arduino in endless loop!

Further:
- I am using multiple DHT22's on different pins. Which library would be most RAM efficient?
- Is the 800usec the right delay for the DHT22? If so, it saves me 5*19msec = 75msec of doing nothing, which is a lot using NilRTOS/ChibiOS RTOSses...

Please comment  smiley-cool

  • 800usec for a DHT22 is according to the datasheets I found. And it works.
  • My library won't hang. The endless loop you mention has a return statement which is called after 85 usec.
  • Per DHT instance, you need 18 bytes of data, perhaps a little more overhead, I would need to measure to give a hard number. So no problem to run a lot of DHT sensors (tried two myself). The advantage of having an instance per sensor is that you can read out them in parallel. On the other hand, it would be straightforward to alter setup() to something like setup(pin, model) and then you could reuse the instance. But not sure if the effort is worth the 18*(sensor-1) bytes...
« Last Edit: June 11, 2013, 04:46:40 pm by Mark Ruys » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

  • 800usec for a DHT22 is according to the datasheets I found. And it works.
Nice. Will try with the current lib I'm using!
Quote
  • My library won't hang. The endless loop you mention has a return statement which is called after 85 usec.
  • Per DHT instance, you need 18 bytes of data, perhaps a little more overhead, I would need to measure to give a hard number. So no problem to run a lot of DHT sensors (tried two myself). The advantage of having an instance per sensor is that you can read out them in parallel. On the other hand, it would be straightforward to alter setup() to something like setup(pin, model) and then you could reuse the instance. But not sure if the effort is worth the 18*(sensor-1) bytes...
Ah! You are right, I misread some of the C++ code  smiley-zipper
18 bytes is not very much indeed. Thanx!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12428
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: August 27, 2013, 05:44:49 am by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12428
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry for the late reaction, I missed it somehow.

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

Code:
#define TIMEOUT (F_CPU/1600)

please give it a try.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12428
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: September 13, 2013, 07:01:44 am by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
« Last Edit: September 14, 2013, 08:34:55 pm by wifialex » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12428
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12428
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 (1139.29 KB, 2448x1836 - viewed 109 times.)

* 20130920_174228.jpg (1566.5 KB, 2448x1836 - viewed 135 times.)
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12428
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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,
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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".
Logged

Pages: 1 2 3 [4] 5 6   Go Up
Jump to: