0

I made a simple temperature controller on DHT11. I want it to work with DHT22 as well having the same code.

However, I can’t find a way how to get known what type of sensor is installed. Is there a way?

  • internet search reveals that DHT11 needs 1 second between readings ... DHT22 needs 2 seconds between readings .... run tests .... see what happens if you try to read each of the sensors at 1.2 second intervals – jsotola Dec 14 '19 at 3:26
3

You can use another library for DHT: https://github.com/markruys/arduino-DHT Although, the library is quite old, it works fine and has auto-detection feature.

| improve this answer | |
  • Wow, I'll try this library. Thanks! – zhekaus Feb 27 at 12:43
1

I don't think that is possible.

What you should do is pass the type (DHT11 or DHT12) to the code, store it and change calculations (or functions) accordingly.

If you use a class, you can pass it as argument of the constructor, so the user of the library only need to set it up once (in the constructor where it belongs). Make it an enumeration, copy it into a class variable and all methods inside your class can use the type.

Something like (not checked):

MyDht.hpp:

class MyDht
{
public:
    enum EType { DHT11, DHT12 };

    public MyDht(EType type);

private:
    EType _type;
}

Dht.cpp:

MyDht::MyDht(EType type)
{
    _type = type;
}

And you create the class by:

MyDht sensor1(MyDht::DHT11);
MyDht sensor2(Mydht::DHT12);
| improve this answer | |
  • 2
    Actually it is possible. If DHT type is wrong, I get empty results. Then I try another type and the results are correct. – zhekaus Dec 15 '19 at 21:18
  • Ok, than use your method in case you don't know, and use mine if you want to do it programmatically... Actually, you could combine both solutions by creating (dynamically) the correct type after your 'one time check', Create a pointer to MyDht, and create either a DHT11 or DHT12 instance. – Michel Keijzers Dec 15 '19 at 21:22

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.