Thermistor Fan Control

Hi all,

Below is my code to control a fan turning on using a thermister. The idea is, if it's >55degC, the fan stays ON. If the the temp is < 40deg C the fan turns off. When I use a potentiometer and simulate the voltage fairly quickly it works perfectly. If I rotate the potentiometer slowly on the way down to around 3.3V, sometimes the fan never turns off. There is some fluctuation with the fan because I can hear it. The Fan is ramped up/down using PWM. Any hlp would be appreciated! Thanks.

                                   ATtiny85
                                -------u-------
            RST - A0 - (D 5) --| 1 PB5   VCC 8 |-- +5V
                               |               |
                  A3 - (D 3) --| 2 PB3   PB2 7 |-- (D 2) - A1  -->
                               |               |
  Thermister I/P  A2 - (D 4) --| 3 PB4   PB1 6 |-- (D 1) - PWM --> Fan Mosfet
                               |               |
                        Gnd ---| 4 GND   PB0 5 |-- (D 0) - PWM -->
                               -----------------
  V1.0
  25.03.22

*/

int thermisterPin = A2;
int thermisterReading;
int fanPWMPin = 1;
int pwmLevelHigh = 255;

int pwmStep = 0;

boolean fanTriggered = false; //Require a first trigger to start controlling fan

void setup() {

  pinMode (fanPWMPin, OUTPUT);

}

void loop() {

  thermisterReading = analogRead(thermisterPin);

  if (thermisterReading > 777)    // 3.8V = ~55degC 
  {
    if (pwmStep < 255)
    {
      pwmStep++;
    }
    else
    {
      ;
    }
    analogWrite (fanPWMPin, pwmStep);
    delay(7);
    fanTriggered = true;
  }

  else if ((thermisterReading > 675) && (fanTriggered == true))   //3.3V = ~40degC
  {
    analogWrite (fanPWMPin, pwmLevelHigh);
  }

  else if ((thermisterReading <= 675) && (fanTriggered == true))
  {
    if (pwmStep > 0)
    {
      pwmStep--;
      analogWrite (fanPWMPin, pwmStep);
      delay(7);
    }
    else
    {
      fanTriggered = false;
    }
  }

}

Please describe how your undocumented code works.

When the temp reaches 55degC, the fan PWM ramps up to 100%. It remains at 100% until the temp reaches 40degC. The fan then ramps down to 0% PWM.Beween 40degC and 55degC, the fan remains at 100% PWM. As I simulate the temperarure dropping around 40degC, sometimes the PWM does not ramp down and the fan remains at 100% PWM. If the transition is quick, the problem never occurs.

Add Serial.print() statements to see the thermistor readings and variable values, and check that they match your expectations.

You should add some hysteresis at the set points so that slow changes don't cause rapid on-off cycles.

The ATiny85 micro does not support the serial print function unfortunatly.

The ATtiny85 supports SoftwareSerial. Use it to solve this problem.

The only hysteresis added is the gap between 55degC and 40degC. I can't understand why it works fine with a fast transition and oddly with a very slow transistion.

The code below should get processed when the temp is <40degC. It appears to miss this out sometimes so the PWM does not ramp down at all. During the fault condition, the input voltage could even be 0V and the lines of code below are not processed.

else if ((thermisterReading <= 675) && (fanTriggered == true))
  {
    if (pwmStep > 0)
    {
      pwmStep--;
      analogWrite (fanPWMPin, pwmStep);
      delay(7);
    }
    else
    {
      fanTriggered = false;
    }

Serial.print() statements would be revealing.

Hi all,

Thanks for your hints. I loaded the code into an Arduino Micro and found the error! Then loaded it back into the ATiny85 and it works perfectly.

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