Go Down

Topic: read 2 DHT22 sensors (Read 2341 times) previous topic - next topic

juycce

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:
Code: [Select]


#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

robtillaart

this way?

Code: [Select]

#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");
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

juycce

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

robtillaart


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

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

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

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

juycce

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

Code: [Select]

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



thankyou

robtillaart

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
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

ninethio

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");
}

robtillaart

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

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

ninethio

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

robtillaart


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.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up