DIY Thermistor Fan Control Sketch Problems

Hello.

Let me start by saying when it comes to the Arduino IDE I am a novice with just a little experience and a very basic understanding of programming structure and basic commands. Whenever I am working on a project I normally combine and modify bits of example code to use for my projects.

Let me explain what I am trying to do. I have a TIG welder that has a very loud cooling fan that runs constant. For my very light duty requirements, this is not necessary. So, I am planning on placing at least one thermistor on an internal heatsink and using the temperature to activate a relay if it goes above a threshold, which will power the fan.

My plan was to use the code I found online a while ago which converts a thermistor reading to a temperature value in the serial monitor (Thermistor_100k), and combine it with a simple if-else statement.

The confusion comes from all of the math and conversions needed in the thermistor code to convert it to a temperature. I have no idea how I would set a threshold to power a pin at or above a predetermined temperature.

Any help would be greatly appreciated and Thank You in advance! :slight_smile:

Thermistor_100k.ino (632 Bytes)

That math you are worried about is converting the analogRead() value into a temperature (T) so all you have to do is compare that variable 'T' with your setpoint using an if() statement and then react to it.

As for the fan, it probably can't be run directly from an arduino pin. Does it just have power/ground or more wires? What voltage is it operating at? 12V, 120Vac, ??? You most likely will need a relay in your circuit so the arduino turns on the relay which powers on the fan.

You'll also need to add some Hysteresis around your set point to keep the fan from cycling on and off continuously with a short period. That would end being more annoying that having it on all the time.

gfvalvo:
You’ll also need to add some Hysteresis around your set point to keep the fan from cycling on and off continuously with a short period. That would end being more annoying that having it on all the time.

Thanks for the reply gfvalvo and yes you are correct. I was thinking about how I would prevent this. My idea was just to do maybe a 5 degree spread. So it would turn on at say 90 degrees and turn off only once it fell below 85 or so. I would how to figure out how to word it in the sketch but it seemed like the simplest solution.

Draytology:
Thanks for the reply gfvalvo and yes you are correct. I was thinking about how I would prevent this. My idea was just to do maybe a 5 degree spread. So it would turn on at say 90 degrees and turn off only once it fell below 85 or so.

Exactly.

blh64: That math you are worried about is converting the analogRead() value into a temperature (T) so all you have to do is compare that variable 'T' with your setpoint using an if() statement and then react to it.

As for the fan, it probably can't be run directly from an arduino pin. Does it just have power/ground or more wires? What voltage is it operating at? 12V, 120Vac, ??? You most likely will need a relay in your circuit so the arduino turns on the relay which powers on the fan.

blh64 thanks for the reply as well. Regarding the fan, I have not yet opened up the welder to confirm but I would assume it is 120VAC. That is why I planned on using a relay, and also a MOSFET since the only relays I have are 12v to switch and 120v rated. If it is 12v-60vDC I will just use a MOSFET since I have plenty. Either way not really a concern.

That is good to know that I can use (T) as a threshold for my if statement, if I am understanding you correctly. I added an LED to my test circuit so I will try to see I have any success writing my sketch and will post it back here for expert review ;)

I have to admit I did not expect it to work on the first try but it verified just fine and I tested it with my LED and it turned on and off as expected @ 80 degrees. I did not yet add any sort of hysteresis because I wanted to first work on the foundation and then move to fine tuning, and because I don’t know how :confused:

It’s not the most elegant but attached is my sketch. Please feel free to share any advice.

Now I need to figure out how to make it turn on if above 80 but only turn off if it is below 75. Really not sure how to accomplish this at the moment so I’m also open to any input on this as well.

Thermistor_100kifelse.ino (687 Bytes)

Hi. I am having trouble modifying "Thermistor_100kifelse" so that pin 7 goes high when above 80 degrees but only goes off once below 75 degrees. If anyone knows if and how this is possible please let me know. Thank you.

if (T > threshold)
  {
    digitalWrite (led, HIGH);
  }
if(T<75)
  {
    digitalWrite (led, LOW);
  }

wildbill:

if (T > threshold)

{
    digitalWrite (led, HIGH);
  }
if(T<75)
  {
    digitalWrite (led, LOW);
  }

Wow I feel dumb lol. Thanks Wildbill that is so simple. I think my problem was I didn’t know I could compare “T” to a number, thought I had to use my threshold.

This is the workaround I just came up with:

if (T > threshold){
digitalWrite (led, HIGH);
delay(60000);
}
else digitalWrite (led,LOW);

It causes it to stop taking readings for a moment but it works I believe. I will insert your suggestion though it is much better thanks!

Draytology: I think my problem was I didn't know I could compare "T" to a number, thought I had to use my threshold.

It would be marginally better if you had HighThreshold and LowThreshold defined so that there are no magic numbers in the main code. Or you could define a number of degrees below threshold where you would turn the fan off.

wildbill: It would be marginally better if you had HighThreshold and LowThreshold defined so that there are no magic numbers in the main code. Or you could define a number of degrees below threshold where you would turn the fan off.

Excuse my ignorance I actually had to run to Google, by "magic numbers" do you mean undefined numbers?

BTW I tested your suggestion and it was exactly what I needed, thanks again. I think I will have to change some things once it is actually controlling a fan because there might be sudden, large temp swings. But, that should be able to be solved with just threshold value changes I believe.

Magic numbers are things like 75 or any literal number you have used in your code. It doesn't really matter in this case, but wherever you use such a thing it's better to define it as a named constant as you did with threshold. It avoids problems if you have used it more than once in the code and need to change it but miss one of them.

I have been messing around with my sketch and I am pretty happy with it so far. But, my TIG welder has more than one heatsink, so I have been trying to add an additional thermistor to the sketch. My problem is if one temp reading goes up the “fan” will come on but will shut back off if the second reading is below threshold2, causing it to go on and off repeatedly. I do not expect the temps to vary that much so it may not be a problem once it’s implemented in the TIG welder. But, I would like the have it so if either one goes above the threshold the fan will stay on until both are below threshold2.

If anyone has any advice on how to accomplish this I would appreciate it.

Happy Holidays!

int ThermistorPin = A0;
int ThermistorPin2 = A1;
int Vo;
float R1 = 10000;
float logR2, R2, T;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
const int led = 7;
const int threshold2 = 75;
const int threshold = 85;
void setup() {
Serial.begin(9600);
pinMode(led,OUTPUT);
}

void loop() {

Vo = analogRead(ThermistorPin);
//Vo = analogRead(ThermistorPin2);
R2 = R1 * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2logR2 + c3logR2logR2logR2));
T = T - 273.15;
T = (T * 9.0)/ 5.0 + 32.0 - 5;

Serial.print(“Temperature: “);
Serial.print(T);
Serial.println(” F”);

delay(1000);

if (T > threshold)
{
digitalWrite (led, HIGH);
}
if(T < threshold2)
{
digitalWrite (led, LOW);
}

Vo = analogRead(ThermistorPin2);
R2 = R1 * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2logR2 + c3logR2logR2logR2));
T = T - 273.15;
T = (T * 9.0)/ 5.0 + 32.0 - 5;

Serial.print(“Temperature: “);
Serial.print(T);
Serial.println(” F”);

delay(1000);

if (T > threshold)
{
digitalWrite (led, HIGH);
}
if(T < threshold2)
{
digitalWrite (led, LOW);
}

}

Thermistor_100kifelse2sensor.ino (1.21 KB)

You need to keep variables for both temperatures. You should really give your variables better (longer) names, but in keeping with your convention let’s say that you have temps T1 and T2 when you get to the end of loop. Then do this:

  if ((T1 > threshold) || (T2> threshold)
  {
    digitalWrite (led, HIGH);
  }
  if ((T1 < threshold2) && (T2 < threshold2))
  {
    digitalWrite (led, LOW);
  }

Get rid of all the other digitalWrites that impact the LED.

wildbill:
You need to keep variables for both temperatures. You should really give your variables better (longer) names, but in keeping with your convention let’s say that you have temps T1 and T2 when you get to the end of loop. Then do this:

  if ((T1 > threshold) || (T2> threshold)

{
    digitalWrite (led, HIGH);
  }
  if ((T1 < threshold2) && (T2 < threshold2))
  {
    digitalWrite (led, LOW);
  }




Get rid of all the other digitalWrites that impact the LED.

Thanks again Wildbill I learned something new once again. || and && are both new to me so that is very helpful. I briefly considered using T1 and T2. I was unsure if it would work because of this:

Vo = analogRead(ThermistorPin);
//Vo = analogRead(ThermistorPin2);
R2 = R1 * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2logR2 + c3logR2logR2logR2));
T = T - 273.15;
T = (T * 9.0)/ 5.0 + 32.0 - 5;

Serial.print(“Temperature: “);
Serial.print(T);
Serial.println(” F”);

Would I need to add or change this to include “T2”?

You have a section in your code that does a bunch of calculation based on a value that was read from ThermistorPin. Every T in that section should be T1.

Later on you have a nearly identical section that works with ThermistorPin2. Every T in that section should be T2.

Get rid of T by deleting its declaration. Then if you have failed to rename one of the uses of T, the compiler will tell you.

Given that you have two bits of code that do the same thing, at some point you might consider making a function that does the calc.