DHT22 - negative celsius temperatures

Hi,

i got a DHT22 in my weather station.

As tempatures are slowly going below freezing point (0 degrees celsius) at night here in germany, i discovered a problem with the sensor's Arduino Class.

When the temperature drops below 0°C (for exampe -1°C) the Class gives me exactly 0°C at all times.
I figured out the class got an update regarding to this issue 4 month ago, so i updated the class and re-uploaded my sketch to the board.

Now, when the temperature drops below 0°C, i get something like 3275.3°C from the class. (but everythink ok with positive temperatures)
As the variable inside the class is working with integers, i remembered the maximum integer value is 32767.
So real my temperature is probalby 32767 (max int. value) - 32753 (value the class gives me) = 14 = 1.4 = the real -1.4°C

It seems to me something causes the integer value to overflow, but i don't know how to solve this...

Ok, as i'm sending the measured values every 10 minutes to a webserver, i could just calculate the real negative temperature,
but that didn't solve the problem on the roots.

Any suggestions?

You can try my DHT library - Arduino Playground - DHTLib - if it meets your requirements. It should work for negative temperatures.
Interface is a bit different as it also supports the DHT11 which does not do negative values.

Hi Rob,

I know this is slightly outside the remits of this forum, but I hope you can help me.

I have a Raspberry Pi and followed the tutorial on this page to connect a DHT22 to it:

I have the same issue as Kduin in that I get 0 instead of negative temperatures.

I have looked at your libraries but can't seem able to apply them to my setup.

I'd be grateful if you could have a quick look in case it's something simple such as some unsigned type of variable.

Thanks
Laurent

Adafruit_DHT.c (3.05 KB)

DHT.h (719 Bytes)

Adafruit_DHT (29.7 KB)

Adafruit_DHT_googledocs.ex.py (1.94 KB)

A quick look shows that the algorithm should work, I see no real failure there wrt sub zero values.
Above 0 it works - you did not mention failure there.

To mimic the code with the one in my library you should change

	f = (data[2] & 0x7F)* 256 + data[3];
        f /= 10.0;
        if (data[2] & 0x80)  f *= -1;
	printf("Temp =  %.1f *C, Hum = %.1f \%\n", f, h);

to

if (data[2] & 0x80)
{
  int t = (data[2] & 0x7F)* 256 + data[3];
  printf("t = %d", t);
  f = t * -0.1;
}
else
{
  int t = data[2] * 256 + data[3];
  printf("t = %d", t);
  f = t * 0.1;
}
printf("Temp =  %.1f *C, Hum = %.1f \%\n", f, h);

can you post the output the application generates?

i get something like 3275.3°C

You are probably storing the value returned from the class into an unsigned integer.
Post your code (using code tags).

Pete

The problem looks like the code thats reading the sensor isnt interpreting the Sensors sign bit.
The DHT22 sensor uses the MSB of data byte 3 to indicate a positive or negative temperature.
Its 0 for positive and 1 for negative temps, so if the sign bit isnt read properly, you get very high positive temps.

Mauried is right. The library you are using doesn't handle negative temperatures correctly. Try the library by Adafruit which appears to get things right: GitHub - adafruit/DHT-sensor-library: Arduino library for DHT11, DHT22, etc Temperature & Humidity Sensors

Pete

I have the same problem. Today temperature goes under 0°C and my weather station send bad values to webserver:

3276.6
3276.5
3276.4
3276.7

Temperatures >0°C is good.

I used this DHT22 library. My Arduino code.
I have ATmega8 without bootloader and it is located in very remote place . So is very difficult to reflash firmware in ATmega8 for me :frowning:

Is there any way to calculate good temperature from number like "3276.5"?

EDIT:
If i understand you, for "3276.5" i must do:
( 32767 / (3276.5 * 10) / 10) = -0.1°C
What about lower temperatures? For example -20.7°C? Is still possible to calculate good temp?

That number looks suspiously like the largest signed 16 bit integer you can use, divided by ten. That should indicate where your problem is.

Simple solution for you guys is to move somewhere warmer.....

I don't think the library does the conversion correctly.
This code:

    // Below zero, non standard way of encoding negative numbers!
    // Convert to native negative format.
    _lastTemperature = -currentTemperature & 0x7FFF;

doesn't look quite right because I think the unary minus will be performed before the &.
Try changing it to this:

    // Below zero, non standard way of encoding negative numbers!
    // Convert to native negative format.
    _lastTemperature = -(currentTemperature & 0x7FFF);

Aside: just because signed-magnitude isn't common doesn't mean that two's complement is "standard".

Pete

solving it the dirty way: (temperatures on Earth won't reach above 1000°C and your DHT22 would melt long before)

//add variable

float temp2;

//within the loop

temp = myDHT22.getTemperatureC();

//add

if (temp > 1000){
temp2 = (temp - 3276.7);
}
else{
temp2 = temp;
}

//then replace Serial.print(temp); with Serial.print(temp2)

  1. That's a really dumb way to fix a bug.
  2. Just in case you haven't noticed, the thread was over three years old before you replied to it and the forum told you it was an old thread but you posted anyway.

Pete

Please , can anyone give me a library for DHT22 that permit me to read positive and negative temperature correctly ?