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.
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?
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.