From 3dfff2930a28cc061da8f6c852b46dc931675c96 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Tue, 22 Jun 2021 10:07:14 +0200 Subject: [PATCH] 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. --- esphome/components/dht/dht.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/esphome/components/dht/dht.cpp b/esphome/components/dht/dht.cpp index fd51a976b7..4a5c418e0a 100644 --- a/esphome/components/dht/dht.cpp +++ b/esphome/components/dht/dht.cpp @@ -94,11 +94,17 @@ bool HOT ICACHE_RAM_ATTR DHT::read_sensor_(float *temperature, float *humidity, delayMicroseconds(40); } else if (this->model_ == DHT_MODEL_DHT22_TYPE2) { delayMicroseconds(2000); + } else if (this->model_ == DHT_MODEL_AM2302) { + delayMicroseconds(1000); } else { delayMicroseconds(800); } 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 byte = 0; @@ -116,8 +122,6 @@ bool HOT ICACHE_RAM_ATTR DHT::read_sensor_(float *temperature, float *humidity, break; } } - if (error_code != 0) - break; start_time = micros(); uint32_t end_time = start_time; @@ -132,8 +136,6 @@ bool HOT ICACHE_RAM_ATTR DHT::read_sensor_(float *temperature, float *humidity, break; } } - if (error_code != 0) - break; if (i < 0) continue;