When every reading is nan
The DHT11 cannot report a fault. Every problem arrives as one of two things: nan, or a number that is wrong. Five symptoms, and what each one narrows the cause down to before you start pulling wires out.
Start from what it prints
- Count the pins from the square pad: GND, VCC, NC, DATA. The third one is connected to nothing.
- Is VCC on a rail that is actually powered? A sensor with no supply is silent, not noisy.
- Is the pin number in the sketch the pin the wire is in?
There is nothing else to go on. The DHT11 has no way to say "I am here but you asked too soon". Either forty valid bits arrive or they do not, and everything else is inference from what the sketch prints.
The order to check in
First, the sensor type. It is one line in the sketch, it is never
detected, and it causes the two commonest symptoms: silence, when the DHT11
is declared as a DHT22 and never wakes, and numbers in the hundreds, when it
wakes anyway and is decoded as one. The TK38 is DHT11.
Then the pin count. Count from the square pad: GND, VCC, NC, DATA. The third pin is connected to nothing, so wiring by position and being one out leaves DATA on a dead pad, which looks exactly like a dead sensor.
Then the supply. Is VCC on a rail that is powered, and is it the voltage your pins expect? A DHT11 with no supply is silent. One on the wrong supply usually works, and quietly stresses the input pin.
Then the timing. A number that never changes is the library's two-second window or a room that has not moved a whole degree, not a stuck sensor.
Last, the placement. A reading two degrees high on correct wiring is not a fault: the sensor is telling you how warm your board is.
What nan is
readTemperature() and readHumidity() return NAN rather than an error
code. It prints as nan, and it compares false against everything, itself
included:
if (celsius == NAN) // never true, ever
if (isnan(celsius)) // this oneThat is the only check that works, and it is why every sketch in this book
has it. A nan printed straight into a graph or a web page is how a wiring
fault gets taken for a sensor reading zero.
When it fails now and then
An occasional failure is the protocol, not your build. The forty bits are measured by counting loop turns with interrupts off, and anything that disturbs the count, such as a slow edge on a long wire or a busy radio, turns one bit over and the checksum discards the frame.
Retry rather than report, and respect the DHT11's one-second floor between tries:
float t = dht.readTemperature();
for (int i = 0; i < 2 && isnan(t); i++) {
delay(1000); // the DHT11's own minimum
t = dht.readTemperature(false, true); // force past the stored value
}Two retries cover ordinary bad luck. If it needs them every time, look at the wiring and the placement rather than the code.
When it does not work
Yes. The bits are timed by counting, so anything that holds up the processor for a few microseconds during the forty bits spoils one, and the checksum throws the frame away. Check every result and skip the bad ones. A single nan is not a fault.
The pull-up has to charge the wire before the line can rise, and a long run of loose wire takes longer to charge. Shorten it first. The datasheet recommends good shielded cable for distance, and a 5 kΩ pull-up rather than the board's 10 kΩ for runs up to 20 m.
The library switches interrupts off for the forty bits, but on an ESP32 the radio's own work can still disturb the count, and a sagging 3V3 rail does not help. Take the reading between transmissions and retry rather than report the first failure.
Check DHT_TYPE in the sketch. Declared as a DHT22, the DHT11 gets a 1.1 ms start pulse instead of 20 ms and often never wakes, so the symptom is total silence on wiring that is correct.
From software you mostly cannot: both give the same silence. Swap in another TK38 on the same three wires and the same sketch. If it answers, the first block is at fault; if it does not, the wires or the pin are.
When whole degrees, 0 °C and ±5 % are not enough: what the TK39's DHT22 changes.
DHT11 or DHT22 →Edit this page — content/books/dht11/when-every-reading-is-nan.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.