DHT lib outputs humidity and temperature values from the point in time of the reading before
I did some tests with a longer delay (20 min) between the DHT sensor readings, I noticed a significant offset between the DHT values and some DS18B20 temp sensors readings in parallel. After a rain shower I noticed also some implausible data values: It starts to rain but the next reading about 10 minutes after did report very "dry" values and only the next reading reports correct "wet" values.
So I did some tests with the sketch below. It reads values every 10 seconds but also a "control reading" 500 ms after the first reading. To trigger sensor changes I breathe upon the sensor to increase humidity, this is directly after last reading, around 9 seconds before next reading. So the sensor has enough time to "warm up" / catch the change.
I got the same results as in my 20 minutes setting (detailed output see below after the code): The reading is NOT the value for the current time but represents the situation of the last measurement. It seams with "DHT.readxx" is done a reading but this reading will be outputted not in the following Serial.print(DHT.temperature, 1); but in outputs just after that, so "one data point too late".
Is this the way the DHT works??--I did not see any comment about that?? Or is there an error in the lib or in my code (a modification of the lib example)?
#include
dht DHT;
#define DHT33_PIN 7
struct{
uint32_t total;
uint32_t ok;
uint32_t crc_error;
uint32_t time_out;
uint32_t connect;
uint32_t ack_l;
uint32_t ack_h;
uint32_t unknown;
} stat = { 0,0,0,0,0,0,0,0};
void setup(){
Serial.begin(9600);
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT_LIB_VERSION);
Serial.println();
Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)\tTime (us)");
}
void loop(){
readData();
Serial.print("\t");
Serial.print("delay 500");
Serial.println();
delay(500);
readData();
Serial.print("\t");
Serial.print("delay 10000");
Serial.println();
delay(10000);
Serial.println();
}
void readData(){
// READ DATA
Serial.print("DHT33, \t");
uint32_t start = micros();
int chk = DHT.read33(DHT33_PIN);
uint32_t stop = micros();
stat.total++;
switch (chk)
{
case DHTLIB_OK:
stat.ok++;
Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
stat.crc_error++;
Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
stat.time_out++;
Serial.print("Time out error,\t");
break;
default:
stat.unknown++;
Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA
Serial.print(DHT.humidity, 1);
Serial.print(",\t");
Serial.print(DHT.temperature, 1);
Serial.print(",\t");
Serial.print(stop - start);
if (stat.total % 20 == 0) {
Serial.println("\nTOT\tOK\tCRC\tTO\tUNK");
Serial.print(stat.total);
Serial.print("\t");
Serial.print(stat.ok);
Serial.print("\t");
Serial.print(stat.crc_error);
Serial.print("\t");
Serial.print(stat.time_out);
Serial.print("\t");
Serial.print(stat.connect);
Serial.print("\t");
Serial.print(stat.ack_l);
Serial.print("\t");
Serial.print(stat.ack_h);
Serial.print("\t");
Serial.print(stat.unknown);
Serial.println("\n");
}
}
What I get is this
LIBRARY VERSION: 0.1.13
Type, status, Humidity (%), Temperature (C) Time (us)
DHT33, OK, 28.4, 23.5, 5096 delay 500
DHT33, OK, 28.3, 23.5, 5240 delay 10000
DHT33, OK, 28.3, 23.5, 5232 delay 500
DHT33, Time out error, -999.0, -999.0, 6936 delay 10000
DHT33, OK, 28.3, 23.5, 5240 delay 500
DHT33, OK, 29.4, 23.5, 5160 delay 10000
=> directly after last reading breathing upon the sensor to
=> increse humidity, this is around 9 seconds before next reading
DHT33, OK, 29.4, 23.5, 5152 delay 500 ??? why is this value (29.4) so low ???
DHT33, OK, 74.4, 23.5, 5336 delay 10000 !!! 74.4 is now ok and "wet", but environment did not change in 500 ms !!!
DHT33, OK, 74.6, 23.5, 5416 delay 500
DHT33, OK, 69.4, 24.3, 5392 delay 10000
DHT33, OK, 68.8, 24.3, 5240 delay 500
DHT33, OK, 55.8, 24.2, 5152 delay 10000
DHT33, OK, 55.2, 24.2, 5112 delay 500
DHT33, OK, 42.0, 23.8, 5232 delay 10000
DHT33, OK, 41.6, 23.8, 5240 delay 500
DHT33, OK, 36.0, 23.8, 5288 delay 10000
=> again: directly after last reading breathing upon the sensor to
=> increse humidity, this is around 9 seconds before next reading
DHT33, OK, 35.9, 23.8, 5336 delay 500 ??? same behavior, why is this value (35.9) so low ???
DHT33, OK, 87.1, 23.9, 5432 delay 10000 !!! 87.1 is now ok and "wet", but environment did not change in the last 500 ms !!!
DHT33, OK, 87.9, 23.9, 5424 delay 500
DHT33, OK, 86.1, 24.2, 5288
TOT OK CRC TO UNK
20 19 0 1 0 0 0 0
delay 10000
DHT33, OK, 84.6, 24.2, 5248 delay 500
DHT33, OK, 48.6, 24.1, 5288 delay 10000
DHT33, OK, 47.5, 24.1, 5384 delay 500
DHT33, OK, 35.9, 24.1, 5296 delay 10000