read 2 DHT22 sensors

Hi there all...i am into a project wich read temperature,i have used the LM35 before but beacause of acuracy,and limited lenght of the cable,errors and such ....i decided to do it with the DHT22/RHT03.Reading the temperature with the <DHT22.h> library works great.The code i use is:

#include <DHT22.h>
// Only used for sprintf
#include <stdio.h>

// Data wire is plugged into port 7 on the Arduino
// Connect a 4.7K resistor between VCC and the data pin (strong pullup)
#define DHT22_PIN 7

// Setup a DHT22 instance
DHT22 myDHT22(DHT22_PIN);

void setup(void)
{
  // start serial port
  Serial.begin(9600);
}

void loop(void)
{ 
  DHT22_ERROR_t errorCode;
  
  // The sensor can only be read from every 1-2s, and requires a minimum
  // 2s warm-up after power-on.
  delay(2000);
  
  errorCode = myDHT22.readData();
  switch(errorCode)
  {
    case DHT_ERROR_NONE:
      Serial.print(myDHT22.getTemperatureC());
      Serial.print(" Celsius      ");
      Serial.print( myDHT22.getHumidity());
      Serial.println("% Humidity");
      // Alternately, with integer formatting which is clumsier but more compact to store and
	  // can be compared reliably for equality:
	  //	  
      char buf[128];

  }
}

my intencion is to keep it most simple as posible.can anyone help me with the code to add another sensor,so i can read 2 DHT22?
i realy apreciate it,thank you all of you.
Juycce

this way?

#include <DHT22.h>
// Only used for sprintf
#include <stdio.h>

// Data wire is plugged into port 7 on the Arduino
// Connect a 4.7K resistor between VCC and the data pin (strong pullup)
#define DHT22_PIN 7
#define DHT222_PIN 8 /// <<<<<<<<<<<<<<<<<<<

// Setup a DHT22 instance
DHT22 myDHT22(DHT22_PIN);
DHT22 myDHT222(DHT222_PIN);

void setup(void)
{
  // start serial port
  Serial.begin(9600);
}

void loop(void)
{ 
  DHT22_ERROR_t errorCode;
  
  // The sensor can only be read from every 1-2s, and requires a minimum
  // 2s warm-up after power-on.
  delay(2000);
  
  errorCode = myDHT22.readData();
  if (errorCode == DHT_ERROR_NONE)
  {
      doPrintTH(myDHT22.getTemperatureC(), myDHT22.getHumidity())
  }

  errorCode = myDHT222.readData();
  if (errorCode == DHT_ERROR_NONE)
  {
      doPrintTH(myDHT222.getTemperatureC(), myDHT222.getHumidity())
  }
}

void doPrintTH(float t, float h)
{
  Serial.print(t,2);
  Serial.print(" Celsius      ");
  Serial.print(h,1 );
  Serial.println("% Humidity");
}

:D:D:D thank you so much robtillaart ...it works,I realy apreciate it.

note you can specify the number of decimals for floats with the second number e.g. Serial.print(t,2);

Furthermore is you want to add more, you might consider using an array of DHT22's instead of single objects.

could u please explain me what are (t,2) and (h,1 ) do....I mean i know the 't' is temperatue and 'h' is humidity but what about the numbers 2 and 1 ? what are they doing

void doPrintTH(float t, float h)
{
  Serial.print(t,2);
  Serial.print(" Celsius      ");
  Serial.print(h,1 );
  Serial.println("% Humidity");
}

thankyou

as explained 2 posts before, for printing a floating point this number indicates the number of decimals.

print(PI, 1) => 3.1
print(PI, 2) => 3.14
print(PI, 7) => 3.1415927
etc

Why when i use this code it error DHT22 myDHT22(DHT22_PIN); ???

#include <DHT22.h>
// Only used for sprintf
#include <stdio.h>

// Data wire is plugged into port 7 on the Arduino
// Connect a 4.7K resistor between VCC and the data pin (strong pullup)
#define DHT22_PIN 7
#define DHT222_PIN 8 /// <<<<<<<<<<<<<<<<<<<

// Setup a DHT22 instance
DHT22 myDHT22(DHT22_PIN);
DHT22 myDHT222(DHT222_PIN);

void setup(void)
{
// start serial port
Serial.begin(9600);
}

void loop(void)
{
DHT22_ERROR_t errorCode;

// The sensor can only be read from every 1-2s, and requires a minimum
// 2s warm-up after power-on.
delay(2000);

errorCode = myDHT22.readData();
if (errorCode == DHT_ERROR_NONE)
{
doPrintTH(myDHT22.getTemperatureC(), myDHT22.getHumidity())
}

errorCode = myDHT222.readData();
if (errorCode == DHT_ERROR_NONE)
{
doPrintTH(myDHT222.getTemperatureC(), myDHT222.getHumidity())
}
}

void doPrintTH(float t, float h)
{
Serial.print(t,2);
Serial.print(" Celsius ");
Serial.print(h,1 );
Serial.println("% Humidity");
}

did you install the DHT22 library?
and restarted all instances of the IDE after the install?

I allready to installed DHT22 but it still error " DHT22 myDHT22(DHT22_PIN);" Please help how to do that?

Hello

I am new in this systems but I copy this code and it gives me an error ; expected before token.
I want use arduino to monitorize 6 sensors in varius places of my house in portugal because I am working in switzerland.
If possible please help.

Thanks in advance

Carlos

carlosasmartinho:
Hello

I am new in this systems but I copy this code and it gives me an error ; expected before token.
I want use arduino to monitorize 6 sensors in varius places of my house in portugal because I am working in switzerland.
If possible please help.

Thanks in advance

Carlos

HI Carlos, welcome,
You state you copied the code but as there are more snippets of code here you should make it more explicit what you copied (refer to the exact post, or post the code yourself - #button ==> code tags)

The error you have is probably small and easy to fix.
Extending this code to six DHT22's should not be a problem, but you should at least add some lines of code to indicate which sensor is printed.

Hello..
I am trying also to read from two different DHT22 sensors..
i tried the solution gaven by Robtillaart for two DHT..
it works but gives me only one report (1 humidity measure, 1 temperature measure,...)
i wonder if it's posible to have two reports, one for each DHT22 sensor..

on my project i have two sensors inside and one sensor outside.. i would like to compare the values of the sensors..
how can i store the temperature value and the humidity value of each sensor to make my comparison with it..??

i would apreciate your help.. :slight_smile: