When every reading is nan
This protocol has no error reporting at all, so every fault arrives as the same thing: nan, or a number that is simply wrong. Five symptoms, and what each one narrows the cause down to before you start pulling wires out.
Start from what it is printing
- 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 sensor cannot report a fault — it has no way to say "I am here but I am cold" or "you asked too soon". Either forty valid bits arrive or they do not, and everything else is inference from what your sketch prints.
The order to check in
First, the sensor type. It is one word in the sketch, it is not detectable,
and it causes both of the two most common symptoms: total silence when a DHT11
is declared as a DHT22, and absurd numbers when the decoders are crossed. Blue
cage is DHT11, white cage is DHT22.
Then the pin count. Count from the square pad: GND, VCC, NC, DATA. The third pin is connected to nothing on the board, so wiring by position and being one place out gives you a data line on a dead pad — which looks exactly like a dead sensor.
Then the supply. Is VCC on a rail that is actually powered, and is that rail the same voltage as your pins expect? A sensor with no supply is silent, and a sensor 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 cache, not a stuck sensor.
Last, the placement. A reading two degrees high on wiring that is otherwise perfect is not a fault at all — the sensor is telling you accurately how warm your board is.
What "nan" actually is
readTemperature() and readHumidity() return NAN rather than throwing or
returning a flag. It prints as nan, and it compares false against everything
including itself, so:
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 handbook
has it. Printing a bare nan into a graph or a web page is how a wiring fault
gets mistaken for a sensor that reads zero.
When it is genuinely intermittent
One failure in a hundred is the protocol, not your build. The forty bits are measured by counting loop iterations with interrupts off, and anything that stalls that count — a long wire making the edges slow, a radio, another library's interrupt — turns one bit over and the checksum discards the frame.
Retry rather than report:
float t = NAN;
for (int i = 0; i < 3 && isnan(t); i++) {
t = dht.readTemperature(false, true); // force past the cache
if (isnan(t)) delay(50);
}Three attempts covers ordinary jitter. If it takes three attempts every time, the problem is the wiring or the placement rather than luck.
When it does not work
Yes. The bits are timed by counting rather than by a clock, so anything that steals the processor for a few microseconds during the forty bits corrupts one and the checksum throws the frame away. Check the return value and skip the bad ones; do not treat a single nan as a fault.
The pull-up has to charge the capacitance of the wire before the line can rise, and a long unshielded run is a lot of capacitance. The datasheet asks for shielded cable past a few metres and caps the useful distance around twenty. Shorten it first, then shield it.
The library switches interrupts off for the forty bits, but on an ESP32 the other core and the radio's own tasks are still running and can stall the counting loop. Take the reading between transmissions rather than during one, and retry rather than reporting the first failure.
Check the sensor type in the sketch. A DHT11 declared as a DHT22 gets a 1.1 ms start pulse instead of 20 ms and frequently never wakes, so the symptom is total silence on wiring that is perfectly correct.
You largely cannot, from software — both produce identical silence. Move the known-good sketch to one of the other five boards in the box. If the second board works on the same three wires, the first one is dead; if it does not, the wires are.
A live page you can open from your phone, off an ESP32, with no backend and no port forwarding.
Put the readings on a dashboard →Edit this page — content/books/dht22/when-every-reading-is-nan.mdx
Questions about this product
See what other owners have asked, and read their solutions.
This page covers several products. Choose yours to see the right 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.