Trying to control fan speed based on temperature, but fan just stays on 100%

Hi everyone. Brand new to this, so I imagine there is plenty to criticize that I’d be glad to hear. In particular, any idea why the fan just stays on 100%?

Arduino Uno R3 powered through USB port with 5V regular. PWM signal sent to Cytron 30A Motor Driver.

void setup() {
  pinMode(13, OUTPUT);      //PWM pin
  pinMode(12, OUTPUT);      //Direction pin
  digitalWrite(12, LOW);
}
void loop() {
  float v = 0;              //Voltage
  int i = 0;                //Counter
  while (i < 1000) {
    v += analogRead(A0);
    delay(10);
    i++;
  }
  v /= 20480;               //Div by 100 to avg, mult by 5/1024 to convert to voltage
  if (v < 1.01) {           //Approximately 210 degrees
    analogWrite(13, 255);
  } else if (v < 1.02) {
    analogWrite(13, 229);
  } else if (v < 1.03) {
    analogWrite(13, 204);
  } else if (v < 1.04) {
    analogWrite(13, 178);
  } else if (v < 1.05) {
    analogWrite(13, 153);
  } else if (v < 1.06) {
    analogWrite(13, 127);
  } else if (v < 1.07) {
    analogWrite(13, 102);
  } else if (v < 1.08) {
    analogWrite(13, 76);
  } else if (v < 1.09) {
    analogWrite(13, 51);
  } else if (v < 1.1) {     //Approximately 200 degrees
    analogWrite(13, 25);
  } else {
    analogWrite(13, 0);
  }
  v = 0;
}
pinMode(13, OUTPUT);      //PWM pin

What Arduino are you using that has PWM on pin 13?

Arduino PWM.

It's confirmed - I'm dumb and can't read. Thanks!

edit working great now! Next up is to fine tune the control to reduce fan cycling. This is an automotive cooling fan by the way.

Generally, setpoints should be same range as the thermostat open/close temps.

Your averaging 10seconds worth of temperature readings then setting fan speed then repeating so yea I would expect your fan to switch on and off possibly every 10 seconds..

I would expect you will get more proportional control by reducing your sample size to one and spend more code time factoring the rate of temperature change to predict how much cooling you need to bring it back down to just above thermostat closing and keeping it there.

Slumpert:
Generally, setpoints should be same range as the thermostat open/close temps.

Your averaging 10seconds worth of temperature readings then setting fan speed then repeating so yea I would expect your fan to switch on and off possibly every 10 seconds..

I would expect you will get more proportional control by reducing your sample size to one and spend more code time factoring the rate of temperature change to predict how much cooling you need to bring it back down to just above thermostat closing and keeping it there.

So in this vehicle, the OEM thermostat starts opening at 195 F and fully opens around 210 F. I'd like to narrow the range of operating temperatures a bit. Still doing some research on ideal temps for this engine (4.0 in a '96 ZJ) but I'm thinking of aiming for 205 F. It will stay well below that most of the time just due to airflow from vehicle speed (unless I get an electric water pump or a thermostat with a narrower range) but that's what I'll go for when the fan is the determining factor.

The current single temp sensor is at the radiator inlet. I'll be adding one at the radiator outlet too. First sensor should give a good approximation of current engine temps, second sensor cooling capacity of the coolant going in, and the difference will indicate how much cooling is currently being accomplished. It is made more difficult by not knowing coolant flow rate.