PWM Fan control - nano

I have an aquarium and Im using 4 pc fans to control the temp should it get too high on the hot days. Im using a nano and have found the results returned from the 10K thermister are not consistent in serial monitor. This problem makes it hard to determine when to turn the fans on and off. The other issue I have is I would like the fans to turn on %100 >545(serial result) but continue to stay on until serial result =500. Is someone out there able to help me with this code please? thanks

this is the current code Im using. really basic.

int sensorPin = A0;
int PWM = 3;
int sensorVal;
int PWMVal;

void setup() {
pinMode(sensorPin, INPUT);
pinMode(PWM, OUTPUT);
Serial.begin(9600);

}

void loop() {

Serial.println(sensorVal);
delay(1000);

sensorVal = analogRead(sensorPin);

if (sensorVal < 545) {
PWMVal = 0;
}

if(sensorVal >= 545){
PWMVal = 255;
}

analogWrite(PWM, PWMVal);

}

Start by reading the forum guidelines and post your code accordingly. Also post a schematic as to how you have wired it showing all connections and power sources. A frizzy picture is useless and may not get you a good answer. Post a link to the hardware parts you are using. Links to azon, a sales outlet, are useless. We need the voltage and current mainly of the fans. If you do not feel like doing the work needed you can search the web I have seen similar projects online.

Hi,
To add code please click this link;

if (sensorVal < 545) {

You want to turn the fan off at 500

if (sensorVal < 500) {

Does that make sense?

Tom.. :smiley: :+1: :coffee: :australia:

Hi Tom, Thanks for your replay mate.
Yeah that does make sense but heres the issue...
Aquarium Heater turns on at 24deg and off around 25.5.
What Id love to have programmed is Fans on at 26deg and off at 24deg. Fans running 100%.

yeah sorry guys. completely new here. reading what Tom has sent. Ill do my best to limit the confusion as much as possible. I appreciate the responses.

You can work out by experimentation, what analog values you get by looking at the temperature gauge in the tank.
What analog value do you get when the tank is at 26deg and at 24deg.

OR
Knowing the data sheet parameters of your thermistor, calculate the temperature reading from the analog value.
Google;

arduino 10k thermistor

Tom.. :smiley: :+1: :coffee: :australia:

Tom.. :smiley: :+1: :coffee: :australia:

This is the current code im running.

int sensorPin = A0;
int PWM = 3;
int sensorVal;
int PWMVal;

void setup() {
  
  pinMode(sensorPin, INPUT);
  pinMode(PWM, OUTPUT);
  Serial.begin(9600);

}

void loop() {
  
  Serial.println(sensorVal);
  delay(10000);

  
  sensorVal = analogRead(sensorPin);
  if (sensorVal > 545) {
    sensorVal = 545;
  }

  
  PWMVal = map(sensorVal, 500, 545, 100, 255);

  
  if (sensorVal < 500) {
    PWMVal = 0;
  }

  
  analogWrite(PWM, PWMVal);

}

Yeah thats what ive been doing. And thats a struggle on its own cus the 10K thermister isnt very consistent. Ill try figure out how to do the schem so you can see what Ive done.

I don't use thermistors but it seems like they would be worth less or even worthless if they were inconsistent.

Leading me to ask that you post a schematic of how the whole system is wired, including sources of power. Pencil drawn, shoot a picture, post it is actually the easiest.

Also tell us about or post a picture of your physical circumstances so we can see how the wiring looks as far as distances from sensor to Arduino and lead dress are concerned. There may be something you are doing (or not doing) that makes your sensors inconsistent.

The hysteresis you want to introduce is very simple.

I can't think through what your late addition of the map function is meant to accomplish.

Please confirm you only need the fan to be on or off, not speed controlled by temperature.

Here's @TomGeorge's code again, it is all the logic you need once you get you sensor problem sorted:

if (sensorVal > 545) {  // too hot?
   // do whatever it is that turns ON the fan
}

if (sensorVal < 500) {  // cooled down enough?
  // do whatever it is that turns OFF the fan.
}

(TBH I don't know if < and > shou,d be swapped in the above code. Is a higher number indicate a higher temperature? Then that code is correct.)

The code will turn ON a running fan, so what? Likewise it will turn OFF a fan that is already. Off. Again, so what?

In between, the fan will either be running trying to cool after a too hot reading, or idle, waiting for the temp to rise again above the upper temp threshold.

If thermistors don't work, or you can't make them do, there are good inexpensive alternatives for getting a temperature number (or something consistently related to temperature) into your logic.

a7

How are you using the thermistor, have you insulated it and immersed it in the tank?

You may need to do some averaging of your readings to get a more accurate and stable result.

Have you got a 0.1uF cap from sensor pin to gnd?
Or even a bigger one like 10uF.
You could be picking up noise from pumps and lamps etc.

Tom.. :smiley: :+1: :coffee: :australia:

Thanks for response Alto777.
Your 100% correct. It should be simple. I think my biggest issue is as you've stated my thermistor is inconsistant.
Im using the Map function because the thermistor makes the fan turn on 100% and off due to the inconsistency at low temp. If its running at 25% I cant hear the fans kick in.
I would like the fans to turn on at 26degC and off and 25.4degC.

In the below picture the nano is powered via usb. I cant remember the Values of R3 and R4. I think they were 100ohm and 10K. Total fan current is 72mA with a 2.5A 12V supply.
I appoligise for the schem. I hope it makes sense.

I have insulated it and it is in the tank.
No I dont have a cap on the sensor. Funny you should ask that. Cus its about 100mm away from a wave maker pump.

Schematic is very good. Missing only the pin numbers on the Nano. Also, which resistor is the thermistor?

But plenty enough to see for now, THX.

There should be a diode in parallel with the motor. Wherever you got the rest of the motor control circuit may have had one. If you thought it was not serving any purpose, a reasonable but incorrect conclusion, and omitted it, fine.

I just googled

   diode in parallel with motor

and suggest you to do the same. It won't solve your issue, but will prevent future issues.

Once you have the thermistor or temperature reading part fixed, and have added the logic in earlier post for hysteresis, you can lose the map call and just turn the fan on and off.

If you have a potentiometer value in the range 1K to 100K, you could use that instead of your thermistor/resistor voltage divider, you could test the logic without involving the thermistor and its problems. Just so you can see the hysteresis functioning.

One end to ground, the other to 5 volts and the wiper to pin the pin you use for analogRead().

a7

I managed to get this ridiculously complete and fully instrumented demo of hysteresis in action done before she arrived, before she even called to say she was rolling.

Just because I have no life like hysteresis so much and wanted to further advertize and lobby for the wokwi simulator. Free! No account needed! No pesky real life issues!

Play with the code here in the wokwi simulator.

// https://forum.arduino.cc/t/pwm-fan-control-nano/1063695
// https://wokwi.com/projects/350670924205261395

# define signal A0  // input: reading the pot
# define output 7   // output: controlling the fan, motor, whatever

# define UPPER  777 // too high, turn on
# define LOWER  222 // too low, turn off

// raw indication
# define hotLED   A1
# define coldLED  A2

bool running;

void setup() {
  Serial.begin(115200);
  Serial.print("hysteresis demo\n\n");

  pinMode(coldLED, OUTPUT);
  pinMode(hotLED, OUTPUT);
}

unsigned long loopCounter;

void loop() {
  int currentReading = analogRead(signal);

  if (currentReading > UPPER) {
    if (!running) {
      Serial.print(loopCounter);
      Serial.println("   too <hot> turn on <fan>\n");
    }
    running = true;  // may have already been
    digitalWrite(output, HIGH);
  }

  if (currentReading < LOWER) {
    if (running) {
      Serial.print(loopCounter);
      Serial.println("   <cold> enough turn off <fan>\n");
    }

    running = false;  // may have already been
    digitalWrite(output, LOW);
  }

// full speed loop available here.

  loopCounter++;

  digitalWrite(hotLED, currentReading > UPPER ? HIGH : LOW);
  digitalWrite(coldLED, currentReading < LOWER ? HIGH : LOW);
}

The frills and complete sketch context kinda hides the logic, srsly it is just as simple as this
if (signal > 777) {  // too hot?
   // do whatever it is that turns ON the fan
}

if (signal < 222) {  // cooled down enough?
  // do whatever it is that turns OFF the fan.
}

The loop counter demonstrates the free running loop that is 99.999 percent of the processor time resource availble and unencumbered.

added: I just noticed the wokwi has an NTC temperature sensor part. I leave implementing that as an exercise to whomever. :wink:

HTH

a7

Tom, your a gun. I have a much more stable serial monitor with just a .1uf cap. I dont have any 10uf caps to see if I can get it cleaner. Thanks for the suggestion. Im also going to use screened conductor for the thermistor and maybe supply the thermistor with a 7805 to eliminate possible inconsistent voltages from the nano.
Your a gem mate. Thank you again :slight_smile:

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