micropython / micropython/micropython-lib
dht22 calculates temperature incorrectly at negative values
Open
Beginner friendly
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 2.9k
- Forks
- 1.1k
- Avg merge
- 7d 6h
- Merged PRs (30d)
- 3
Description
I recently installed DHT22, and to my surprise, after a few days of frost, it showed a temperature of -3274.5.
The calculations performed by dht.py are consistent with the documentation, but they are incorrect.
Example and proposal for new calculation:
class DHTBase:
def xbuf(self, buf):
self.buf = buf
class DHT22(DHTBase):
def humidity(self):
return (self.buf[0] << 8 | self.buf[1]) * 0.1
def temperature(self):
t = (self.buf[2] & 0x7f) << 8 | self.buf[3]
if self.buf[2] & 0x80:
t = - t ^ 0x7fff
return t*0.1
def main():
# hum = 69, temp=-3274.5 = -2.2
cls = DHT22()
cls.xbuf([2, 183, 255, 233, 162])
temp = cls.temperature()
hum = cls.humidity()
print(f'temp={temp:.1f} hum={hum:.1f}')
main()
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in dht.py at the DHT22 temperature calculation and reproduce the supplied buffer example. Compare the current result with the proposed calculation, then verify that the example reports -2.2°C and that humidity remains 69.0%.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- embedded-iot
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 62/100