1

I’ve got a quite weird behavior of DHT library.

I’ve got DHT22 sensor. If it’s signal cable is not connected, reading the sensor causes a problem: Arduino reboots or hangs in unpredictable places.

If I connect the signal cable again, the problem vanishes.

Is this normal behavior or some sort of bug? I think unsuccessful reading affects some part of Memory.

PS. The project is quite big and I can’t share the code here.

It happens that I found an evidence that dht object influences foreign memory. Look at this code:

DV("pClock->isInited 3", pClock->isInited); // prints 1
dht1.readHumidity();
DV("pClock->isInited 4", pClock->isInited); // print 0 !!!!
// nothing more can be called between these lines.

// DV is a macro which prints label and value.
// Both objects created before this.

DHT dht1(11, DHT22);
…
dht1.begin();
…
pClock = new ZhClock(); // uses RTC_DS1307
pClock->init(&rtc); // rtc is RTC_DS1307
10
  • It's not normal behaviour. What hardware are you using, what pins have you connected the DHT22 to, and what DHT library are you using?
    – StarCat
    Feb 19, 2020 at 9:37
  • @StarCat I use Arduino nano CH340. DHT library shipped with Arduino IDE. I tried pins D11 and A2.
    – zhekaus
    Feb 19, 2020 at 9:45
  • Is that the DHT library from Adafruit (installed through the library manager)? I don't think there is a DHT library that comes standard with the Arduino IDE. A2 and D11 should work fine. But what do you expect it to do when you disconnect the signal cable? The library should return an error (NaN values returned by temperature and humidity) when the DHT is not connected. How do you handle these errors? If you continue using the NaN values returned by the library, you will mess up all subsequent calculations. Also remember you cannot read from a DHT22 more than once every 2 seconds.
    – StarCat
    Feb 19, 2020 at 10:02
  • 1
    @chrisl , I am talking about RAM, obviously. Shortage of RAM kills the device. Look into dht.cpp, DHT::read method, and you'll see it yourself. I am sure, that, for instance , uint32_t cycles[80]; says quite something to you ;-).
    – zhekaus
    Feb 19, 2020 at 14:53
  • 1
    Adafruit DHT library requires about 390 byte of free RAM. Check if you have so.
    – evgard
    Feb 27, 2020 at 16:56

1 Answer 1

1

This problem is usual if you are in shortage of RAM. Adafruit's libraries wastes lots of memory. You should be very careful to run their stuff if you are in shortage of memory.

I suggest you to try another library which quite old, but uses much lesser memory to read DHT sensor’s data: https://github.com/markruys/arduino-DHT

1
  • Yep! That's solved my problem!
    – zhekaus
    Feb 27, 2020 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.