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?

1
  • 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
    Commented Dec 14, 2019 at 3:26

2 Answers 2

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.

1
  • Wow, I'll try this library. Thanks!
    – zhekaus
    Commented Feb 27, 2020 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);
2
  • 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
    Commented Dec 15, 2019 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. Commented Dec 15, 2019 at 21:22

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.