# DHT11 sensor providing -999 outputs

I'm new here, trying to learn Arduino by hacking, that is plugging stuff in, looking for code and then trying to understand it bit by bit so I can move on to bigger and better projects.

My issue is that I am getting a periodic readout of -999.0 from a 3 pin DHT11 temperature and humidity sensor in between actual readouts. I can't find anything online explaining this but maybe I haven't looked hard enough. Attached is a screenshot containing both the code and serial monitor readout. Mechanical engineering background, so no electronic knowledge but feel free to get techy with your response.

if the image doesn't load this is the code;

#include <dht.h>

dht DHT;

#define DHT11_PIN 7

void setup(){
Serial.begin(9600);
}

void loop()
{
constrain (DHT.temperature, -25, 70);
constrain (DHT.humidity, 0, 90);

if((DHT.temperature || DHT.humidity) > -25)
{
Serial.print("Temperature, Humidity = ");
Serial.print(DHT.temperature);
Serial.print(", ");
Serial.println(DHT.humidity);
delay(1000);
}
}


Thanks in advance for help or direction to help.

-999 means that it didn't get a result from the DHT. You need to filter them out - which you are attempting to do, but wrongly.

if((DHT.temperature || DHT.humidity) > -25) {


is not doing what you thing it is.

If you start from the inner most brackets and "reduce", you get:

Is DHT.temperature or DHT.humidity?


That makes no real sense - except in C any number that isn't 0 is "true" and 0 is "false", so that really means:

Is DHT.temperature not 0 or DHT.humidity not 0?


And of course that will be "true" in 99.99999% of situations. So insert that into the if again:

if (true > -25) {


Is true more than -25? Well, the reverse of what I stated above is also true: "true" is 1 and "false" is 0. So is 1 greater than -25? Yes. It is.

Instead you should be testing each value separately:

if ((DHT.temperature > -25) && (DHT.humidity > -25)) {


Also your constraints are doing nothing. constrain() doesn't modify (limit) the variable you pass to it. Instead it takes the value in that variable, constrains it, and returns the new constrained value to you for you to assign to a new variable. So you should:

float temperature = constrain (DHT.temperature, -25, 70);


And then you can use the temperature variable instead of DHT.temperature, and you can guarantee that it will be between -25 and +70.

As I recall you're not supposed to read from a DHT11 or DHT22 more frequently than every 2 seconds. If you wait until 2 seconds pass before reading again you should avoid -999 readings.

Majenko's answer shows you how to check for and skip out-of-range readings, but only reading once every 2 seconds should let you avoid them entirely.

Note that you should also check for NaN (not a number) values returned. That means there was a read error, which does happen from time to time.

Try to use Pull up resistor. Connect the 4.7K resistor parallel to 5v (if your supply for DHT11 is 5v 4.7K is Good, for other voltage, you need another value of resistor) and Data pin.