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
zhekaus
  • 439
  • 1
  • 6
  • 14
  • 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 '20 at 09:37
  • @StarCat I use Arduino nano CH340. DHT library shipped with Arduino IDE. I tried pins D11 and A2. – zhekaus Feb 19 '20 at 09: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 '20 at 10:02
  • @StarCat Why I cannot read more than once? Can this be the source of the problem? I was supposed that I get wrong results, but it cannot lead the memory leakage. Yes, this library is by Adafruit, I was wrong. – zhekaus Feb 19 '20 at 10:29
  • I meant that your code has to wait at least 2 seconds after reading it, before reading the DHT again (technically: 0.5Hz or less). – StarCat Feb 19 '20 at 10:35
  • @StarCat , So, this is not the case. Please look an updated description, please. – zhekaus Feb 19 '20 at 10:46
  • Whatever you say, now I know that Adafruit DHT library is the worst library ever I met! It is occurred that simple reading call wastes a huge amount of memory! About 280 bytes or so. Shame, shame, shame! – zhekaus Feb 19 '20 at 14:43
  • @zhekaus Are you talking about RAM or the program memory? What do you mean by wasting memory? When you don't use the library, the compiler will optimize it out. When you then change your sketch to actually use it, the compiler will put the library (or only the needed parts of it) in, which results in a bigger sketch and partly in a bigger RAM usage. – chrisl Feb 19 '20 at 14:47
  • 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 '20 at 14:53
  • 1
    Adafruit DHT library requires about 390 byte of free RAM. Check if you have so. – evgard Feb 27 '20 at 16:56

1 Answers1

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

zhekaus
  • 439
  • 1
  • 6
  • 14
evgard
  • 71
  • 3