question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DHT22 reports wrong negative temperatures

See original GitHub issue

First off: it sounds like the bug which has been reported here: https://github.com/adafruit/Adafruit_CircuitPython_DHT/issues/9 but it happend to me with the latest version as well.

I have recently migrated from the old Adafruit lib to this one, because I hoped that the bug which occurred in the old lib as well has been fixed in the new one. Unfortunately it seems as if there is an issue with the new lib in my setup as well.

sudo pip3 install adafruit-circuitpython-dht
sudo apt-get install libgpiod2

Unfortunately I am not really familiar which informtation I might need to provide for this bug report nor I have no clue what I could try next.

Therefore: any help is appreciated 😃

Side note: the sensor I have in use does not require a pull up resistor - and I assume that this is correct - as said: all values except negative temperatures are pretty much accurate.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:20

github_iconTop GitHub Comments

1reaction
apra00commented, Dec 15, 2022

Hi everyone!

I had this issue too. I just did some try&error testing and found out that with my python script I can add the wrong value at the actual -0.1˚C to the wrong number (ex: temperature_c+3276.6) and then multiply that with -1 to get the actual negative temperature.

For this you have to get your reported value at -0.1˚C and write it down. Then go into your code and add it there as the correction value.

I don’t have an other DHT22 for testing but I’m not sure if every one has the same reported value at -0.1˚C. So be aware of that.

Tested it for a week and compared it the an actual thermometer and it worked for me.

There is my code:

import time
import board
import adafruit_dht

dhtDevice = adafruit_dht.DHT22(board.D6)
dhtDevice._trig_wait = 1500

temperature_c = dhtDevice.temperature
humidity = dhtDevice.humidity

try:
    if temperature_c < -1:
        temperature_c = round((temperature_c+3276.6)*(-1), 2)
    if (humidity != None) and (temperature_c != None) and (100 > temperature_c > -100):
        # do something with the correct temperature
        print(temperature_c, humidity)
    else:
        print("None value")
        
except RuntimeError as error:
            # Errors happen fairly often, DHT's are hard to read, just keep going
            print(error.args[0])
except Exception as error:
            dhtDevice.exit()
            raise error

This gives the correct output: -0.1 99.9 Yes, there is fog outside 😃

0reactions
dg8ygzcommented, Dec 15, 2022

It seems some DHT22 sensors act different at negative temperatures; my ones returns a twos compliment value. I adapt the adafruit_dht.py to my needs (maybe a solution for further versions?!)

here’s my patch for the actual 3.7.8

246,249c246,247
<                 new_temperature = (((buf[2] & 0x7F) << 8) | buf[3]) / 10
<                 # set sign
<                 if buf[2] & 0x80:
<                     new_temperature = -new_temperature
---
>                 new_temperature = int.from_bytes(buf[2:4], byteorder="big", signed=True) / 10
>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Reading Negative Temperatures with DHT22 - Arduino Forum
The problem is that it does not report negative temperatures correctly. The temperature will either be a number below -3000 or it will...
Read more >
DHT22 sensor reading code interpretes negative values ...
When the temperature drops below 0 °C, this code returns inadequate values in the range from -3200 to -3300 (most often the values...
Read more >
DHT22 checksum error for negative temperatures
The code works okay for temperatures greater than 0C. However, when the temperature drops below 0C, it returns a checksum error. The datasheet ......
Read more >
DHT22 sensors reporting bad values in ESPHome
I have just recently had to make a new sensor to subtract 2.3 from humidity and 1.6 from Temperature. to have this sensor...
Read more >
Below 0°C, DHT22 reads wild low temperatures (-3271.4°C)
A quick search revealed that some of the DHT22 drivers do have a problem with negative temperatures; partly because the returned data format...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found