How often you may ask
The DHT11's datasheet allows one reading a second. Adafruit's library allows one every two: inside that window it hands back the last result without touching the wire. So a DHT11 read in a tight loop shows a number up to two seconds old, and a failed read stays failed until the window ends.
Two floors, not one
Drag the slider to whatever delay your loop has in it.
There are two limits, one on top of the other, and only one of them is the sensor.
The DHT11's own floor. The datasheet (§6) says the sampling period "should be no less than 1 second". Ask sooner and the part either does not answer or answers before it has finished measuring.
The library's window. MIN_INTERVAL in Adafruit's DHT.cpp is 2000 ms,
for every sensor type. A read inside that window never reaches the wire: it
returns the last result, decoded from the bytes already in memory.
So through this library the DHT11 is read at most once every two seconds, though its datasheet allows one.
What the window buys you
It is the reason this works:
float humidity = dht.readHumidity();
float celsius = dht.readTemperature();Two calls, one exchange. The first does the whole 24 ms conversation and stores the five bytes; the second decodes the same bytes again. Without the window those two lines would be two readings, and the second would come far sooner than the one second the DHT11 needs.
What it costs you
It stores failures just as readily. A read that timed out is stored as a
failure, and every call for the next two seconds returns nan without
trying again. One glitch looks like a two-second outage.
And an old reading looks exactly like a new one. There is no timestamp and no flag. A display redrawn ten times a second shows the same number twenty times over.
Put it on a timer
Leave the loop free and read on a clock:
unsigned long last = 0;
void loop() {
if (millis() - last >= 2000) {
last = millis();
// one reading, here
}
// everything else keeps running
}Two seconds is the shortest interval that gets a fresh reading every time. For a room, ten or thirty seconds is better: nothing indoors moves a whole degree in two, and the sensor is slower to catch up than that anyway.
When it does not work
Either you are reading faster than every two seconds and seeing the library's stored value, or the room has not moved a whole degree. The DHT11 reports whole numbers, so a room drifting half a degree shows nothing. Breathe on it: if the reads are real, the humidity climbs within ten seconds or so.
No, together they cost one. The first call does the exchange and stores all five bytes; the second finds them less than two seconds old and decodes them again. That is why the two numbers always come from the same instant.
The library stores failures too. A read that timed out is remembered as failed, and every call for the next two seconds returns nan without trying again. One glitch looks like a two-second outage. Wait for the next real attempt.
readTemperature(false, true) and readHumidity(true) skip the stored value. The DHT11's datasheet allows one reading a second, so forcing at that pace is within specification. Faster than that gets you failed reads, not fresh ones.
The element has to warm up and take on water before the number moves, and where you put it matters more than how often you ask.
How slowly it catches up →Edit this page — content/books/dht11/how-often-you-may-ask.mdx
Questions about this product
See what other owners have asked, and read their solutions.
DHT11 Temperature and Humidity Sensor
Loading discussions…
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.