Using two DHT 22's with UNO

Hi all.

I am trying to write a sketch for UNO that uses tw DHT 22’s, one that measures Temperature & Humidity in an Air Conditioned Display Cabinet, and the other to measure Temperature & Humidity in the room the Cabinet is in.

I want the Cabinet DHT22 to send a relay HIGH to turn a Fan on, when the Temperature exceeds the set point.

With my sketch the fan relay does not go HIGH, i have tried several different approaches, even copying similar Sketches, but nothing as worked so far, this is why i need your help, what am i doing wrong?

Ray

// Libraries

// Libraries

#include <LCD.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <DHT.h>

//Constants
 
#define DHT1PIN 10    // Cabinet Sensor - what pin we're connected to
#define DHT2PIN 11   // Room Sensor -what pin we're conneted to
#define DHT1TYPE DHT22   // DHT 22  (AM2302)
#define DHT2TYPE DHT22   // DHT 22  (AM2302)
#define fan 12   // what pin fan is connected to.
#define I2C_ADDR 0x27  //ALWAYS USE THIS WITH LCD I2C and Addres 0x27
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

int maxHum = 55; // RH%
int maxTemp = 21; //degrees "C"

DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);
 
void setup() {
pinMode(fan,OUTPUT);
Serial.begin(9600); 
Serial.println("DHTxx test!");
lcd.begin(20,4);
dht1.begin();
dht2.begin();
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);

}
 
void loop() {
delay(2000);   // Wait a few seconds between measurements.
 
float h1 = dht1.readHumidity();
float t1 = dht1.readTemperature();
float h2 = dht2.readHumidity();
float t2 = dht2.readTemperature();

if (isnan(t1) || isnan(h1)) 

Serial.println("Failed to read from DHTs");     // check if returns are valid, if they are NaN (not a number) then something went wrong!

{
if(h1 > maxHum || t1 > maxTemp) 

{
digitalWrite(fan, HIGH);
} 
else 
{
digitalWrite(fan, LOW); 
}

Serial.print("Cabinet Humidity : "); 
Serial.print(h1);
Serial.print(" %\t");
Serial.print("Cabinet Temperature : "); 
Serial.print(t1);
Serial.println(" Degrees C");

Serial.print("Room Humidity : "); 
Serial.print(h2);
Serial.print(" %\t");
Serial.print("Room Temperature : "); 
Serial.print(t2);
Serial.println(" Degrees C");
Serial.println();

// Print to LCD data from DHT1

lcd.clear();
lcd.setCursor(1,0);
lcd.print("Cab Temp : ");
lcd.print(t1);
lcd.print(" ");
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(2,1);} 
lcd.print("Cab Hum : ");
lcd.print(h1);
lcd.print(" % ");

// Print to LCD data from DHT2
 
lcd.setCursor(0,2);
lcd.print("Room Temp : ");
lcd.print(t2);
lcd.print(" ");
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(1,3);
lcd.print("Room Hum : ");
lcd.print(h2);
lcd.print(" % ");

delay(4000); // Wait 4 seconds

 }
 }

What do the debug prints say?

#define DHT2PIN 4    // Room Sensor -what pin we're conneted to
...
#define D4_pin 4

Are you using pin 4 for 2 different purposes ?

To Globalmoderator. I have corrected this error, Pin 4 is now Pin8

To AWOL. being self taught, i have never done debugging, can you give me an example?

To Globalmoderator. I have corrected this error, Pin 4 is now Pin8

Which one ?

i have never done debugging, can you give me an example?

Use Serial.print() at strategic points in the program to print values such as h1, t1, h2 and t2. Do they look right ? Put Serial.prints inside the conditional code blocks so that you know that the code block is being executed

gresleyman:
To AWOL. being self taught, i have never done debugging, can you give me an example?

To Full Member - you read what your sketch has printed to the serial monitor, and see if that aligns with your expectations.

I am still unsure of what to do, i have used Serial.println() to display Temperature & Humidity from each sensor, both Temperatures read the same, Humidity is within one degree of each other, so perfect for me so far.

To monitor action of relay (HIGH / LOW) i am not sure what to write or to where i should write it. My relay is on a board, i have a Red led for Power ON and a Green LED when the relay gets a signal from the UNO, i am not getting the Green led when i alter the Set points in the sketch to actuate the relay.

    if (h1 > maxHum || t1 > maxTemp)

Print these 4 values immediately before you test them. I suggest that you print text labels too so that you can see which values is which

Why is the code following the "failed" message in curly brackets ?

Hi.

Sorry i think translation is awkward.

The curly brackets A ROUND FAILED " " are quotation / punctuation marks, done to highlight the statement.

Print 4 values ?? before testing ?? I need help here. You are showing only 2 values, h1 & t1. where do i print them to ?? The values are displayed in the Serial Monitor, when Arduino has analysed the data it receives from the DHT 22's, how do i print off what has not yet happened ??

No curly brackets like this { }.
You’ve got a superfluous opening one, but I can’t see the closing one

Print 4 values ?? before testing ?? I need help here. You are showing only 2 values, h1 & t1. where do i print them to ??

  if (isnan(t1) || isnan(h1))
  {
    Serial.println("Failed to read from DHTs");     // check if returns are valid, if they are NaN (not a number) then something went wrong!
  }
  Serial.print("h1 : ");
  Serial.println(h1);
  Serial.print("maxHum : ");
  Serial.println(maxHum);
  Serial.print("t1 : ");
  Serial.println(t1);
  Serial.print("maxTemp : ");
  Serial.println(maxTemp);
  Serial.println();
  if (h1 > maxHum || t1 > maxTemp)

Hi UKHELIBOB

i have tried your sketch and i get the same results, i.e data from the DHT's, but the relay for the Fan on Pin12 is still not energising when the actual Temp & hum is above the set points in the sketch.

Hi AWOL.

Where are you looking i see two {} these are called Parenthesis

No, those are braces.
These () are parentheses.

I’m looking at the superfluous opening brace right after "Serial.println(“Failed…”

Hi AWOL.

When i remove the brackets from the sketch, it will not download, i get an error message -
"." before string content

These are brackets .

Where did you remove them from?

Hi all.

Something strange, or it is at least to me, after taking advice and making recommended changes, and still not finding a solution to my problem, i again downloaded my original sketch, and the situation i have no is this -

int naxHum is set to 55% , int maxTemp is set to 21 degrees C. the relay for the fan is now HIGH, but the actual figures are Humidity 45%, Temperature 19 Degrees ??

I do not know whats going on ?

Hint: we can’t see your code.
We can’t see your schematic.

gresleyman:
Hi UKHELIBOB

i have tried your sketch and i get the same results, i.e data from the DHT’s, but the relay for the Fan on Pin12 is still not energising when the actual Temp & hum is above the set points in the sketch.

Print a message when you write to pin 12 to turn the fan on

Do you see the message even though the fan does not run ?

Hi ukhelibob

Is this what you mean ??

{
if(h1 > maxHum || t1 > maxTemp)

{
digitalWrite(fan, HIGH); //Fan is RUNNING}
else
{
digitalWrite(fan, LOW); Fan is OFF
}

Has i have stated previously, the situation with my project now is this -

Both DHT’s taking data and displaying it on a 20,4 lcd, sketch is written so the UNO sets pin 12 HIGH to energise a relay which switches ON a 12 vdc Fan. when the temperature reaches 25 Deg C or 60% Humidity. My situation now is this. Temp is 19 Deg C Humidity is 48%, and the Relay is energised, despite both set points being well below setting point ?? i do not understand.

I have uploaded my code again, if it helps, cannot give a schematic yet, as i have used “Powerpoint” not a proper CAD program.

// Libraries

#include <LCD.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <DHT.h>

//Constants
 
#define DHT1PIN 10    // Cabinet Sensor - what pin we're connected to
#define DHT2PIN 11   // Room Sensor -what pin we're conneted to
#define DHT1TYPE DHT22   // DHT 22  (AM2302)
#define DHT2TYPE DHT22   // DHT 22  (AM2302)
#define fan 12   // what pin fan is connected to.
#define I2C_ADDR 0x27  //ALWAYS USE THIS WITH LCD I2C and Addres 0x27
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

int maxHum = 60; // RH%
int maxTemp = 25; //degrees "C"

DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);
 
void setup() {
pinMode(fan,OUTPUT);
Serial.begin(9600); 
Serial.println("DHTxx test!");
lcd.begin(20,4);
dht1.begin();
dht2.begin();
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);

}
 
void loop() {
delay(2000);   // Wait a few seconds between measurements.
 
float h1 = dht1.readHumidity();
float t1 = dht1.readTemperature();
float h2 = dht2.readHumidity();
float t2 = dht2.readTemperature();

if (isnan(t1) || isnan(h1)) 

Serial.println("Failed to read from DHTs");     // check if returns are valid, if they are NaN (not a number) then something went wrong!

{
if(h1 > maxHum || t1 > maxTemp) 

{
digitalWrite(fan, HIGH);  //Fan Turns running
} 
else 
{
digitalWrite(fan, LOW); Fan is OFF
}

Serial.print("Cabinet Humidity : "); 
Serial.print(h1);
Serial.print(" %\t");
Serial.print("Cabinet Temperature : "); 
Serial.print(t1);
Serial.println(" Degrees C");

Serial.print("Room Humidity : "); 
Serial.print(h2);
Serial.print(" %\t");
Serial.print("Room Temperature : "); 
Serial.print(t2);
Serial.println(" Degrees C");
Serial.println();

// Print to LCD data from DHT1

lcd.clear();
lcd.setCursor(1,0);
lcd.print("Cab Temp : ");
lcd.print(t1);
lcd.print(" ");
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(2,1);} 
lcd.print("Cab Hum : ");
lcd.print(h1);
lcd.print(" % ");

// Print to LCD data from DHT2
 
lcd.setCursor(0,2);
lcd.print("Room Temp : ");
lcd.print(t2);
lcd.print(" ");
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(1,3);
lcd.print("Room Hum : ");
lcd.print(h2);
lcd.print(" % ");

delay(4000); // Wait 4 seconds

 }