Iot: DHT22 Wrong temperature readings.

Created on 19 Feb 2020  路  7Comments  路  Source: dotnet/iot

The temperature output dropped to to 1掳C when the real temperature got above 26掳C.

Steps to reproduce

using var dht = new Dht22(26);
while (true)
 {
         // Try to read the temperature.
         var temp = dht.Temperature;
         if (!dht.IsLastReadSuccessful) continue;

          // Try to read the humidity.
          var humidity = dht.Humidity;
          if (!dht.IsLastReadSuccessful) continue;

          Console.WriteLine($"Temperature: {temp.Celsius:0.0} 掳C, 'Humidity: {humidity:0.0} %");

          break;
}

Expected behavior

Output: Temperature: 26.0 掳C, 'Humidity: 36.1 %

Actual behavior

Output: Temperature: 1.0 掳C, 'Humidity: 36.1 %

Versions used

Add following information:

  • Dotnet on the build machine: Version: 3.1.1 Commit: a1388f194c
  • Dotnet on the raspberry pi 3B: Version: 3.1.1 Commit: a1388f194c
  • Version of System.Device.Gpio package: 1.0.0
  • Version of Iot.Device.Bindings package: 1.0.0
area-device-bindings bug

Most helpful comment

@fomenkodmitry and @BramEsendam, you are right, there was a mistake. I found some time to dig into the code and found the problem.
Line 32 in the Dht222.cs file should be:

var temp = ((readBuff[2] & 0x7F) * 256 + readBuff[3]) * 0.1;

@joperezr, will make a PR asap.

All 7 comments

8a60d15c0ab975022315428a4098dcf6

I ran it with a small python script as well.
And it resulted in the correct values.

I was running a Dht11 sensor before this, and i never had any problems with it.
Except with the 2掳C inaccuracy. :P

I was running a Dht11 sensor before this, and i never had any problems with it.

So when you say ya were running it before, do you mean with python or with .net core? If you were doing it with .net core and it was working correctly, did you recompile or updated the nuget package to get a newer version?

cc: @Ellerbach @ZhangGaoxing as FYI.

@joperezr The Dht11 was almost running on the exact same code.
Just replace the

using var dht = new Dht22(26);

With

using var dht = new Dht11(26);

I just remebered that i updated to .net core 3.1 as well. So the Dht11 was running on .net core 3.0 and the Dht22 is running on .net core 3.1. You can see all the changes here.
I can try running the Dht11 on .net core 3.1 as well to see if the same thing will happen.

Sustained, similar bug.
You can see the code here: https://github.com/fomenkodmitry/NetCore-raspberrypi4-dht22
Platform: Rasberry pi 4 model b, net core 3.1, dht22

python3 ../../humidity.py 
Temp=26.3*C  Humidity=33.4%
Temp=26.3*C  Humidity=33.9%
Temp=26.3*C  Humidity=33.9%

```bash
dotnet run
Hello DHT!
Temperature: NaN掳C, Humidity: NaN%
Temperature: NaN掳C, Humidity: NaN%
Temperature: NaN掳C, Humidity: NaN%
Temperature: 1.7掳C, Humidity: 33.2%
Temperature: 1.7掳C, Humidity: 33.3%
````

@fomenkodmitry and @BramEsendam, you are right, there was a mistake. I found some time to dig into the code and found the problem.
Line 32 in the Dht222.cs file should be:

var temp = ((readBuff[2] & 0x7F) * 256 + readBuff[3]) * 0.1;

@joperezr, will make a PR asap.

I've merged that PR so hopefully the results will be fine now. I'll go ahead and close this issue assuming it does, but please feel free to reopen if that is not the case.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mpalenov picture mpalenov  路  32Comments

krwq picture krwq  路  34Comments

Ellerbach picture Ellerbach  路  26Comments

maloo picture maloo  路  18Comments

shaggygi picture shaggygi  路  22Comments