How to detect DHT22 Sensor disconnect, using UNO, and DHT library

When my sensor has been disconnected, the value (a float) that gets sent via Serial.print(x) shows up in the serial monitor as "nan".
Where is this originated, and how can I intercept it and replace it with a different string like "Sensor x disconnected."?

Welcome to the forum.

Can you tell which sensor it is (if possible with a link to it) and which Arduino board you use, and which libraries you have used and show the sketch please.

Often a library can detect if a sensor has been disconnected. If not, then you can test for "nan" in the sketch or test for values that can't be true.

[ADDED]
I see now in the title that you have a Arduino Uno.
Could you scroll up and change the title, because the Serial response is not the problem. You could for example say "How to detect a disconnected NTC" if you have a NTC temperature sensor or "How does the OneWire library tell that a sensor is disconnected" if you would use the OneWire library.

Check with isnan().

But the sensor library begin() method probably returns a boolean for success or failure.

If you look at the library example sketches, you might find that they already do that.

Thanks everyone, the isnan() function did the trick!
Basically, I created a Boolean variable named skip1...n
h1 is the humidity data from sensor #1 (a float).

skip1 = isnan(h1);

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.