winlinvip / SimpleDHT

Simple, Stable and Fast Arduino Temp & Humidity Sensors for DHT11 and DHT22. http://learn.adafruit.com/dht
MIT License
144 stars 61 forks source link

[Improvement] [DHT11] It is possible to get tenths of temperature #51

Open martin-eden opened 2 years ago

martin-eden commented 2 years ago

For DHT11

While rewriting library for myself I've discovered that there are tenths of degrees Celsius transmitted in low nibble of second temperature byte. It is encoded in BCD, so hex values are in range 00..09.

martin-eden commented 2 years ago

I'm using this "DHT11" sensor from eBay: https://www.ebay.com/itm/221914097856

Despite specified range 0..50 °C it can measure negative temperatures and have temperature granularity 0.1 °C. Temperature sign is stored in bit 7 of second temperature byte.

image

DaleSchultz commented 1 year ago

are you getting more accuracy than DHT11.read2() ? (read2 takes floats)

martin-eden commented 1 month ago

As you figured it out in another issues,

read2() returns floats but temperature granularity is 1.0, not 0.1. https://github.com/winlinvip/SimpleDHT/blob/f938823ea9e0eaaa2deee98171097582c953aa33/SimpleDHT.cpp#L180

That's how I've done it:

  // 2nd temperature byte: tenths of temperature in low nibble in BCD.
  Temperature = (float) ((int16_t) Data[2] * 10 + (Data[3] & 0x0F)) / 10;
  // 2nd temperature byte: bit 7 is temperature sign.
  if (Data[3] & 0x80)
    Temperature = -Temperature;

That lines in my "scrapyard" unmaintained repo.