// Fan Speed Controlled by Temperature and Arduino ~ EDUCATION & TECHNOLOGY

Saturday, 4 July 2015

Fan Speed Controlled by Temperature and Arduino

I made this project because I wanted a way to automatically control the speed of a DC fan according to the temperature read by a LM35 sensor. I had a few problems with the PWM part mainly because the fan made a disturbing noise so I had to add a simple RC filter at the output of the PWM pin on the Arduino board.

Arduino Sketch
  1. #include <LiquidCrystal.h>
  2. LiquidCrystal lcd(7,6,5,4,3,2);
  3. int tempPin = A1; // the output pin of LM35
  4. int fan = 11; // the pin where fan is
  5. int led = 8; // led pin
  6. int temp;
  7. int tempMin = 30; // the temperature to start the fan
  8. int tempMax = 70; // the maximum temperature when fan is at 100%
  9. int fanSpeed;
  10. int fanLCD;
  12. void setup() {
  13. pinMode(fan, OUTPUT);
  14. pinMode(led, OUTPUT);
  15. pinMode(tempPin, INPUT);
  16. lcd.begin(16,2);
  17. }
  19. void loop() {
  20. temp = readTemp(); // get the temperature
  21. if(temp < tempMin) { // if temp is lower than minimum temp
  22. fanSpeed = 0; // fan is not spinning
  23. digitalWrite(fan, LOW);
  24. }
  25. if((temp >= tempMin) && (temp <= tempMax)) { // if temperature is higher than minimum temp
  26. fanSpeed = map(temp, tempMin, tempMax, 32, 255); // the actual speed of fan
  27. fanLCD = map(temp, tempMin, tempMax, 0, 100); // speed of fan to display on LCD
  28. analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed
  29. }
  30. if(temp > tempMax) { // if temp is higher than tempMax
  31. digitalWrite(led, HIGH); // turn on led
  32. } else { // else turn of led
  33. digitalWrite(led, LOW);
  34. }
  35. lcd.print(“TEMP: “);
  36. lcd.print(temp); // display the temperature
  37. lcd.print(“C “);
  38. lcd.setCursor(0,1); // move cursor to next line
  39. lcd.print(“FANS: “);
  40. lcd.print(fanLCD); // display the fan speed
  41. lcd.print(“%”);
  42. delay(200);
  43. lcd.clear();
  44. }
  46. int readTemp() { // get the temperature and convert it to celsius
  47. temp = analogRead(tempPin);
  48. return temp * 0.48828125;
  49. }
test setup
I used an LCD shield to display the current temperature and speed of the fan, but you can use the circuit without the LCD display. You also need to select the transistor by the type of fan that you use. In my case I used the well-known BD139 transistor and a 9V battery to provide power to the fan and transistor. The LM35 temperature sensor and red led are powered with 5V from the Arduino board.
How does the circuit works?
As you can see in the sketch on the first line I included the LiquidCrystal library (header) that includes useful functions to use when an LCD is connected to the Arduino board. Then I set the pins for the sensor, led and fan.
The most important part is to set the variables tempMin and tempMax with your desired values. tempMin is the temperature at which the fan starts to spin andtempMax is the temperature when the red led lights warning you that the maximum temp was reached. For example if you set tempMin at 30 and tempMax at 35 then the fan will start spinning at 30°C and reach its maximum speed at 35°C.
We store the temperature value in the temp variable and then use some if() functions to check if temp is lower than tempMin and if so let the fan OFF (LOW). The next if() is to check if temperature is higher than the minTemp and lower than the tempMax and if so then use the map() function to re-map the temp value from one value to another. In our case fanSpeed will have a value of 32 at tempMin and 255 at tempMax. These values are used to control the speed of the fan using PWM and the analogWrite().
The fanLCD re-maps the temp to allow the display of fanSpeed in a 0 to 100% range so you can say that the speed of the fan is directly dependent of the LM35′s temperature. When the temperature reaches the value set in tempMax the fan will be at its maximum spinning velocity and the LCD will display FANS: 100% even though the temperature might increase above tempMax.
The rest of the explanation can be read in the comments area of the Arduino sketch.


Post a Comment