Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DHT22] Incorrect handling of negative temperature values #221

Open
bkormann80 opened this issue Dec 29, 2024 · 2 comments
Open

[DHT22] Incorrect handling of negative temperature values #221

bkormann80 opened this issue Dec 29, 2024 · 2 comments

Comments

@bkormann80
Copy link

The current release 1.4.6 of DHT-sensor-library supports various DHTxx sensors. I tested several DHT22 sensors on ESP8266 and Arduino Uno experiencing correct behavior for humidity and positive temperature values. As soon as the temperature drops below zero, incorrect negative values are determined by float DHT::readTemperature(bool S, bool force) function in DHT.cpp.

The data sheet of (DHT22) explains the temperature data extraction on page 3. The MCU receives 40 bits (5 * 8 bits) with the following structure:

  • 1st 16 bits: RH data (relative humidity)
  • 2nd 16 bits: T data (temperature)
  • last 8 bits: check sum

The current implementation follows the description of the data sheet, but returns incorrect data. A detailed inspection revealed, that temperature data is returned as a 16 bit signed integer value, hence no distinction between negative or positive values is needed. The float DHT::readTemperature(bool S, bool force) function in DHT.cpp must be adapted to:

float DHT::readTemperature(bool S, bool force) {
  float f = NAN;
  short s = 0;
...
    case DHT22:
      s = ((short) data[2]) << 8 | data[3];
      f = s * 0.1;
      if (S) {
        f = convertCtoF(f);
      }
      break;
...

The above mentioned patch solves the previous issue of incorrect negative temperature values. It seems like an inconsistency between documentation (data sheet) and sensor implementation or it might be a sensor variant to be supported by the DHT-sensor-library.

@dhalbert
Copy link

Thanks for the diagnosis! Would you be willing to submit a pull request?

@bkormann80
Copy link
Author

Sure. I already added the changes to a branch of the DHT-sensor-library repo locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants