Possible library bug - DHT22 on ESP32.

76 views
Skip to first unread message

Andy Gadget

unread,
Jan 13, 2021, 6:20:56 AM1/13/21
to Annex WiFi RDS

I'm making a remote greenhouse and garden temperature and humidity monitor and have encountered a possible issue with the library used in the DHT22 command.

I'm using the AM2302 (basically the same as DHT22 but with internal pullup on the data line) and with a minimal test program this is returning NAN (not a number) for all three parameters in a manner which appears to be termperature related.  My immediate thought was a faulty sensor, but I have tried another with exactly the same result.  I have also tried this on different ESP32 pins and with an external pullup with exactly the same result.

Googling around, it does appear that there are known timing issues causing bad DHT22 reads with some Arduino libraries (which I think is what this driver is based on), although no posting I've seen mentions a termperature effect. 
Probably the best explanation of the issue and resolution is here : https://github.com/adafruit/DHT-sensor-library/issues/48

Does anyone have DHT22 / AM2302 devices working correctly at all temperatures?
(If so, I'm probably looking at a batch problem with the devices I'm using.)


DHT.SETUP 13,22
pause 2000

do
wlog  DHT.TEMP
pause 2200
loop




Bugs

unread,
Jan 13, 2021, 7:50:11 AM1/13/21
to Annex WiFi RDS
I hooked an AM2303 to an ESP32 and took some reading in the house the put it outside to go down to about 3 deg C.
Brought it back inside to ramp up again.
Results attached;  7 random(?) nans in 1075 readings - probably about what I would expect from DHT devices - not much faith in them...
Am2302.txt

Andy Gadget

unread,
Jan 13, 2021, 9:08:41 AM1/13/21
to Annex WiFi RDS
Thanks Bugs.

I was getting more extreme failures than that, OK from 18 up to 23 or so and then continuous NAN up to around 35 when it came good again. 
I am wondering whether my eBay specials came out the factory back door so I'll try getting a couple from a more trusted source.

A occasional NAN wouldn't be a problem as it's easy enough to detect and re-sample but a whole excluded block of temperatures is not much use.

Bugs

unread,
Jan 13, 2021, 10:00:39 AM1/13/21
to Annex WiFi RDS
Hi Andy,
There was no chance of getting 35C here (its snowing in Scotland) so stuffed it in a box and turned a fan heater on.
Took 185 readings from 21C to 35C and no nans logged.
Have you tried one with an ESP8266 or arduino?

Andy Gadget

unread,
Jan 13, 2021, 10:52:46 AM1/13/21
to Annex WiFi RDS
I'm in balmy Gloucestershire; 10.03C and wet here at the moment (by my fuel tank monitoring station :-)
My 35 degrees was a soldering iron a few inches below the sensor.

Why didn't I think of that?  
I've loaded an Arduino sketch using the same hardware and using the Adafruit integrated sensor library and the AM2302 is behaving perfectly - Chugging away next to me with no missed reads from 10C up to 40C. 
Something strange going on here.  Did you mean AM2303 in your last post or is it an AM2302?

Bugs

unread,
Jan 13, 2021, 11:03:46 AM1/13/21
to Annex WiFi RDS
Sorry - misread the markings on the case it is AM2302.
For completeness, my tests were done with ESP32_devkit_v4 and  Annex32 WiFi 1.38 beta RC5 .
Will watch this space...

Andy Gadget

unread,
Jan 13, 2021, 11:32:30 AM1/13/21
to Annex WiFi RDS
Thanks Bugs.

I was running 1.41b5 but have re-flashed with 1.38rc5 with exactly the same results as before.
Not too sure how to proceed from here so I'll probably order a couple of BME280s which I know are reliable.

Andy Gadget

unread,
Jan 19, 2021, 6:17:03 AM1/19/21
to Annex WiFi RDS

Just to close this off, I couldn't work out the reason why I was getting whole blocks of bad readings whereas Bugs only had the occasional failure so I took the east way out and replaced the DHT22 with a BME280 which works perfectly.
Reply all
Reply to author
Forward
0 new messages