Improve DHT read timings (#1901)

Make sure that the initial rising edge is properly detected even if
timing is somewhat off.

Set MCU start signal to 1ms for AM2302.
This commit is contained in:
Stefan Agner 2021-06-22 10:07:14 +02:00 committed by GitHub
parent 027e0de48e
commit 3dfff2930a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -94,11 +94,17 @@ bool HOT ICACHE_RAM_ATTR DHT::read_sensor_(float *temperature, float *humidity,
delayMicroseconds(40); delayMicroseconds(40);
} else if (this->model_ == DHT_MODEL_DHT22_TYPE2) { } else if (this->model_ == DHT_MODEL_DHT22_TYPE2) {
delayMicroseconds(2000); delayMicroseconds(2000);
} else if (this->model_ == DHT_MODEL_AM2302) {
delayMicroseconds(1000);
} else { } else {
delayMicroseconds(800); delayMicroseconds(800);
} }
this->pin_->pin_mode(INPUT_PULLUP); this->pin_->pin_mode(INPUT_PULLUP);
delayMicroseconds(40);
// Host pull up 20-40us then DHT response 80us
// Start waiting for initial rising edge at the center when we
// expect the DHT response (30us+40us)
delayMicroseconds(70);
uint8_t bit = 7; uint8_t bit = 7;
uint8_t byte = 0; uint8_t byte = 0;
@ -116,8 +122,6 @@ bool HOT ICACHE_RAM_ATTR DHT::read_sensor_(float *temperature, float *humidity,
break; break;
} }
} }
if (error_code != 0)
break;
start_time = micros(); start_time = micros();
uint32_t end_time = start_time; uint32_t end_time = start_time;
@ -132,8 +136,6 @@ bool HOT ICACHE_RAM_ATTR DHT::read_sensor_(float *temperature, float *humidity,
break; break;
} }
} }
if (error_code != 0)
break;
if (i < 0) if (i < 0)
continue; continue;