Code for simple temp. controlled fan isn't working

I'm just trying to get back into the hang of using arduinos and this is my first project.

Why isn't the fan turning off below the tempMin value?

Managed to get the temp reading value correct etc, just not the fan control function

Any help would be much appreciated

float temp;

int tempPin = A0; //arduino pin used for temperature sensor
int tempMin = 25; // the temperature to start the buzzer
int tempMax = 70;
int fan = 6; // the pin where fan is connected
int fanSpeed = 0;

void setup() {
pinMode(fan, OUTPUT);
pinMode(tempPin, INPUT);
Serial.begin(9600);
}

void loop() {
  temp = analogRead(tempPin)*5/1024.0;
  temp = temp - 0.5;
  temp = temp / 0.01;
Serial.print ("Temperature: ");
Serial.println(temp);
delay(1000); // delay in between reads for stability

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 the minimum range
{
fanSpeed = map(temp, tempMin, tempMax, 32, 255); // the actual speed of fan
analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed
}

}

OP's image:


Please post a hand drawn wiring diagram (not Fritzing). No one can make sense of that photo.

Do you have a motor driver? The Arduino cannot power ANY motors or servos.

Why isn't the fan turning off below the tempMin value?

We have no proof that it isn't. Your Serial output is missing from your post.

Some Serial.print() statements in the if blocks would be useful, too.

Double check your wiring/circuit, specifically the fan side.
I'm not quite sure what circuit you're trying to implement there, but whatever it is, it's not correct. You are essentially just plugging the fan into a power source.

a) use a more imaginative identifier than temp.

b) you've got a three state system and you're only providing conditional functions for two of those states, never a good idea.

c) to avoid confusion, re-examine your tests. In your first test, temp < tempMin then your second test immediately if temp >= tempMin. Well if the first fails, the second must be true. So,

if(temp < tempMin)
{
  blah, blah
}
else
{
  if (temp <= tempMax)
  {
    blah, blah, blah
  }
  else
  {
    blah, blah, blah, blah // the third and final conditional test ensuring all possibilities are covered.
  }
}

By structuring your tests like this, you can guarantee that only one of you conditional statement blocks is ever executed.

Last thing, and I'm sure you got some compiler warnings, you're testing a float against an integer, also not a very good idea.

This is the wiring diagram i was working off of

FNXGM38IA0WFW69.LARGE.jpg

Did you connect all the grounds?

You need hysteresis, otherwise the fan will be constantly switching on and then off again which is
annoying and can wear things out:

#define OFF_TEMP 24.0
#define MIN_TEMP 26.0   // a few degrees of hysteresis to prevent hunting on and off
#define MAX_TEMP 55.0

....
  float temp = get_temperature() ;
  if (temp <= OFF_TEMP)
    switch_fan_off () ;      // use lots of short well-named functions for everything
  else if (temp > MAX_TEMP)
    switch_fan_on () ;
  else if (temp > MIN_TEMP)
    set_fan_speed (map (temp, MIN_TEMP, MAX_TEMP, 32, 255)) ;
  // no need for any delays, hysteresis will do the job.
 ....

Your circuit (as per the last photo) is wrong.

This is how you have wired things up
fan_circuit_incorrect.jpg
Note: The earth from the Batteries is not actually connected to anything.
I'm surprised that the fan even turns

I've added some blue thingies to your 'photo, indicating where the wires should be moved to.


Just make sure you disconnect the red wire from the battery before making the changes.
Once the new wiring arrangement is okay, re-connect the red wire.

I am making the assumption that the NPN and temperature sensor are correctly wired.
I am also making the assumption that the stripe on the diode is closes to the red wires.

The wire marked with blue crosses should be removed.

fan_circuit_incorrect.jpg