RobTillaart / DHTNew

Arduino library for DHT11 and DHT22 with automatic sensor recognition
MIT License
96 stars 15 forks source link

Fix negative temperature below -25.5°C #60

Closed RobTillaart closed 3 years ago

RobTillaart commented 3 years ago

As the DHT has an operational range from -40°C (-40°F) it fails for the lowest temperatures.

Bug is in line 202

      if(_bits[2] == 0x80)
      {
        _temperature = -_temperature;
      }

works well down to -25.5 C but not below that, as the _bits[2] will be 0x81 for such low temperatures. For most applications this is not an issue, but it is still a bug.

Code proposal

      if(_bits[2] & 0x80  != 0)
      {
        _temperature = -_temperature;
      }

Unfortunately I cannot test down to -40°C (my fridge goes to -20 or so)

RobTillaart commented 3 years ago

if( (_bits[2] & 0x80) == 0x80 ) is more explicit.