First Arduino project HELP!

This is the modified circuit with the Hysteresis , thanks to this forum,
I now have an issue of heat which I need to address.
The current configuration allows the humidity to dictate the fan and vent operation.
I now would like to turn a fan on at around 95 degrees.
Thanks for the consideration.

[/
//
// Humidity control
//  AUTHOR: Curt Crothers
// VERSION: 0.1.01
// PURPOSE: DHT library test sketch for DHT11 && Arduino
//     URL:
//
// Released to the public domain
//

#include <dht.h>

dht DHT;

#define DHT11_PIN 5
int fan = 3;
int vent = 4;



void setup()
{
  Serial.begin(115200);
  Serial.println("DHT TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");

  pinMode (fan, OUTPUT);
  pinMode (vent, OUTPUT);
}

void loop()
{
  // READ DATA
  Serial.print("DHT11, \t");
  int chk = DHT.read11(DHT11_PIN);
 
  // DISPLAY DATA
  Serial.print(DHT.humidity, 1);
  Serial.print(",\t");
  Serial.println(DHT.temperature, 1);
  delay(5000);
  if (DHT.humidity>57) {
    digitalWrite(vent,LOW);
  } else if (DHT.humidity<55){
    digitalWrite(vent, HIGH);
  }


  if (DHT.humidity>61) {
    digitalWrite(fan,LOW);
  }else if (DHT.humidity<59) {
    digitalWrite(fan,HIGH);
  }
}
//
// END OF FILE
//code]

You need to add hysteresis to your if/else statements.

So I think what your saying is your fan is turning on and off quickly because your humidity is going below 55 quickly. If that's the problem then here is some code to fix that.

if (DHT.humidity>55) {
digitalWrite(fan,LOW);
} else {
while(DHT.humidity>30){ //replace 30 with a desirable humidity.
digitalWrite(fan,HIGH);
}
}

A bit late :sleeping: here but something like:

if (DHT.humidity>61) 
{
    digitalWrite(vent,LOW);
} 

else if (DHT.humidity<59)
{
    digitalWrite(vent, HIGH);
}

Both snippets of code are to add hysteresis. If you don't know what that word means, Google it.

Newbie
Posts: 3
Karma: 1 [add]

Re: First Arduino project HELP!

#6

Today at 02:53 pm

I modified my code as follows, but now I have found that with our unseasonably mild spring I need to be able to control the heat too, so how do I add a fan control based upon temp, knowing it may be when the fan is being told to stay off due to humidity?

new code, which solved the bouncing on and off issue:

//
// Humidity control
// AUTHOR: Curt Crothers
// VERSION: 0.1.01
// PURPOSE: DHT library test sketch for DHT11 && Arduino
// URL:
//
// Released to the public domain
//

#include <dht.h>

dht DHT;

#define DHT11_PIN 5
int fan = 3;
int vent = 4;

void setup()
{
Serial.begin(115200);
Serial.println("DHT TEST PROGRAM ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT_LIB_VERSION);
Serial.println();
Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");

pinMode (fan, OUTPUT);
pinMode (vent, OUTPUT);
}

void loop()
{
// READ DATA
Serial.print("DHT11, \t");
int chk = DHT.read11(DHT11_PIN);

// DISPLAY DATA
Serial.print(DHT.humidity, 1);
Serial.print(",\t");
Serial.println(DHT.temperature, 1);
delay(5000);
if (DHT.humidity>57) {
digitalWrite(vent,LOW);
} else if (DHT.humidity<55){
digitalWrite(vent, HIGH);
}

if (DHT.humidity>61) {
digitalWrite(fan,LOW);
}else if (DHT.humidity<59) {
digitalWrite(fan,HIGH);
}
}
//
// END OF FILE
//

It sounds like this is a continuation of another Thread about the same project. If so, please just continue with the original Thread so we have all the project information in the one place.

And please use the code button </>

so your code looks like this

and is easy to copy to a text editor
...R

I have a project I have been working on fro over a year.
I am using an arduinio nano to control the vent , and fan in my pool house.
I first tried just basing it on humidity, humidity over 85% fan on, else fan off.
I now want to measure the outside humidity, and compare that with the inside, and use conditions to determine if the fan should run, and the vent open.
It seems to run non stop though, and I am not sure why?

Can someone on here provide guidance, and a better way to configure this program so it will have a fail safe that will limit the time the fan runs and create a rest cycle?

I can monitor the humidity, and temp. from a serial port window, so it is measuring all the time.

#include <DHT.h>

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
 
// DHT_dual_test
// Demonstrates multiple sensors
// Modified sketch by DIY-SciB.org
 
#include "DHT.h"
 
#define DHT1PIN 2     // what pin we're connected to
#define DHT2PIN 3
int fan=5;
int vent=6;
 
// Uncomment whatever type you're using!
#define DHT1TYPE DHT11   // DHT 11 
#define DHT2TYPE DHT22   // DHT 22  (AM2302)

//#define DHTTYPE DHT21   // DHT 21 (AM2301)
 
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
 
DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);

void setup() {
  Serial.begin(9600); 
  Serial.println("DHTxx test!");
 
  dht1.begin();
  dht2.begin();
}
 
void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h1 = dht1.readHumidity();
  float t1 = dht1.readTemperature();
  float h2 = dht2.readHumidity();
  float t2 = dht2.readTemperature();
 
  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t1) || isnan(h1)) {
    Serial.println("Failed to read from DHT #1");
  } else {
    Serial.print("Humidity 1: "); 
    Serial.print(h1);
    Serial.print(" %\t");
    Serial.print("Temperature 1: "); 
    Serial.print(t1);
    Serial.println(" *C");
  }
  if (isnan(t2) || isnan(h2)) {
    Serial.println("Failed to read from DHT #2");
  } else {
    Serial.print("Humidity 2: "); 
    Serial.print(h2);
    Serial.print(" %\t");
    Serial.print("Temperature 2: "); 
    Serial.print(t2);
    Serial.println(" *C");
    pinMode (fan, OUTPUT);
    pinMode (vent, OUTPUT);
  }
  Serial.println();
 
  if ( h2 > h1+15&&t2>t1
  ) {digitalWrite (fan,HIGH);;
  Serial.print( "FAN ON");}
  else  {digitalWrite (fan,LOW);}
  if (h2 > h1+20&&t2>t1+5
  ) {digitalWrite (vent,HIGH);Serial.print( "Vent Open");}
  else if ( h2 == h1 or h2<h1 ) {digitalWrite (vent,LOW);delay (720000);}
}

dual_DMT_with_controls_version_2_with_temp_compensation_nano_5.ino (2.15 KB)

Hi, can you post your code as described in the forum guide please? Many forum members use mobile devices much of the time and cannot open .ino files.

Relative humidity depends heavily on temperature. It is measured as a percentage of the maximum moisture the air can hold at a given temperature. Outside air temperature is likely to be significantly different to inside temperature, so you can't compare the relative humidities. You might want to think about using absolute humidity, which is measured as grams of moisture per cubic metre, regardless of the temperature. It can be calculated from the temperate and relative humidity. I have code for that if you need it.

For example, where I am right now, outside humidity is 95% and temp is 9C. The absolute humidity is 8.4g/m3. Indoors, humidity is 39% and temp is 23C. The absolute humidity is 8.0g/m3, so the amount of moisture in the air is around the same as outdoors.

I see that while I was posting, you updated your post and included the code. But you didn't do it as described in the forum guide. Please modify your post again. Please perform Auto Format on the code before you re-post it.

I think your "delay()" is misplaced. Now it will only be excuted when h2 is less or equal to h1.

Consider using millis() for timing. Even if delay() might work, it is blocking your code from doing anything else for 9 seconds, and will become a problem when you want to implement more features.

Move this to setup(), as it only needs to be set once.

    pinMode (fan, OUTPUT);
    pinMode (vent, OUTPUT);

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

The DHT11 is not much use for humidity >80%.

Do the printed values make sense? Then your readings are correct.

Is, based on those readings, the fan supposed to run continuously? Then that part is also correct.

When you get readings where the fan is not supposed to run, does it still run? Then also that is correct.

To stop the fan from running longer than some time use millis() based timing: record when you start running, if longer than a certain time switch it off, and don't switch it on for some time after that.

Hi,
This is the third time in 2 years the OP has tried to launch this subject.
I wonder if he/she is waiting for the email to inform that the thread has been answered?

Tom... :o :o :o :o :o

Yes the reported values make sense, they look legitimate.

[/[code]
#include <DHT.h>

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
 
// DHT_dual_test
// Demonstrates multiple sensors
// Modified sketch by DIY-SciB.org
 
#include "DHT.h"
 
#define DHT1PIN 2     // what pin we're connected to
#define DHT2PIN 3
int fan=5;
int vent=6;
 
// Uncomment whatever type you're using!
#define DHT1TYPE DHT11   // DHT 11 
#define DHT2TYPE DHT22   // DHT 22  (AM2302)

//#define DHTTYPE DHT21   // DHT 21 (AM2301)
 
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
 
DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);

void setup() {
  Serial.begin(9600); 
  Serial.println("DHTxx test!");
 
  dht1.begin();
  dht2.begin();
}
 
void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h1 = dht1.readHumidity();
  float t1 = dht1.readTemperature();
  float h2 = dht2.readHumidity();
  float t2 = dht2.readTemperature();
 
  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t1) || isnan(h1)) {
    Serial.println("Failed to read from DHT #1");
  } else {
    Serial.print("Humidity 1: "); 
    Serial.print(h1);
    Serial.print(" %\t");
    Serial.print("Temperature 1: "); 
    Serial.print(t1);
    Serial.println(" *C");
  }
  if (isnan(t2) || isnan(h2)) {
    Serial.println("Failed to read from DHT #2");
  } else {
    Serial.print("Humidity 2: "); 
    Serial.print(h2);
    Serial.print(" %\t");
    Serial.print("Temperature 2: "); 
    Serial.print(t2);
    Serial.println(" *C");
    pinMode (fan, OUTPUT);
    pinMode (vent, OUTPUT);
  }
  Serial.println();
 
  if ( h2 > h1+15&&t2>t1
  ) {digitalWrite (fan,HIGH);;
  Serial.print( "FAN ON");}
  else  {digitalWrite (fan,LOW);}
  if (h2 > h1+20&&t2>t1+5
  ) {digitalWrite (vent,HIGH);Serial.print( "Vent Open");}
  else if ( h2 == h1 or h2<h1 ) {digitalWrite (vent,LOW);delay (720000);}
}

code]

 if ( h2 > h1+15&&t2>t1
  ) {digitalWrite (fan,HIGH);;
  Serial.print( "FAN ON");}

Are your attempts to make your code illegible deliberate?

Do you realise that when h2 is less than or equal to h1 you're not going to do any measurements for the next 12 minutes?

If you would do some proper formatting of your code (e.g. by using the autoformat function of the IDE) it'd become a lot easier to spot this... that formatting is really really horrible, almost worthy an entry to the Obfuscated C contest.

Hi,
What model Arduino are you writing your code for?

Thanks.. Tom.. :slight_smile:

I was attempting to make it only activate the vent, and fan if the humidity was higher in the pool house than it was outside, by a percentage of humidity, and also not run the fan if the temperature inside was colder than the temperature outside, the poorly written code can be attributed to lack of understanding, and newbieism!

By the way it is a NANO . I have tried another one too. I know it is my code, but it just seems to run the fan all night, and I know the humidity has stabilized in an hour or two I can see it in a monitor window.