DHT11: First 2 readings after reset are always faulty

I have interfaced a DHT11 temp and humidity sensor with the Arduino. It works fine, but always the first 2 readings are garbage values. How can I filter out or omit the first two readings so that I get correct readings every time?

It is crucial that I get correct readings every time because my further calculations mess up if I get incorrect values!.

How can I filter out or omit the first two readings so that I get correct readings every time?

Throw away the first two readings. If you need more in-depth help post your code! And don’t forget to use code tags (that’s the </> button in the editor)!

void loop(){
   while(mySerial.available()>0){
     bool flag = checkiffalsesignal();
     if(flag == true) break;
     else{
      int chk,i;
      for(i=0;i<3;i++){
       chk = DHT.read11(DHT_pin);
       delay(2000);
      } 
      int T = DHT.temperature;
      int RH = DHT.humidity;
     int p =100*PMV(T,RH);
     senda(T,RH,p);
     delay(30000);           //wait for 1 minute before breaking out.
     break;
     }        
   }
}

The above is the code snippet for the loop. Note, I tried an intermediate loop and read the dht 3 times every time it was called. Then I took the 3rd reading. Still, same issue.

Post your code not a code snippet! The error may be in the includes or the setup() routine.

#include <dht.h>
#include <SoftwareSerial.h>
#define DHT_pin 5

SoftwareSerial mySerial(10,11);
dht DHT;

byte option;

void setup()
{
  // put your setup code here, to run once:
 mySerial.begin(9600);
}

void loop(){
   while(mySerial.available()>0){
     bool flag = checkiffalsesignal();
     if(flag == true) break;
     else{
      int chk,i;
      for(i=0;i<3;i++){
       chk = DHT.read11(DHT_pin);
       delay(2000);
      } 
      int T = DHT.temperature;
      int RH = DHT.humidity;
     int p =100*PMV(T,RH);
     senda(T,RH,p);
     delay(30000);           //wait for 1 minute before breaking out.
     break;
     }        
   }
}


bool checkiffalsesignal(){
  char c = mySerial.read();
  if(c=='a'){option = 1;return false;}
  else if(c=='b'){option = 2;return false;}
  else return true;

}

void senda(int T,int RH,int p){
  
  char str[8];
  str[0]='(';
  str[1]= T+45;         //characters will be 65 onwards
  str[2]= RH+25;
  str[3]= 48 + p/100;
  str[4]= 48 + (p%100)/10;
  str[5]= 48 + p%10;
  str[6]= ')';
  str[7]='\0';
  mySerial.print(str);
  delay(10);
}

float PMV(int T, int RH){
//int M = 70;
//float eta = 0.2;
//float f = 0.5;                  //standard summer clothing
//float Icl = 1.3725;
//float H = 4.65;               // assuming Vrev = 0.2 (standard)
//PMV variables

  float Tcl = 0.5075*T + 16.7006;
  float Psat = 190.1*T-1535;
  float Pa = 0.01*RH*Psat;
  float p = 0.0506*(- 0.00189*Pa + 0.098*T + 4.0477*Tcl - 77.0544);
    return p;
}

1. What type of device (HC05/HC06/HC12/NRF24L01/UNO-2) have you connected with your software UART Port?

2. How do you know that you are getting bad readings from dht11? You are not displaying the readings on the Serial Monitor.

3. Which dht11 Library have you used? There are so many libraries. Please provide a link to the laibrray that you have used for the codes of your post.

4. I have some problems to compile your codes in my UNO/MEGA?

  1. Uno~2
  2. If u see the code carefully, all the data is being sent through software serial to uno2 to be displayed on LCD.
  3. The standard dht library by Rob Tillaart.
  4. Sorry, I don't think there's anything in this code that should not compile.
  1. Sorry, I don't think there's anything in this code that should not compile.

It compiles for me too.

Why don't you read the sensor twice in the setup and throw these values away? I guess just waiting for some time in the setup would produce the same result.

Another idea: it seems you're also receiving data on SoftwareSerial, quite possibly WHILE you try to read the DHT11 sensor. That's guaranteed to mess up the DHT11 reading.

SoftwareSerial is known for blocking/disabling interrupts for a long time (try altSoftwareSerial instead - supposedly much better at that). Interrupts are needed for updating timers such as millis(). Timing is essential for reading the DHT11's signal - mess with the timing, even for a few milliseconds (one character on 9600 bps takes 10 ms), and the signal can't be read properly.