dht: Fix sensor reading from DHT22 (#926)

* dht: Fix sensor reading from DHT22 (#239)

Looking at the datasheet for DHT22, it claims to need a delay of 18ms, not
800us. Change to use the same delay as DHT11. Tested working with NodeMCUv3
and DHT22 sensor with board with built-in resistor.

* dht: Add model DHT22_TYPE2 with 2ms delay instead of 0.8ms

The model DHT22_TYPE2 is exactly the same as DHT22, but uses a different
delay.

* dht: Fix bogus negative temperature reading on DHT22_TYPE2

The workaround for negative numbers associated with DHT22s can be skipped
on these sensors.
This commit is contained in:
Robin Smidsrød 2020-05-19 01:24:13 +02:00 committed by GitHub
parent 1d9ce2afc5
commit 7df72ddb96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 2 deletions

View file

@ -92,6 +92,8 @@ bool HOT ICACHE_RAM_ATTR DHT::read_sensor_(float *temperature, float *humidity,
delayMicroseconds(500); delayMicroseconds(500);
this->pin_->digital_write(true); this->pin_->digital_write(true);
delayMicroseconds(40); delayMicroseconds(40);
} else if (this->model_ == DHT_MODEL_DHT22_TYPE2) {
delayMicroseconds(2000);
} else { } else {
delayMicroseconds(800); delayMicroseconds(800);
} }
@ -208,7 +210,7 @@ bool HOT ICACHE_RAM_ATTR DHT::read_sensor_(float *temperature, float *humidity,
uint16_t raw_humidity = (uint16_t(data[0] & 0xFF) << 8) | (data[1] & 0xFF); uint16_t raw_humidity = (uint16_t(data[0] & 0xFF) << 8) | (data[1] & 0xFF);
uint16_t raw_temperature = (uint16_t(data[2] & 0xFF) << 8) | (data[3] & 0xFF); uint16_t raw_temperature = (uint16_t(data[2] & 0xFF) << 8) | (data[3] & 0xFF);
if ((raw_temperature & 0x8000) != 0) if (this->model_ != DHT_MODEL_DHT22_TYPE2 && (raw_temperature & 0x8000) != 0)
raw_temperature = ~(raw_temperature & 0x7FFF); raw_temperature = ~(raw_temperature & 0x7FFF);
if (raw_temperature == 1 && raw_humidity == 10) { if (raw_temperature == 1 && raw_humidity == 10) {

View file

@ -12,7 +12,8 @@ enum DHTModel {
DHT_MODEL_DHT22, DHT_MODEL_DHT22,
DHT_MODEL_AM2302, DHT_MODEL_AM2302,
DHT_MODEL_RHT03, DHT_MODEL_RHT03,
DHT_MODEL_SI7021 DHT_MODEL_SI7021,
DHT_MODEL_DHT22_TYPE2
}; };
/// Component for reading temperature/humidity measurements from DHT11/DHT22 sensors. /// Component for reading temperature/humidity measurements from DHT11/DHT22 sensors.
@ -28,6 +29,7 @@ class DHT : public PollingComponent {
* - DHT_MODEL_AM2302 * - DHT_MODEL_AM2302
* - DHT_MODEL_RHT03 * - DHT_MODEL_RHT03
* - DHT_MODEL_SI7021 * - DHT_MODEL_SI7021
* - DHT_MODEL_DHT22_TYPE2
* *
* @param model The DHT model. * @param model The DHT model.
*/ */

View file

@ -15,6 +15,7 @@ DHT_MODELS = {
'AM2302': DHTModel.DHT_MODEL_AM2302, 'AM2302': DHTModel.DHT_MODEL_AM2302,
'RHT03': DHTModel.DHT_MODEL_RHT03, 'RHT03': DHTModel.DHT_MODEL_RHT03,
'SI7021': DHTModel.DHT_MODEL_SI7021, 'SI7021': DHTModel.DHT_MODEL_SI7021,
'DHT22_TYPE2': DHTModel.DHT_MODEL_DHT22_TYPE2,
} }
DHT = dht_ns.class_('DHT', cg.PollingComponent) DHT = dht_ns.class_('DHT', cg.PollingComponent)