# Temp sensor logic

Hello all!
I am currently working on a project to control the temperature in an electronics cabinet.
Currently because of the cabinets construction it is essentially a convection oven. I would like to install some server fans to vent out the hot air and bring in fresh air.
The logic piece I am trying to solve is as follows:
If temp >= 85° fans on, until temp <75°
If it is a simple if/else the fans are likely sto switch on/off constantly as the temp will constantly hover at the set point.

Does anybody have any ideas on how to accomplish something like this?

It can be as simple as:

``````if(temp >= 85)
{
// fan on
}
else if(temp <= 75)
{
//fan off
}
``````

Fan will stay on till temp less than or equal to 75 then turn off. Fan comes back on at 85 or more. 10 degrees hysteresis.

To confirm that code works, connect a LED to pin 4 and a pot to A0 and run this simple test code. The LED (heater) should turn on at 500 and off at 200.

``````const byte heaterPin = 4;
const byte tempPin = A0;

void setup()
{
Serial.begin(115200);
pinMode(heaterPin, OUTPUT);
digitalWrite(heaterPin, LOW);
}

void loop()
{
static boolean fanState = false;
static unsigned long timer = 0;
unsigned long interval = 200;
if (millis() - timer >= interval)
{
timer = millis();

Serial.print("heater value  ");
Serial.print(temp);
Serial.print("   fan state  ");
Serial.println(fanState == 0 ? "OFF" : "ON");
if (temp >= 500)
{
fanState = HIGH;
}
else if (temp <= 200)
{
fanState = LOW;
}
digitalWrite(heaterPin, fanState);
}
}
``````

If it is a simple if/else the fans are likely sto switch on/off constantly as the temp will constantly hover at the set point.

how often would switching on/off be too much?

i wonder how quickly the temperature would drop 10 degs. wouldn't you want the temperature to vary a little as possible between 2 points.

you might consider using PWM to modulate the speed of the fan to maintain a temperature with +/- 1 deg variation, or just a duty-cycle and frequency between turning the fan on

and you could adaptively determine what the temperature switching points are to minimize variation as well as the frequency of turning the fans on

Thermal sensors themselves are very slow. Thus there is no need for any delays, as long as there is hysteresis.