View Single Post
Old 01-25-15, 01:25 PM   #2
buffalobillpatrick
Master EcoRenovator
 
Join Date: Mar 2014
Location: Florissant, Colorado
Posts: 599
Thanks: 814
Thanked 59 Times in 55 Posts
Default

I modified sketch to be able to use multiple AM2301 sensors & to take 25 samples & average them before each Serial Print, this is done to smooth out a little bouncing around that I was seeing.

BTW, the 2 AM2301 sensors that I have on hand consistantly read RH about 2.7% apart from each other. I have ordered 3 more from China, I will probably have to correct for their offsets with code.

It would be easy to modify some more & have sketch energize a relay to run dehumidifier automaticaly and to stop cooling ceiling until humidity drops enough.

Code:
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
// Modified by BBP
// Sensors are wired to pins D2, D3, etc


#include <DHT.h>

#define NUM_SENSORS           2
#define SENSOR_STARTING_PIN   2    //D2

#define Sample_cnt 25


#define DHTPIN 2     // what pin we're connected  D2
//#define DHTPIN 3     // what pin we're connected  D3

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE 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 dht(DHTPIN, DHTTYPE);

float tF;
float dP;
float dPF;
float h;
float t;
   
float alpha =   0.9;     // factor to tune
float average;
  
int i;
 
int I;

void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx test!");

//  dht.begin();
}

void loop() {

  for ( I = SENSOR_STARTING_PIN; I < (SENSOR_STARTING_PIN + NUM_SENSORS); I++ )
  {
  
  DHT dht(I, DHTTYPE);
  dht.begin();
   
  // Reading temperature or humidity takes about 250 milliseconds!
  // float h = dht.readHumidity();
  // float t = dht.readTemperature();
  
  average = 0.0;  
  for (i=0; i< Sample_cnt; i++) 
  {  
  average =  alpha * dht.readHumidity() + (1-alpha) * average;
  delay(10);
  }
  h = average;

  average = 0.0;  
  for (i=0; i< Sample_cnt; i++) 
  {  
  average =  alpha * dht.readTemperature() + (1-alpha) * average;
  delay(10);
  }
  t = average;

 // delay(10000);

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } 
  else {

    Serial.print("Sensor Pin D");
    Serial.print(I);
    Serial.print("  ");
    
    Serial.print("Humidity:");
    Serial.print(h);
    Serial.print("%  ");
    
    Serial.print("Temp:");
    // Serial.print(t);
    // Serial.print(" *C ");
    tF=((t*9)/5)+32;
    Serial.print(tF);
    Serial.print("*F  ");


  Serial.print("Dew Point:");
  // Serial.print(dewPointFast(t, h));
  // Serial.print(" *C ");
  dP=(dewPointFast(t, h));
  dPF=((dP*9)/5)+32;
  Serial.print(dPF);
  Serial.print("*F");
  Serial.print("  ");

  Serial.print("Heat Index:");
  Serial.print(heatIndex(tF,h));
  Serial.println("*F");

  }  //end else
  
  }  //end for loop reading sensors

}    //end void loop

// delta max = 0.6544 wrt dewPoint()
// 6.9 x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity*0.01);
double Td = (b * temp) / (a - temp);
return Td;
}

double heatIndex(double tempF, double humidity)  //how hot it feels
{
  double c1 = -42.38, c2 = 2.049, c3 = 10.14, c4 = -0.2248, c5= -6.838e-3, c6=-5.482e-2, c7=1.228e-3, c8=8.528e-4, c9=-1.99e-6  ;
  double T = tempF;
  double R = humidity;

  double A = (( c5 * T) + c2) * T + c1;
  double B = ((c7 * T) + c4) * T + c3;
  double C = ((c9 * T) + c8) * T + c6;

  double rv = (C * R + B) * R + A;
  return rv;
}

DHTxx test!
Sensor Pin D2 Humidity:36.00% Temp:70.16*F Dew Point:41.95*F Heat Index:76.38*F
Sensor Pin D3 Humidity:33.30% Temp:70.52*F Dew Point:40.25*F Heat Index:76.16*F
Sensor Pin D2 Humidity:36.10% Temp:70.16*F Dew Point:42.02*F Heat Index:76.39*F
Sensor Pin D3 Humidity:33.20% Temp:70.52*F Dew Point:40.18*F Heat Index:76.15*F
Sensor Pin D2 Humidity:35.99% Temp:70.16*F Dew Point:41.94*F Heat Index:76.38*F
Sensor Pin D3 Humidity:33.20% Temp:70.70*F Dew Point:40.33*F Heat Index:76.15*F
Sensor Pin D2 Humidity:35.90% Temp:70.16*F Dew Point:41.88*F Heat Index:76.37*F
Sensor Pin D3 Humidity:33.09% Temp:70.70*F Dew Point:40.25*F Heat Index:76.14*F
Sensor Pin D2 Humidity:35.80% Temp:70.16*F Dew Point:41.81*F Heat Index:76.37*F
Sensor Pin D3 Humidity:33.21% Temp:70.52*F Dew Point:40.18*F Heat Index:76.15*F

Last edited by buffalobillpatrick; 01-25-15 at 05:08 PM..
buffalobillpatrick is offline   Reply With Quote