Hello, I want to create a 5V fan that will be controlled by temperature using LM35. I have the code that i got it from internet and i had changed some. When I upload the code, the fan spin in constant not increase and decrease. The code is shown below :
int tempPin = A2; // the output pin of LM35
int fan = 11; // the pin where fan is
int temp;
int tempMin = 0; // the temperature to start the fan
int tempMax = 60; // the maximum temperature when fan is at 100%
int fanSpeed;
int fanLCD;
void setup() {
pinMode(fan, OUTPUT);
pinMode(tempPin, INPUT);
Serial.begin(9600);
}
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, 0, 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
}
Serial.print("TEMP: ");
Serial.print(temp); // display the temperature
Serial.print("C ");
Serial.print("\n");
Serial.print("FANS: ");
Serial.print(fanLCD); // display the fan speed
Serial.print("%");
Serial.print("\n");
delay(5000);
}
int readTemp() { // get the temperature and convert it to celsius
temp = analogRead(tempPin);
return temp * 0.48828125;
}
Please help me.