using arduino + lm35 + mcp41010

hello everyone
im new to arduino so would appreciate any help
english is not my first language so please excuse any mistakes

im trying ot build a tempreture controlled fan but i dont wanna use pwm due to the noise it makes

so im using lm35 to read the temp then using mcp41010 controlling a mosfet and the speed of the fan

i have wrote a part of the code for optaining the temp my problem is i dont know how to relate it to the mcp41010 in a way that temps under 40 using a fixed value and then as temp increases the value increases too in a way that increase as 2 C increases the value of mcp41010 slowly and the max temp be 55

would appreciate if you help me with it

here is the code ive done so far:

float tempC; // create variable to store the temperature in.
int tempPin = 0; // Attach vout to analog pin 0.

void setup() // Will execute once at the start of the code.
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode (fan1, OUTPUT); // sets the fan1 pin 3 up as an output.

void loop() // code here will continue to replay nutil powered off.
tempC = analogRead(tempPin); // read the analog value from the lm35 sensor.
tempC = (5.0 * tempC * 100.0)/1024.0; // convert the analog input to temperature in centigrade.
Serial.print((byte)tempC); // send the data to the computer.

if (tempC < 40) // creates bool expression for analyzation. if it evaluates to true,
{ // the body of the if statement will execute.

digitalWrite (led, LOW); // turns off led.
digitalWrite (fan1, LOW); // turns off fan1.
if (tempC >=40 <=45) // if the if equation evaluates to false the else statement will execute.
digitalWrite (led, HOW); // turns on led.
digitalWrite (fan1, HOW); // turns on fan1.

delay(2000); // wait 2 seconds before redoing the loop.

    if (tempC >=40 <=45)                                // if the if equation evaluates to false the else

The result of comparing tempC to 40 will be true or false. Both true and false are less than 45. That is NOT how to determine if a value is in a range. There are NO shortcuts. You must do two separate comparisons.

It would appear that the map() function is what you are looking for.

thank you for the reply can you please help me with how to write the temperature range then ?

also if i use the map function wouldnt it be in real time i mean lets say the temp is at 41 and the fan ramps up then it gets to 40 and slows down and again 41 ...... i want it to be somehow steeping like when it gets to 41 it increases a bit till lets say 43 then after 43 to 45 a bit and so on not increasing or decreasing by 1 temp in linear matter

thank you for the reply can you please help me with how to write the temperature range then ?

The temperature range is the range of interesting values. If you are concerned about 40 to 60, than that is the range.

The output range is the range of values that the mcp41010 accepts.

If you meant “Could I help you correct the if statement?”, that is easy:

if(tempC >= 40 && tempC <= 45)

thanks ,

what i mean is like this

lets say i want the fan to spine at 30% for temps smaller to 40 for temps equal and greater than 40 but less than 50 the fan speed up according to the increasing temp , from the temps equal or greater than 50 the fan goes all the way up

is there anyway to writ such thing ? ofcurse ill need to change the values cause these are just examples but this is the frame that i need to work with

thank you for your time

is there anyway to writ such thing ?


  if(tempC < 40)
    analogWrite(somePin, 255* 0.3);
  else if(tempC > 50)
    analogWrite(somePin, 255);
    analogWrite(somePin, map(tempC, 40, 50, 255 * 0.3, 255));

Or simply map the temperature from the range 40 to 50 to the range 30% of 255 to 100% of 255, and then constrain the mapped value to be in the range 30% of 255 to 100% of 255, and analogWrite() the constrain()ed value to the pin/device.