Multiple Dht22 Sensors

Good night Arduino community. Im trying to connect 4 dht 22 sensors.
This is my code:

//Libraries
#include <DHT.h>;

//Constants
#define DHT11_DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define DHTPIN 3
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define DHTPIN 4
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define DHTPIN 5
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

//Variables
int chk;
float hum1; //Stores humidity value
float temp1; //Stores temperature value
float hum2;
float temp2;
float hum3;
float temp3;
float hum4;
float temp4;

void setup()
{
Serial.begin(9600);
dht.begin();

}

void loop()
{
//Read data and store it to variables hum and temp
hum1 = dht.readHumidity();
temp1= dht.readTemperature();
hum2 = dht.readHumidity();
temp2= dht.readTemperature();
hum3 = dht.readHumidity();
temp3= dht.readTemperature();
hum4 = dht.readHumidity();
temp4= dht.readTemperature();

//Print temp and humidity values to serial monitor
Serial.print("Sensor 1: Humidity: ");
Serial.print(hum1);
Serial.print(" %, Temp: ");
Serial.print(temp1);
Serial.println(" Celsius");

Serial.print("Sensor 2: Humidity: ");
Serial.print(hum2);
Serial.print(" %, Temp: ");
Serial.print(temp2);
Serial.println(" Celsius");

Serial.print("Sensor 3: Humidity: ");
Serial.print(hum3);
Serial.print(" %, Temp: ");
Serial.print(temp3);
Serial.println(" Celsius");

Serial.print("Sensor 4: Humidity: ");
Serial.print(hum4);
Serial.print(" %, Temp: ");
Serial.print(temp4);
Serial.println(" Celsius");
delay(2000); //Delay 2 sec.
}

As u can in the print see i have always the same output for diferent variables.

Can someone help me?
Thank you.

you have to create multiple instances of a DHT object.

DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);

but you should learn to use your arrays:

#include <DHT.h>;

#define DHTPIN1 2 
#define DHTPIN2 3
#define DHTPIN3 4
#define DHTPIN4 5

DHT dht[] = {
  {DHTPIN1, DHT22},
  {DHTPIN2, DHT22},
  {DHTPIN3, DHT22},
  {DHTPIN4, DHT22},
};

float humidity[4];
float temperature[4];

void setup()
{
  Serial.begin(9600);
  for (auto& sensor : dht) {
    sensor.begin();
  }
}

void loop()
{
  for (int i = 0; i < 4; i++) {
    temperature[i] = dht[i].readTemperature();
    humidity[i] = dht[i].readHumidity();
  }

  for (int i = 0; i < 4; i++) {
    Serial.print(F("Temperature "));
    Serial.print(i);
    Serial.println(temperature[i]);
    Serial.print(F("Humidity "));
    Serial.print(i);
    Serial.println(humidity[i]);
  }
  delay(5000);
}
2 Likes

God bless you Lowell. Thank you for your help, i really appreciate it.

Could you or someone tell me exactly where you got the <DHT.h> library from and if known is it compatible with mega 2560 v3? Been trying to do something similar for sometime having issues with matching libraries to sketch code samples. I'm very new to all this. Thanks

Hello everyone

I found this thread which suits my needs and my difficulties at the moment quite well. I would like to know if anyone could quickly explain to me the following bit of code:

for (auto& sensor : dht) {

I tried to find answers on the web as to the meaning of "auto&" and ":" but couldnt find anything.

Thank you all for your inputs.

Cheers,

moses

I tried to find answers on the web as to the meaning of "auto&" and ":" but couldnt find anything

That's a really good reason for not jamming the & up against the type. It makes no more sense than char*.

Auto means "I haven't a clue what type this variable should be. Compiler, you figure it out and use the proper type".

The rest of it means "I know that dht is an array, and I want you to figure out the length and iterate the proper number of times, handling each element of the array, one at a time."

I know that some people will disagree with my description of what auto means, but I don't really care.

That for loop could be written:

for(byte i=0; i<sizeof(dht)/sizeof(dht[0]; i++)
{
   DHT &someDht = dht[i];
   someDHT.begin();
}

I find that code a lot easier to explain.

1 Like
for(byte i=0; i<sizeof(dht)/sizeof(dht[0]; i++)
{
   dht[i].begin();
}

gfvalvo:

for(byte i=0; i<sizeof(dht)/sizeof(dht[0]; i++)

{
  dht[i].begin();
}

Sure, but how do you compare that code, with no references, to the code with references, to explain the relationship?

Hello PaulS

Thank you for your inputs.

Just for my further understanding some questions for clarification:

auto - gets a datatype for a variable with an unknown type?
& - is it "the addresse of"? And of what is it the addresse? The array?
: - I still don't understand the meaning of this sign. How could it be explained so I could use it independently the next time?

Is it true that I could use all of the above examples to reach the same goal?

Thank you.

moses

HI .. im Use the Code 4 Sensor DHT22

the code does not separate the result .. how do you do that i only get errors .. here are pictures

Imgur

BulldogLowell:
you have to create multiple instances of a DHT object.

DHT dht1(DHTPIN1, DHTTYPE);

DHT dht2(DHTPIN2, DHTTYPE);




but you should learn to use your arrays:



#include <DHT.h>;

#define DHTPIN1 2
#define DHTPIN2 3
#define DHTPIN3 4
#define DHTPIN4 5

DHT dht[] = {
{DHTPIN1, DHT22},
{DHTPIN2, DHT22},
{DHTPIN3, DHT22},
{DHTPIN4, DHT22},
};

float humidity[4];
float temperature[4];

void setup()
{
Serial.begin(9600);
for (auto& sensor : dht) {
sensor.begin();
}
}

void loop()
{
for (int i = 0; i < 4; i++) {
temperature[i] = dht[i].readTemperature();
humidity[i] = dht[i].readHumidity();
}

for (int i = 0; i < 4; i++) {
Serial.print(F("Temperature "));
Serial.print(i);
Serial.println(temperature[i]);
Serial.print(F("Humidity "));
Serial.print(i);
Serial.println(humidity[i]);
}
delay(5000);
}

Hello,

I don know what is wrong with this example but its not working, The only message i got on serial is:
"dht11 start condition 2 not met". - problem is when I'm using as suggested the arrays, When Im using this

DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
DHT dht3(DHTPIN1, DHTTYPE);
DHT dht4(DHTPIN2, DHTTYPE);

and them in program

temperature[1] = dht1.readTemperature();
temperature[2] = dht2.readTemperature();
temperature[3] = dht3.readTemperature();
temperature[4] = dht4.readTemperature();

It is working fine, but when i use this code:

#include <DHT.h>;

#define DHTPIN1 2
#define DHTPIN2 3
#define DHTPIN3 4
#define DHTPIN4 5

DHT dht[] = {
  {DHTPIN1, DHT22},
  {DHTPIN2, DHT22},
  {DHTPIN3, DHT22},
  {DHTPIN4, DHT22},
};

float humidity[4];
float temperature[4];

void setup()
{
  Serial.begin(9600);
  for (auto& sensor : dht) {
    sensor.begin();
  }
}

void loop()
{
  for (int i = 0; i < 4; i++) {
    temperature[i] = dht[i].readTemperature();
    humidity[i] = dht[i].readHumidity();
  }

  for (int i = 0; i < 4; i++) {
    Serial.print(F("Temperature "));
    Serial.print(i);
    Serial.println(temperature[i]);
    Serial.print(F("Humidity "));
    Serial.print(i);
    Serial.println(humidity[i]);
  }
  delay(5000);
}

I'm getting message:
"dht11 start condition 2 not met" on serial output.

What is wrong whith this code? I really need this on array as its much clear in code.