Fan control in greenhouse

I am trying to control an exhaust fan in a greenhouse with an h-bridge. I'm not sure on how to keep the fan at full speed when the temperature reaches the maximum in the code. The fan reaches full speed at maximum, but when the temperature rises, the fan starts and stops intermittently. If I raise the maximum temperature in the code, the fan does not exhaust the air fast enough to keep the temperature down.
I have pasted the pertinent code below. I have also played with the map statement a bit (moved the 255 figure to 512, and down to 127). Is there something I am missing in the code? Should I approach this differently? I appreciate the help.

#define R_EN  26  //Fan enable
#define R_PWM 2  //Fan Motor

DHT dht(DHTPIN, DHTTYPE);
#define tempF1 (dht.readTemperature(true))

int tempMin = 75;
int tempMax = 95;
int fanSpeed;
const int freq = 5000;
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 50;

void setup() {

  pinMode(R_EN, OUTPUT);
  pinMode(R_PWM, OUTPUT);
  digitalWrite(R_EN, HIGH);
  // configure LED PWM functionalitites
  ledcSetup(pwmChannel, freq, resolution);

  // attach the channel to the GPIO to be controlled
  ledcAttachPin(R_PWM, pwmChannel);
}

void loop() {
  fanOn();
  currentTime = millis();
}

void fanOn() {
 if (currentTime - tempMillis > tempInterval) {
    tempMillis = currentTime;
    Serial.print(tempF1); Serial.println(" Degrees");
    if (tempF1 < tempMin) {
      fanSpeed = 0;
      digitalWrite(R_EN, LOW);
      digitalWrite(fan_LED, LOW);
      Serial.println("fan off");
      //Serial.println(tempF1);
    } else {
      //openDoor();
      if (tempF1 >= tempMin) {
        fanSpeed = map(tempF1, tempMin, tempMax, 0, 255);
        digitalWrite(R_EN, HIGH);
        digitalWrite(fan_LED, HIGH);
        ledcWrite(pwmChannel, fanSpeed);
      }
      Serial.print ("Fan Speed: "); Serial.println(fanSpeed);
      //Serial.print(tempF1); Serial.println(" Above");
    }
  }
}

Where does the replacement air come from when the exhaust is running? We had a separate outside vent that opened when the exhaust fan started.

That is a symptom of an inadequate (overheating) H-bridge or motor power supply.

Please post a wiring diagram, and links to the motor, H-bridge and the specs of the motor power supply.

  1. Add some hysteresis to the Fan ON code.
  2. Add a minimum ON to OFF time.
  3. Make sure the temperature sensor has good power decoupling.

What I ended up doing is add an 'else' statement for when the temperature went above the max statement

else {
        if (tempF1 > tempMax) {
          digitalWrite(R_EN, HIGH);
          fanSpeed = 255;
          ledcWrite(pwmChannel, 255);
        }

(This occurred to me at about 3 this morning)
I had another setup in the shop to experiment with. I found if the temperature went above the maxTemp function, the fanSpeed went above 255, and the h-bridge (ibt_2) output went down, or off, depending on the temperature. I suspected the issue might be overheating, so I had a small fan of the heat fins of the h-bridge. That did not solve the issue.
I have another esp32 opening a intake door at the other end of the greenhouse before the fan starts.

1 Like

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