Multiple DHT11 Sensors

Hi everybody...

I try to use more than one DHT11 sensor with arduino nano (328).
my code works good with one sensor but I don't know the way for multiple sensors...
could you give me a example ?

Thanks!

my example code:

#include "DHT.h"

DHT dht;

void setup()
{
  Serial.begin(9600);
  Serial.println();
  Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)");

  dht.setup(2); // data pin 2
}

void loop()
{
  delay(dht.getMinimumSamplingPeriod());

  float humidity = dht.getHumidity();
  float temperature = dht.getTemperature();

  Serial.print(dht.getStatusString());
  Serial.print("\t");
  Serial.print(humidity, 1);
  Serial.print("\t\t");
  Serial.print(temperature, 1);
  Serial.print("\t\t");
  Serial.println(dht.toFahrenheit(temperature), 1);
}

I use this lib. GitHub - markruys/arduino-DHT: Efficient DHT library for Arduino

I think you need to declare more DHT object:

#include "DHT.h"

DHT dht1;
DHT dht2;

void setup()
{
  Serial.begin(9600);
  Serial.println();
  Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)");
  dht1.setup(2); // data pin 2
  dht2.setup(3); // data pin 3
}

and after on loop() use dht1 or/and dht2 object for read.

Thank you for your response...

I already tried with your solution.. it's working but I want simplifying the code.. with arrays ? or with any function ?

Thanks.

it's working but I want simplifying the code

What code? I don't see any (of your) code.

#include "DHT.h"

DHT dht[3];  // element 0 to 2 

void setup()
{ Serial.begin(9600);
  Serial.println();
  Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)");
  dht[0].setup(2); // data pin 2
  dht[1].setup(3); // data pin 3
...
}

I am fully aware this is an old post, but wouldn't it make this simpler to use a function rather then an array? Like getDHT(n, d) where n is the sensor to read from and d is the data you need (0=temp and 1=humidity). Have your function return the information, and use a "switch/case" within the function to choose what sensor information is needed.

Just an idea, I may do this myself (making sensors for a green house) and post an example code once its working, for future readers.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.