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

Problem: DHT22 does not send data in sign-magnitude format #182

Open
argltuc opened this issue Feb 12, 2021 · 3 comments
Open

Problem: DHT22 does not send data in sign-magnitude format #182

argltuc opened this issue Feb 12, 2021 · 3 comments

Comments

@argltuc
Copy link

argltuc commented Feb 12, 2021

  • Arduino board 1: ESP8266 / NodeMCU 1.0 (ESP12-E Module)
  • Arduino board 2: ESP-WROOM-32 / ESP32 DEVKIT V1
  • Arduino IDE version (found in Arduino -> About Arduino menu): 1.8.13
  • installed DHT-sensor-library version: 1.4.1 / installed by Arduino IDE Lib manager

This problem is directly connected to #177

After some more research on negative temperatures with DHT22 and ESP8266 / ESP32, i figure out, that my DHT22 sensor does not send data as expected.

This library assumed, sensor temperature data is in sign-magnitude format. This is also documented here: https://cdn-shop.adafruit.com/datasheets/Digital+humidity+and+temperature+sensor+AM2302.pdf

But this DHT22 which I have available for testing, seems to send data in two's complement format. With one exception:
During transition from positive to negative values, there is one point with sign-magnitude.
Going from 0.1°C (0x0001) -> 0°C (0x0000) -> -0°C (0x8000) -> -0.1°C (0xFFFF)

I have also a log of this, where I add a hex print of data[2]/data[3] at end of readTemperature():

22:07:01.473 -> 0003    0.3
22:07:01.473 -> Temperature: 0.30C
22:07:03.485 -> 8000    -3276.8
22:07:03.485 -> Temperature: -3276.80C
22:07:05.500 -> FFFD    -0.3
22:07:05.500 -> Temperature: -0.30C
@dl2sba
Copy link

dl2sba commented Feb 17, 2021

Also my finding - I have fixed this code in DHT.cpp.

   case DHT22:
   case DHT21:
     int16_t d = ((int16_t)data[2]) << 8 | data[3];
     //	msb set == negativ
     if (d & 0x8000) {
   	  d &= 0x7fff;
   	  //	0x8000 == 0x0000 == 0)
   	  if ( d == 0 ) d = 0x7fff;
   	  d = 0x7fff - d;
   	  d *= -1;
     }
     f = d * 0.1f;

     if (S) {
       f = convertCtoF(f);
     }
     break;

@JavanXD
Copy link

JavanXD commented Mar 6, 2021

Having the same problem on Raspberry Pi using the latest Adafruit_DHT package.

@RamitArko
Copy link

I am also facing the same problem using Raspberry Pi 4

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

4 participants