DHT22 sampling frequency

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
florinandrei
 
Posts: 27
Joined: Sun Mar 11, 2012 7:50 pm

DHT22 sampling frequency

Post by florinandrei »

The only real downside of this sensor is you can only get new data from it once every 2 seconds, so when using our library, sensor readings can be up to 2 seconds old.
It's not clear to me what this really means.

Can I issue a library call as often as I want, but I may get stale data a lot?

Or - is an error or an exception generated if I re-read the data too soon?

The difference is pretty important. I plan to build a controller for a scientific instrument, and I want to run the main loop as quickly as possible. Ideally, I'd like to simply issue a library call to read the temperature whenever I want, even if the data is "stale". But if that creates problems, then more code is needed to avoid tripping the sensor.

If I read only the temperature (not the humidity), can I read it twice as often? If I read both temperature and humidity, do I need to introduce a delay between each reading, no matter whether it's temp or humid?

User avatar
rob drizzle
 
Posts: 127
Joined: Wed May 04, 2011 6:14 pm

Re: DHT22 sampling frequency

Post by rob drizzle »

The reading is 2 seconds old when you read it.... The actual time it takes to get a reading from the DHT is about 250 ms and that reading is the temp and humidity from 2 seconds ago... So you can get about 4 readings a second.

For $10, it's not a bad sensor at all... I believe it about as accurate as my honeywell HIH4030 humidity sensor which costs about the same with no temp function.

I don't think it will make it any faster if you're only calling one function, but you can try.

No, there is no need for delays. Just get humid, get temp and your done.

User avatar
tromo
 
Posts: 23
Joined: Fri Jul 08, 2011 11:29 pm

Re: DHT22 sampling frequency

Post by tromo »

Both readTemperature() and readHumidity() call a private read() function which always delays for 250ms, but you will only get new values when more than 2s have passed since the last read. FWIW, it looks like the 250ms delay could be moved beyond the 2s test if you need fast polling between actual measurements.

User avatar
florinandrei
 
Posts: 27
Joined: Sun Mar 11, 2012 7:50 pm

Re: DHT22 sampling frequency

Post by florinandrei »

tromo wrote:Both readTemperature() and readHumidity() call a private read() function which always delays for 250ms, but you will only get new values when more than 2s have passed since the last read. FWIW, it looks like the 250ms delay could be moved beyond the 2s test if you need fast polling between actual measurements.
Are you saying I must leave it alone for 2s if I want to get an updated value at all?

User avatar
tromo
 
Posts: 23
Joined: Fri Jul 08, 2011 11:29 pm

Re: DHT22 sampling frequency

Post by tromo »

Are you saying I must leave it alone for 2s if I want to get an updated value at all?
No... The class checks to see when you last called readTemperature() or readHumidity() and, if it's been less than 2 seconds, it returns the previously read values, otherwise it will actually fetch new values from the sensor and return those. You can call it as often as you want.
...I plan to build a controller for a scientific instrument, and I want to run the main loop as quickly as possible. Ideally, I'd like to simply issue a library call to read the temperature whenever I want, even if the data is "stale"...
This is actually what I was addressing... When you call either of those functions, there is going to be a 250ms delay that I don't believe always needs to be there. If you really want speed, I would try moving those delays in DHT.cpp behind the test for 2 seconds passing. Otherwise, you're waiting a half a second (for both calls) just to get data you probably already have when you could be doing other things...

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”