Fan Controller for grow room

Hello, I am using a sketch that I have modified to fit my needs. I am building a arduino fan controller using a 5v relay. I have a DHT22 sensor with the information displaying on an I2C 16x2. The code allows the fan to come on at or above the max temp, and shuts off at the min temp. I have put in an LED to indicate when the fan (relay) is on/off (located at bottom of sketch), but the "on/off" doesn't work correctly in this section either. My issue is displaying the on/off on the I2C. I'm wondering if there is a simple code that will change the display by simply using the state of the relay. If you run the current sketch, fan will show on when at or above max temp; the display is blank in between min and max temp; then displays off when at or below min temp. Thanx for the info and please keep it a simple as possible for me to understand.
This is the beginning of my project. I am looking to build a complete arduino controlled grow room with display. It will operate my fan, lights, pumps, etc.

#include <LiquidCrystal_I2C.h> //initialize LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define DHTPIN 12     // what pin we're connected to
int ledPin = 3;  //LED pin
int relayPIN = 9; //5v Relay

#define DHTTYPE DHT22 //enter DHT11 or DHT22

#define pwm 9 //define PIN 9 as pwm



// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);

//Tempurature Range
// Its good to provide a buffer for efficiency, if the temp bounces from 79-81 constantly
// it will turn on and off the fan constantly. So set a minimum temperature that the fan
// can cool the are too before it starts checking for max temperature.
float maxTemperature = 80;
float minTemperature = 75;

// always make sure to activate baklight
// set.cursor (0, 0 = top left / 15, 0 = top right / 0, 1 = bottom left / 15, 1 = bottom right)
void setup() {
  lcd.begin(16,2);
  lcd.backlight();
  lcd.clear();
  lcd.print("   Fan Speed  ");
  lcd.setCursor(0, 1);
  lcd.print("  Controller ");
  delay(2000);
  lcd.clear();
  analogWrite(pwm, 255);
  lcd.clear();
  Serial.begin(9600);
  dht.begin(); //start DHT sensor

   // Pin Setup/Register
  pinMode(relayPIN, OUTPUT);
  pinMode(3, OUTPUT);

}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  int h = dht.readHumidity();   //int (2 digit display (ex. 74F)), float (4 digit display (ex. 74.65F))
  float t = dht.readTemperature(true);  // Read temperature as Celsius for Farenhieght type true in ()

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
  Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("temperature: ");
  Serial.print(t);
  Serial.print(" *F ");

  lcd.clear();
  lcd.setCursor(0, 0); //set cursor location to start ()
  lcd.print("Temp:");
  lcd.print(t);   // Printing temperature on LCD
  lcd.print("F  FAN");
  

  lcd.setCursor(0, 1);
  lcd.print("Humidity:");
  lcd.print(h); // Printing humidity on LCD
  lcd.print("%");

  lcd.setCursor(13,1);
  
   // put your main code here, to run repeatedly:
    if(t >= maxTemperature)
    { lcd.print(" ON");
      powerOnRelay();
      delay(100);  } 
  else if (t <= minTemperature)
  { lcd.print("OFF");
    powerOffRelay();
    delay(100); }
 
  delay(1000); // Check tempertature every second

}

void powerOnRelay() {
    digitalWrite(relayPIN, HIGH);
    Serial.println("Relay On");
    digitalWrite(3, HIGH); //activate LED when relay on
}

void powerOffRelay() {
    digitalWrite(relayPIN, LOW);
    Serial.println("Relay Off");
    digitalWrite(3, LOW); //activate LED when relay off
    delay(1000);
}

As far as I can see through the tiny window, the logic looks plausible. Classic on/off with hysteresis.

I usually write it a bit different, but yours should be the same as

  lcd.setCursor(13,1);
   
 
  if (t  >= maxTemperature) {
    lcd.print(" ON");
    powerOnRelay();
  }

  if (t <= minTemperature) {
    lcd.print("OFF");
    powerOffRelay();
  }

If I was at my workstation I would place some serial printing about liberally to check the flow through your code and confirm the values of key variables that inform that flow.

Also, there is no problem with doing, but this logic will turn on, or off, the relay continuously while the reading is without the middle band.

So you are printing to the LCD many times " ON" or "OFF". This might explain the relay working correctly, but the display messing up.

You've asked a tangential but related question. You can certainly use the relay to hold the state.

  if (t  >= maxTemperature) {
    if (digitalRead(relayPin) == LOW) {  // is it off?
      lcd.print(" ON");
      powerOnRelay();
    }
  }

but do better, I just grabbed the digital output and messed up your nicely separated logic and action with real i/o.

Usually ppl might have a flag

bool fanIsRunning;

and maintain that in the functions that actually turn things on and off, and exploit it elsewhere to suppress copious printing or other needlessly repeated code.

HTH

a7

1 Like

thanx alto for the response.
I did the If (digitalRead(relayPIN)) code. It seemed to work, but doesn't seem to hold the on/off status on the display. It will pop up once the state of the relay changes, but then goes away as the temp changes.
I may have to explore bool code more to better my project. I'm not that savvy on the codes yet, but am learning with projects I'm building.
Is there anything in my sketch that is wrong to fix why the on/off status isn't holding on the display that you can see? I tried messing with the delay, but that didn't work.

    if(t >= maxTemperature)
    {if (digitalRead(relayPIN) == LOW)
     {lcd.print(" ON");
      powerOnRelay();
      delay(100);}} 
  else if (t <= minTemperature)
  {if (digitalRead(relayPIN) == HIGH)
  { lcd.print("OFF");
    powerOffRelay();
    delay(100);}}
 
  delay(1000); // Check tempertature every second

Do not use this function in loop( ).

Print unchanging output in setup. Clear individual spaces or overwrite locations in loop. You are clearing the "ON" "OFF" statements written at the threshold values.

Thanx cattledog, that fixed the issue.
Thank you to both.
now to add some buttons to change temp settings.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.