DHT11 returns NaN on Arduino: keep missing readings separate
Distinguish an invalid humidity sample from zero, then check the configured sensor family, wiring, and read interval.
Preserve failed reads as failures
The Adafruit DHT library returns NaN when a request fails. Printing zero instead can create a false valid measurement in a chart. Test with isnan() before logging a number. DHT11 and DHT22 use different decode selections, so compare the part marking and constructor argument.
Check the interval, then test one sensor
Use a two-second delay for this simple diagnostic and start with the D2 signal only, with a shared ground and the supply and data pull-up appropriate to your sensor or module. Verify the physical pin order against its datasheet. A board that draws power and a sketch that compiles can still have a swapped or floating data lead.
Open the exact Uno example
Expect either a numeric humidity sample or the explicit invalid-sample line. The Uno compile verifies the pinned API. Hardware measurements require the sensor.
#include <DHT.h>
DHT sensor(2, DHT11);
void setup() {
Serial.begin(9600);
sensor.begin();
}
void loop() {
delay(2000);
const float humidity = sensor.readHumidity();
if (isnan(humidity)) Serial.println("invalid sample");
else Serial.println(humidity, 1);
}Verified with [email protected] on Uno R3 (2026-12-05 UTC). This check did not use physical hardware.