DHT11/Limits and a check/07. How often you may ask
Limits and a check · 07 of 9

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.

How often you may ask
every 500 ms
delay() in your loop500 ms
Real readings
30/min
Calls that did nothing
18
Oldest value shown
1.5 s
Asking faster does not get you a newer number. Inside 2 seconds the library never touches the wire at all — it hands back whatever it got last time, including the last failure. Your loop spins, the sensor sleeps, and the reading on screen can be up to 1.5 seconds old with nothing saying so.

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

The reading never changes

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.

Don't readHumidity and readTemperature each cost a reading?

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.

Once it fails it keeps failing for a while

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.

Can I force a reading once a second?

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.

Where this goes next

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

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

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.

Browse Modules and blocks on the forum