Christian
Published © GPL3+

Basement/Crawlspace Ventilation System

Intelligently reduce the moisture in your basement/crawlspace to help control mildew growth and lower your heating/cooling bill.

IntermediateFull instructions provided12,603
Basement/Crawlspace Ventilation System

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
IO Expander
×1
IO Expander Bundle
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Basement/Crawlspace Ventilation System

Use the Arduino Nano to create a Smart Ventilation System.

Code

Basement/Crawlspace Ventilation System

C/C++
Use the Arduino Nano to create a Smart Ventilation System
/* IO Expander 
 *  
 * Basement/Crawlspace Ventilation System v1.1
 * 
 */

#include <math.h>
#include <SoftwareSerial.h>
#include <avr/wdt.h>
#include "IOExpander.h"

#define FAHRENHEIT
#define ONEWIRE_TO_I2C_ROM      "i4s71"
#define INIT_OLED               "st13;si;sc;sd"
#define HUMIDITY_SENSOR_INSIDE  "s6t1"
#define HUMIDITY_SENSOR_OUTSIDE "s8t1"
#define FAN_ON                  "r1o"
#define FAN_OFF                 "r1f"
#define ABSOLUTE_DELTA_FAN_ON   1           // Fan on if absolute humidity delta of inside >= outside 
#define ABSOLUTE_DELTA_FAN_OFF  0.5         // Fan off if absolute humidity delta of inside <= outside
#define OUTSIDE_RELATIVE_FAN_ON 88          // Fan on if outside relative humidity is <= %
#define OUTSIDE_RELATIVE_FAN_OFF 90         // Fan off if outside relative humidity is >= %
#define MINIMUM_TEMPERATURE     15          // Cycle vent on/off if outside temperature <= 15C/59F
#define FAN_ON_TIME             (20*60*1000L) // 20 min
#define FAN_OFF_TIME            (20*60*1000L) // 20 min

//#define SERIAL_DEBUG
#define SERIAL_TIMEOUT  5000                // 5 sec delay between DHT22 reads

#ifdef SERIAL_DEBUG
SoftwareSerial swSerial(8,7);
#endif

struct HS {
  float temp;
  float relative;
  float absolute;
  bool error;
};

int led = 13;
bool init_oled = true;
long ontime, offtime;

#ifdef FAHRENHEIT
#define C2F(temp)   CelsiusToFahrenheit(temp)
float CelsiusToFahrenheit(float celsius)
{
  return ((celsius*9)/5)+32;
}
#else
#define C2F(temp)   (temp)
#endif

void SerialPrint(const char* str, float decimal, char error)
{
  Serial.print(str);
  if (error) Serial.print(F("NA"));
  else Serial.print(decimal, 1);
}

float DewPoint(float temp, float humidity)
{
  float t = (17.625 * temp) / (243.04 + temp);
  float l = log(humidity/100);
  float b = l + t;
  // Use the August-Roche-Magnus approximation
  return (243.04*b)/(17.625-b);
}

#define MOLAR_MASS_OF_WATER     18.01534
#define UNIVERSAL_GAS_CONSTANT  8.21447215

float AbsoluteHumidity(float temp, float relative)
{
  //taken from https://carnotcycle.wordpress.com/2012/08/04/how-to-convert-relative-humidity-to-absolute-humidity/
  //precision is about 0.1°C in range -30 to 35°C
  //August-Roche-Magnus   6.1094 exp(17.625 x T)/(T + 243.04)
  //Buck (1981)     6.1121 exp(17.502 x T)/(T + 240.97)
  //reference https://www.eas.ualberta.ca/jdwilson/EAS372_13/Vomel_CIRES_satvpformulae.html    // Use Buck (1981)
  return (6.1121 * pow(2.718281828, (17.67 * temp) / (temp + 243.5)) * relative * MOLAR_MASS_OF_WATER) / ((273.15 + temp) * UNIVERSAL_GAS_CONSTANT);
}

void ReadHumiditySensor(HS* hs)
{
  SerialCmd("sr");
  if (SerialReadFloat(&hs->temp) &&
      SerialReadFloat(&hs->relative)) {
    //hs->dewpoint = DewPoint(hs->temp, hs->relative);
    hs->absolute = AbsoluteHumidity(hs->temp, hs->relative);
    hs->error = false;
  }  
  else hs->error = true;
  SerialReadUntilDone();
}

void setup() {
  Serial.begin(115200);
#ifdef SERIAL_DEBUG
  swSerial.begin(115200);
  //swSerialEcho = &swSerial;
#endif  
  pinMode(led, OUTPUT);
  wdt_enable(WDTO_8S);
  offtime = millis() - FAN_OFF_TIME;
}

void loop() {
  HS inside, outside;
  static bool fan = false;
  static bool cycle = false;
  static long last_time = -(60L * 1000L);

  Serial.println();
  if (SerialReadUntilDone()) {
    //if (init_oled) {
    //  if (SerialCmdNoError(ONEWIRE_TO_I2C_ROM)) {
    //    SerialCmdDone(INIT_OLED);
    //    init_oled = false;
    //  }
    //}

    // Read the humidity sensors only once a minute or they will self heat if read too quickly
    if (millis() - last_time > 60L * 1000L)
    {
      if (SerialCmdDone(HUMIDITY_SENSOR_INSIDE)) 
        ReadHumiditySensor(&inside);
  
      if (SerialCmdDone(HUMIDITY_SENSOR_OUTSIDE))
        ReadHumiditySensor(&outside);
  
      if (inside.error || outside.error) fan = false;
      else {
        if (fan) {
          if (outside.relative >= OUTSIDE_RELATIVE_FAN_OFF || inside.absolute - outside.absolute <= ABSOLUTE_DELTA_FAN_OFF)
            cycle = fan = false;
          else {
            if (cycle && outside.temp <= MINIMUM_TEMPERATURE && 
              millis() - ontime > FAN_ON_TIME) fan = false;  
          }
          if (!fan) offtime = millis();
        }
        else {
          if (outside.relative <= OUTSIDE_RELATIVE_FAN_ON && inside.absolute - outside.absolute >= ABSOLUTE_DELTA_FAN_ON) 
            cycle = fan = true;
          if (cycle && outside.temp <= MINIMUM_TEMPERATURE)
            fan = (millis() - offtime > FAN_OFF_TIME) ? true : false;        
          if (fan) ontime = millis();
        }
      }
  
      if (fan) SerialCmdDone(FAN_ON);
      else SerialCmdDone(FAN_OFF);
  
      if (SerialCmdNoError(ONEWIRE_TO_I2C_ROM)) {
        if (init_oled) {
          SerialCmdDone(INIT_OLED);
          init_oled = false;
        }
        SerialCmdDone("st13;sc;sf0;sa1;sd70,0,\"INSIDE\";sd127,0,\"OUTSIDE\";sf1;sa0;sd0,12,248,\""
  #ifdef FAHRENHEIT
            "F" 
  #else          
            "C" 
  #endif          
            "\";sd0,30,\"%\";sf0;sd0,50,\"g/m\";sd20,46,\"3\";");
        SerialPrint("sf1;sa1;sd70,12,\"", C2F(inside.temp), inside.error);
        SerialPrint("\";sd70,30,\"", inside.relative, inside.error);
        SerialPrint("\";sd70,48,\"", inside.absolute, inside.error);
        SerialPrint("\";sd127,12,\"", C2F(outside.temp), outside.error);
        SerialPrint("\";sd127,30,\"", outside.relative, outside.error);
        SerialPrint("\";sd127,48,\"", outside.absolute, outside.error);
        Serial.print("\";");
        Serial.print("sf0;sa0;sd0,0,\"");
        if (fan) Serial.print("FAN");
        else Serial.print("v1.1");
        Serial.println("\";sd");
        SerialReadUntilDone();
      }
      else init_oled = true;

      last_time = millis();
    }

    delay(1000);
  }
  else {
    digitalWrite(led, HIGH);
    delay(500);
    digitalWrite(led, LOW);
    delay(500);
    init_oled = true;
  }
  wdt_reset();
}

Credits

Christian

Christian

24 projects • 134 followers
Senior Embedded Engineer

Comments