← index #611Issue #3593
Off-topic · high · value 0.395
QUERY · ISSUE

Problem with dht library

openby gandipgopened 2023-02-12updated 2023-03-03

Hello
Problem with dht library not working properly in subzero temperatures with dht22 module.
reports (-3264.4°C)
it is written here about a domestic problem https://forums.raspberrypi.com/viewtopic.php?t=296097

2 comments
Keelando · 2023-02-28

Indeed! I'm not well-versed enough to suggest a proper solution at this time but I've just been adding the offset value corresponding to -0.1C and then multiplying by -1

dpgeorge · 2023-03-03

I tested the dht driver with a DHT22 and it works OK for me at temperatures less than 0 Celsius.

The dht driver here assumes that the top bit of the returned temperature value is a sign bit. But if you are getting values like -3264.4 then it could be that your DHT22 returns a 2's complement signed value.

After you do a reading can you print out the buffer, like this:

dht.measure()
print(dht.temperature(), dht.buf)

That will help narrow down the problem.

If it is 2's complement then you have a non-standard DHT22 module and you'll need to use conversion like this:

def dht_temp(dht):
    value = dht.buf[2] << 8 | dht.buf[3]
    if value & 0x8000:
        value -= 0x10000
    return value * 0.1
CANDIDATE · ISSUE

DHT22 no result

closedby ds2k5opened 2018-02-03updated 2018-02-05

Hello,
try to get Temprature from a DHT22

Board: NodeMcu V3, ESP8266
Board DHT22
GND --> right (4)
V3 --> left (1)
PIN4 --> Signal (2)

found this CODE but did not get a result:

import dht
import machine
import time

print("Starting DHT22.")
d = dht.DHT22(machine.Pin(4))

while True:
    print("Measuring.")
    
    retry = 0
    while retry < 3:
        try:
            d.measure()
            break
        except:
            retry = retry + 1
            print(".", end = "")

    print("")

    if retry < 3:
        print("Temperature: %3.1f °C" % d.temperature())
        print("   Humidity: %3.1f %% RH" % d.humidity())

    time.sleep(5)

Starting DHT22.                                                                 
Measuring.                                                                      
...                                                                             
Measuring.                                                                      
...         

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied