RobTillaart / DHTstable

Arduino Library for the DHT temperature and humidity sensor.
MIT License
17 stars 4 forks source link

How to use two or more DHT11 sensors with DHTStable library #15

Closed awincuk closed 2 years ago

awincuk commented 2 years ago

Hello!

I would like to use this library to read data from two independent DHT11 sensors. Is it possible? I tried to define DATA pins as: DHTStable DHT1(DATA_PIN); DHTStable DHT2(DATA_PIN); Similarly like in this library: https://robojax.com/how-use-two-or-more-dht11-or-dht22-temperature-and-humidity-sensor But it does not work.

Do you have any idea how to do it?

Thank you for your work. This is a great and stable library indeed!

RobTillaart commented 2 years ago

The DHTStable library uses one DHTStable object for multiple DHT sensors. This was done (IIRC) to minimize the footprint.

You can create two objects however you must apply the PIN with every read. I wrote a minimalistic version below (not tested as I have no HW setup, it compiles for UNO) it uses two different DHT objects and two different pins.

//
//    FILE: dht11_two.ino
//  AUTHOR: Rob Tillaart
// PURPOSE: DHT library test sketch for DHT11 && Arduino
//     URL: https://github.com/RobTillaart/DHTstable

#include "DHTStable.h"

DHTStable DHT_A;
DHTStable DHT_B;

#define DHT11_PIN_A       5
#define DHT11_PIN_B       6

void setup()
{
  Serial.begin(115200);
  Serial.println(__FILE__);
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHTSTABLE_LIB_VERSION);
  Serial.println();
}

void loop()
{
  Serial.print("DHT A:\t");
  DHT_A.read11(DHT11_PIN_A);
  Serial.print(DHT_A.getHumidity());
  Serial.print(",\t");
  Serial.println(DHT_A.getTemperature());

  Serial.print("DHT B:\t");
  DHT_B.read11(DHT11_PIN_B);
  Serial.print(DHT_B.getHumidity());
  Serial.print(",\t");
  Serial.println(DHT_B.getTemperature());

  delay(2000);
}

// -- END OF FILE --

With - https://github.com/RobTillaart/DHTNew - library you can set the device PIN in the constructor. It uses almost the same core to fetch the data from the sensors, so it is also stable (imho).

The big difference is that the DHTNew lib has more functionality that won't be backported to DHTStable.

Have a look at - https://github.com/RobTillaart/Temperature - for some temperature/humidity related functions.

awincuk commented 2 years ago

Yes, it works! Thank you so much! This library rocks :)

RobTillaart commented 2 years ago

Good to hear the problem is solved. The example is added to the library so it will be part of a next release (if ever)