I came across the problem to handle two DHT22 devices.

I use the Adafruit DHT Sensor library.

Since my Arduino sketch works as a "relay" for Serial commands on a host, I tried to wire up and dynamically instantiate DHT22. With no luck.

I checked the source and found, that the DHT object only gets constructed before setup() as a constant in the examples, because the library is written that way.

I did a quick hack, which works, but seems ugly to me:


DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
-  _pin = pin;
-  _type = type;
-  #ifdef __AVR
-    _bit = digitalPinToBitMask(pin);
-    _port = digitalPinToPort(pin);
-  #endif
+  //_pin = pin;
+  //_type = type;
+  //#ifdef __AVR
+  //  _bit = digitalPinToBitMask(pin);
+  //  _port = digitalPinToPort(pin);
+  //#endif


-void DHT::begin(void) {
+void DHT::begin(uint8_t pin, uint8_t type, uint8_t count) {
+  _pin = pin;
+  _type = type;
+  #ifdef __AVR
+    _bit = digitalPinToBitMask(pin);
+    _port = digitalPinToPort(pin);
+  #endif


-   void begin(void);
+   void begin(uint8_t pin, uint8_t type, uint8_t count=6);

Basically, I moved a few lines from the constructor to the begin() method. The whole thing works. I can even rewire & change the data-pin dynamically while the Arduino runs.

Here are my questions:

  • Is the hacked away behavior a bug or feature?
  • Would my solution be good enough for a pull request to Adafruit? I would clean it up beforehand.
  • 2
    I would contact the owners of the Github repository and point them to your "hack" and get the thumbs up first. At least, you would be notifying them about the issue. Either they don't know about it, or are working on a fix – Greenonline Mar 26 '16 at 9:00
  • Upvote for editing my post. Nice diff feature btw. – ansi_lumen Mar 26 '16 at 9:30

There is nothing wrong with the library. It does not allow just one object instance.

What is wrong is your way of using it.

It, and many other libraries, specify the pin numbers and other (under normal usage) static data in the constructor. You want to have two objects then create two objects.

But it seems you want to change that static data. So you need to construct differently. Instead of statically declaring your objects you need to be dynamic. Use new and delete.


myDHT = new DHT(4,5);
// do something
// change pins
delete myDHT;
myDHT = new DHT(6,7);
  • Cool, I missed that, since I'm not familar with the underlying concepts of C, but your answer is what I was looking for. This is why I wrote, that my solution seems ugly to me. I keep the repo on github, to keep this post understandable. – ansi_lumen Mar 26 '16 at 9:28
  • 1
    The onky caveat with it is that excessive use on boards with very low memory could lead to heap fragmentation and stability issues. Shouldn't happen as long as you new immediately after deleteing. – Majenko Mar 26 '16 at 9:30
  • On second thought. Wouldn't it save mem on the arduino, if only one Object exists? I think this was the origin of the hack. Changing an objects attribute value should be much cheaper, than instanciating an object and destroying it again. – ansi_lumen Mar 26 '16 at 12:42
  • No memory difference. Just where in memory it resides. Maybe a minor difference in processing at the time of instantiation, but minimal. – Majenko Mar 26 '16 at 12:44

Changing the pins sounds like a reasonable requirement, and you shouldn't have to delete/new to achieve it.

I would leave the library alone (for backwards compatibility) except to add a changePins function which you may choose whether or not to call, to assign new pins to the class structure. (Effectively it would be your rewritten begin function with a different name).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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