PWM 4 pin Fan jupping at 100%

Very much a beginer with Arduino stuff I am fighting with something that was working fine the first time I tried. I must have messed something but after multiple checks I still can't find where.
I am trying to control a 4 pin fan using pwm on an Arduino Uno and a lm135 sensor. I got the variables right but the fan is jumping from its minimum rmp to the maximum as soon as the "actual Fan speed" is above zero but far from the maximum. I got this on the console

Actual Fan speed: 0
Temp: 29ßC 
FANS: 0% 
Actual Fan speed: 0 //Fan at minimum speed
Temp: 29ßC 
FANS: 0% 
Actual Fan speed: 32 //Fan at 100%
Temp: 30ßC 
FANS: 0% 
Actual Fan speed: 32
Temp: 30ßC 
FANS: 0% 
Actual Fan speed: 37
Temp: 31ßC 
FANS: 2% 
Actual Fan speed: 37
Temp: 31ßC 
FANS: 2% 
Actual Fan speed: 37
Temp: 31ßC 
FANS: 2% 
Actual Fan speed: 37
Temp: 31ßC 
FANS: 2% 
Actual Fan speed: 37
Temp: 31ßC 
FANS: 2% 
Actual Fan speed: 32
Temp: 30ßC 
FANS: 0% 
Actual Fan speed: 32
Temp: 30ßC 
FANS: 0% 
Actual Fan speed: 37
Temp: 31ßC 
FANS: 2%

I use this code:

#include <SoftwareSerial.h>
#include <LiquidCrystal.h>

//source: http://www.electroschematics.com/9540/arduino-fan-speed-controlled-temperature/
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int tempPin = A1;   // the output pin of LM35
int fan = 11;       // the pin where fan is
int led = 8;        // led pin
int temp;
int tempMin = 30;   // the temperature to start the fan
int tempMax = 70;   // the maximum temperature when fan is at 100%
int fanSpeed;
int fanLCD;

//source: bens at http://forum.arduino.cc/index.php/topic,10697.0.html#6
void setup() {
  // configure hardware timer2 to generate a fast PWM on OC1B (Pin 10)
  // set pin high on overflow, clear on compare match with OCR1B
  TCCR2A = 0x23;
  TCCR2B = 0x0C;  // select timer2 clock as 16 MHz I/O clock / 64 = 250 kHz
  OCR2A = 249;  // top/overflow value is 249 => produces a 1000 Hz PWM
  OCR1B = 125;  // set the PWM to 50% duty cycle
  // open the serial port at 9600 bps for debuging
  Serial.begin(9600);
  pinMode(fan, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(tempPin, INPUT);

  lcd.begin(16, 2);
}

void loop() {
  temp = readTemp();     // get the temperature
  if (temp < tempMin) {  // if temp is lower than minimum temp
    fanSpeed = 0;      // fan is not spinning
    digitalWrite(fan, LOW);
  }
  if ((temp >= tempMin) && (temp <= tempMax)) { // if temperature is higher than minimum temp
    fanSpeed = map(temp, tempMin, tempMax, 32, 255); // the actual speed of fan
    fanLCD = map(temp, tempMin, tempMax, 0, 100);  // speed of fan to display on LCD
    analogWrite(fan, fanSpeed);  // spin the fan at the fanSpeed speed
  }

  if (temp > tempMax) {       // if temp is higher than tempMax
    digitalWrite(led, HIGH);  // turn on led
  } else {                    // else turn of led
    digitalWrite(led, LOW);
  }
  Serial.print("Actual Fan speed: ");
  Serial.println(fanSpeed); 
  Serial.print("Temp: ");
  Serial.print(temp);
  Serial.print((char)223);
  Serial.println("C ");
  Serial.print("FANS: ");
  Serial.print(fanLCD);
  Serial.println("% ");
    
  lcd.print("TEMP: ");
  lcd.print(temp);      // display the temperature
  lcd.print((char)223);
  lcd.print("C ");
  lcd.setCursor(0, 1);  // move cursor to next line
  lcd.print("FANS: ");
  lcd.print(fanLCD);    // display the fan speed
  lcd.print("% ");
  delay(500);
  lcd.clear();
}

int readTemp() {  // get the temperature and convert it to celsius
  temp = analogRead(tempPin);
  return temp * 0.48828125;
}

The fan is always sniping even at zero %, normal as far I understand, 20% is the very minium speed for this kind of fan

Schematic looks like attached (without the Fan sens wiring)
Any help welcome
Thanks

Schematic looks like attached (without the Fan sens wiring)

The schematic looks nothing like that. That is the bread board view, it said so in the Fritzing tab. There is also a schematic tab, why do you think the bread board tab is the same as the schematic tab.

The bread board view is totally useless for anything but the most trivial of circuits.
Please read this:-
How to use this forum

how is the 12v connected to the fan?
is it sourcing 12v (PNP or P channel mosfet) or sinking 12v (NPN or N channel mosfet)?

I find that you must source in order to get a stable rpm control of the fan.

this is how I wire it

also fan spec recommends pwm frequency to be 21 to 28khz so you do not hear an audible whine.

Thanks doughboy,
I attached how it is connected, simpler than your version. I am not using the Sens pin 3 of the fan right know, (I tried but the pulsein returns me 0) have to fix the pwm first. The board is powered by the USB but I also tried the 12V input. The wiring should be ok, as mentioned it was working the first time I did it with a good progressive control of the fan. I should I messed something, probably in the code but only probably. I really reviewed everything, certainly a very simple mistake but I don't get it.

Frustrating but still fun learning this stuff

Grumpy_Mike:
The schematic looks nothing like that. That is the bread board view, it said so in the Fritzing tab. There is also a schematic tab, why do you think the bread board tab is the same as the schematic tab.

The bread board view is totally useless for anything but the most trivial of circuits.
Please read this:-
How to use this forum

Thanks for your friendly and constructive note Grumpy_Mike, I read the "How to use this forum" including the last paragraphe of the section 13.

fan will run at full speed if pwm is disconnected or floating. that is by design. So perhaps your circuit might have a loose connection.

also, fan will not completely shutdown, even if pwm pin is 0v. Again, that is by design. So if you really want the fan to completely shut down, you must add a circuit to turn off the 12v to the fan.

I read the "How to use this forum" including the last paragraphe of the section 13.

Pity that you decided to ignore it then isn't it.

doughboy:
fan will run at full speed if pwm is disconnected or floating. that is by design. So perhaps your circuit might have a loose connection.

Yes I saw that disconnecting the pwm pin will have the fan running full speed, but the fan only runs full speed when the signal change on the pwn pin, I wonder what can provoque such thing, wrong pulse frequency?

also, fan will not completely shutdown, even if pwm pin is 0v. Again, that is by design. So if you really want the fan to completely shut down, you must add a circuit to turn off the 12v to the fan.

I notice that too, no issue, if powered I will need cooling even a minimum, I am in Singapore not in North Pole :wink:

Thanks!

Grumpy_Mike:
Pity that you decided to ignore it then isn't it.

We are pitiful, but don't take your nickname too literally or me too seriously. Subject close