Shaeroden
Published © GPL3+

Humidity Controller

A simple device that makes controlling humidity and saving energy a snap. Operates as easily as a thermostat.

IntermediateFull instructions provided15,170
Humidity Controller

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
DHT11 Temperature & Humidity Sensor (3 pins)
DHT11 Temperature & Humidity Sensor (3 pins)
×1
Grove - 2-Channel SPDT Relay
Seeed Studio Grove - 2-Channel SPDT Relay
×1
Alphanumeric LCD, 16 x 2
Alphanumeric LCD, 16 x 2
×1
Jumper wires (generic)
Jumper wires (generic)
×1
2-Gang 47 cu. in. PVC New/Old Work Electrical Box (B249B)
×1
Decora 2-Gang Midway Nylon Wall Plate - White
×1
decora duplex outlet, white
×1
yellow wire nuts
×1
5V 2.5A Switching Power Supply
Digilent 5V 2.5A Switching Power Supply
×1
grounded power cable
can use any grounded power cable such as extension cord or appliance cord.
×1
Resistor 10k ohm
Resistor 10k ohm
for button circuits
×2
resistor 2k ohm
for lcd backlight setting.
×1

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

wiring diagram

Code

Humidity controller code

C/C++
code for operating the humidity controller
//HUMIDITY CONTROLLER 1.1
//written for NANO

//controls and displays relative humidity
//sesnor used is DHT11
//uses active-low relay module to control live lines for 2 standard electrical outlets
//uses i2c lcd to display humidity and humidity setting
//turns display off during inactivity
//setting is adjustable from 10%-90% using 2 buttons
//backlight of LCD is controlled by pin 4, connected to top LED jumper pin on i2c backpack
//serial communications to monitor to ensure all code is working.

//added: "system off" function - allows both outlets to be turned off

// █ G █ L █ O █ B █ A █ L █ S █
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <dht.h>

// set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);  

dht DHT;

#define DHT11_PIN A2

//buttons and variables to adjust calibration
int calupbtn = A0;
int calup;
int caldownbtn = A1;
int caldown;

//i2c PINS A4, A5

//pin for turning on humidifier relay
int humidifier = 3;
//pin for turning on the fan relay
int fan = 2;
//pin for turning on LCD backlights
int lcdlight = 4;

//calibration variable
int setpoint = 50;  //feedback setpoint.  
bool calstate = false;  //enables calibration adjustments only when LCD is ON.

//backlight timing variables
int displtime = 12000;  //amount of time the display is to be on before turning off
unsigned long displtimeon;  //last recorded time display timer was reset
int calunlock = 0;   //loop counter for unlocking calibration  

//sensor timing variables
unsigned long lastcheck;  //last time DHT was read
long interval = 30000;      //time between DHT readings

//system variables
bool syson = true;    //reference for system on/off. 
byte systog = 2;       //even number toggles on, odds toggle off
int syslock = 0;     //loop counter for unlocking system toggle

// █ S █ E █ T █ U █ P █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █
void setup(){

  Serial.begin(9600);  //serial communication used to ensure code is working correctly.
  
  lcd.init();  //initialize LCD
  lcd.backlight(); //enable LCD backlight, but doesn't turn it on

  lastcheck = 0;
  digitalWrite(lcdlight, HIGH);
  Serial.println("STARTING");
  lcd.print("   STARTING");

  //pin assignments
  pinMode(calupbtn, INPUT);
  pinMode(caldownbtn, INPUT);
  pinMode(humidifier, OUTPUT);
  pinMode(fan, OUTPUT);
  pinMode(lcdlight, OUTPUT);
  delay(1000);

  
  //test fan
  lcd.setCursor(0,1);
  lcd.print("<FAN>     HMDFR  ");  
  digitalWrite(fan, LOW);
  digitalWrite(humidifier, HIGH);
  delay(6000);
  //test humidifier
  lcd.setCursor(0,1);
  lcd.print(" FAN     <HMDFR> ");
  digitalWrite(fan, HIGH);
  digitalWrite(humidifier, LOW);
  delay(6000);
  //stop startup test
  digitalWrite(humidifier, HIGH);
  digitalWrite(fan, HIGH);
  lcd.clear();

  displtimeon = millis();

  int chk = DHT.read11(DHT11_PIN);
  lastcheck = millis();

  lcd.setCursor(0,0);
  lcd.print("Humidity: ");
  lcd.setCursor(13,0);
  lcd.print(DHT.humidity);
  lcd.setCursor(15,0);
  lcd.print("%  ");
  lcd.setCursor(0,1);
  lcd.print("setting: ");
  lcd.setCursor(13,1);
  lcd.print(setpoint);
  lcd.setCursor(15,1);
  lcd.print("%  ");
  delay(100);
}

// █ L █ O █ O █ P █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █
void loop(){

  //check calibration buttons
  calup = digitalRead(calupbtn);
  caldown = digitalRead(caldownbtn);

  if(calup == HIGH and caldown == HIGH){  //--------SYSTEM TOGGLE
    syslock ++;
    Serial.println(syslock);
    if(syslock == 20){  //if both buttons held down for this many loops
      systog++;
      if(systog % 2 == 1){  //--------SYSTEM OFF
        syson = false;
        digitalWrite(fan, HIGH);
        digitalWrite(humidifier, HIGH);
        digitalWrite(lcdlight, HIGH);
        Serial.println("SYSTEM TURNED OFF");
        lcd.clear();
        lcd.print("   SYSTEM OFF");
        lcd.setCursor(0,1);
        lcd.print("  hold both btns");
        delay(2000);
        lcd.setCursor(0,1);
        lcd.print("  to turn on    ");
        displtimeon = millis();
      }
      if(systog % 2 == 0){  //--------SYSTEM ON
        syson = true;
        Serial.println("SYSTEM TURNED ON");
        digitalWrite(lcdlight, HIGH);
        lcd.clear();
        lcd.print("   SYSTEM ON");
        delay(2000);
        lcd.clear();
        displtimeon = millis();
      }
      syslock = 0;
    }
  }
  else(syslock = 0);
  
  //read humidity at appropriate intervals
  if(millis() > lastcheck + interval and syson == true){  //read the DHT humidity
    int chk = DHT.read11(DHT11_PIN);
    lastcheck = millis();
    Serial.print("DHT read = ");
    Serial.print(DHT.humidity);
    Serial.print("%");
    Serial.println(" ");
    }

  //turn on the led lights when calibration buttons are pressed
  if(calup == HIGH xor caldown == HIGH){
    digitalWrite(lcdlight, HIGH);
    calstate = true;
    displtimeon = millis();  //set display timer
    Serial.println("cal btn ACTIVE");
    if(syson == false){
      digitalWrite(lcdlight, HIGH);
      lcd.clear();
      lcd.print("   SYSTEM OFF");
      lcd.setCursor(0,1);
      lcd.print("  hold both btns");
      delay(3000);
      lcd.setCursor(0,1);
      lcd.print("  to turn on    ");
      displtimeon = millis();
    }
  }

  if(calstate == true and syson == true){  //--------DISPLAY ROUTINE
    Serial.println("printing display");
    //display variables on LCD
    lcd.setCursor(0,0);
    lcd.print("Humidity: ");
    lcd.setCursor(13,0);
    lcd.print(DHT.humidity);
    lcd.setCursor(15,0);
    lcd.print("%  ");
    lcd.setCursor(0,1);
    lcd.print("setting: ");
    lcd.setCursor(13,1);
    lcd.print(setpoint);
    lcd.setCursor(15,1);
    lcd.print("%  ");
    delay(100);

    calunlock ++;  
    //keeps calibration locked until display cycles 5 times after initially turned on
    //prevents adjustments on initial button press.
  }

//--------CALIBRATION ADJUSTMENTS
  if(calup == HIGH and caldown == LOW and calstate == true and syson == true){ 
    if(setpoint < 90 and calunlock > 5){ 
      setpoint = setpoint + 5;  //increase setpoint 
      Serial.println("adj setpoint up");
    }
    Serial.println(setpoint);
    delay(100);
    displtimeon = millis();     //reset backlight timeout
  }

  if(caldown == HIGH and calup == LOW and calstate == true and syson == true){
    if(setpoint > 10 and calunlock > 5){
      setpoint = setpoint - 5;  //decrease setpoint
      Serial.println("adj setpoint dn");
      }
    Serial.println(setpoint);
    delay(100);  
    displtimeon = millis();     //reset backlight timeout
  }

  if(millis() > displtimeon + displtime){  //-----------------BACKLIGHT TIMEOUT
    digitalWrite(lcdlight, LOW); //turn off the screen
    calstate = false;
    Serial.println("displ + backlights off");
    lcd.clear();
    calunlock = 0;  //lock calibration
  }

  if(millis() < lastcheck){
    lastcheck = millis();  //reset timers in a millisecond rollover
  }

                                      //--------SETPOINT ERROR PROCEDURE
  if(setpoint > 91 or setpoint < 9){  //in case setpoint is ever out of bounds
    Serial.println("O/B ERROR");
    digitalWrite(lcdlight, HIGH);
    lcd.clear();
    lcd.print("O/B ERROR");  //display error message on lcd
    lcd.setCursor(0,1);
    lcd.print("RESETTING");
    delay(1000);
    for(int count = 9; count >= 0; count = count - 1){
      lcd.setCursor(15,1);
      lcd.print(count);     //count down from 10
      delay(1000);
    }
    setpoint = 50;        //reset setpoint at 50.
    displtimeon = millis();
  }

  //turn on humidifier relay if below setpoint  --------RELAY CONTROL
  //RELAY MODULE IS ACTIVE LOW
  if(DHT.humidity <= setpoint - 3 and syson == true){  //if humidity is 3% lower than setpoint
    Serial.println("humidifier ON, fan OFF");  
    digitalWrite(humidifier, LOW);  //turn on humidifier
    digitalWrite(fan, HIGH);
  }
  else if(DHT.humidity >= setpoint + 3 and syson == true){  //if humidity is 3% above setpoint
    Serial.println("humidifier OFF, fan ON");
    digitalWrite(humidifier, HIGH);  //turn on fan
    digitalWrite(fan, LOW);
  }
  else{
    Serial.println("all off");  //if humidity is within 3% of setpoint
    digitalWrite(humidifier, HIGH);  //turn both off
    digitalWrite(fan, HIGH);
  }
  //delay(700);  //un-comment for serial debugging

  Serial.print("setpoint = ");
  Serial.println(setpoint);

  //delay(700);  //un-comment for serial debugging
}

/*  WIRING DIAGRAMS
 https://66.media.tumblr.com/98426b566744beacdd42bc0221092b76/tumblr_pvr0u8kvZy1yrmjh9o1_1280.png
 */

Credits

Shaeroden

Shaeroden

0 projects • 5 followers

Comments