Pages: [1]   Go Down
Author Topic: read 2 DHT22 sensors  (Read 1918 times)
0 Members and 1 Guest are viewing this topic.
Madrid,Spain
Offline Offline
Newbie
*
Karma: 4
Posts: 41
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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13915
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 -
(Please do not PM for private consultancy)

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

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13915
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 -
(Please do not PM for private consultancy)

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13915
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 -
(Please do not PM for private consultancy)

Madrid,Spain
Offline Offline
Newbie
*
Karma: 4
Posts: 41
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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13915
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 -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13915
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


* dht22.jpg (104.93 KB, 501x607 - viewed 70 times.)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13915
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: