Loading...
Pages: [1]   Go Down
Author Topic: read 2 DHT22 sensors  (Read 251 times)
0 Members and 1 Guest are viewing this topic.
Madrid,Spain
Offline Offline
Newbie
*
Karma: 4
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

#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
Logged

Netherlands
Offline Offline
Tesla Member
***
Karma: 88
Posts: 9392
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this way?

Code:
#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");
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Madrid,Spain
Offline Offline
Newbie
*
Karma: 4
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

smiley-grin:D:D thank you so much  robtillaart ...it works,I realy apreciate it.
Logged

Netherlands
Offline Offline
Tesla Member
***
Karma: 88
Posts: 9392
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Netherlands
Offline Offline
Tesla Member
***
Karma: 88
Posts: 9392
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Madrid,Spain
Offline Offline
Newbie
*
Karma: 4
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void doPrintTH(float t, float h)
{
  Serial.print(t,2);
  Serial.print(" Celsius      ");
  Serial.print(h,1 );
  Serial.println("% Humidity");
}


thankyou
Logged

Netherlands
Offline Offline
Tesla Member
***
Karma: 88
Posts: 9392
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Pages: [1]   Go Up
Print
 
Jump to: