arduino temperature fan speed control

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.

Schematic of the Automatic Fan Speed Controller

arduino temperature fan speed control

Arduino Sketch

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;

void setup() {
  pinMode(fan, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(tempPin, INPUT);

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); 
   lcd.print("TEMP: ");
   lcd.print(temp);      // display the temperature
   lcd.print("C ");
   lcd.setCursor(0,1);   // move cursor to next line
   lcd.print("FANS: ");
   lcd.print(fanLCD);    // display the fan speed

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

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 and tempMax 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.

In the next project I will make a temperature protection circuit that will turn off the power of a equipment when its temperature has reached a certain value.


Related Products: Thermal Management | Fan Cooler


Join the conversation!

Error! Please fill all fields.
  • elfelivzgmail-com

    why it is that my fan starts in tempmax not temp min?

  • elfelivzgmail-com

    Can you help me guiz coz i have a problem about this project. the story goes like this ahmm my i put a settings tempmin 30 degree then max 35 then the fan start only in tempmax not the tempmin anyone help me guiz?

  • waleedahmed737gmail-com

    Hi guzy i need ur help its my school project so i have some issuse according to project its not working well i think output pin is not define and my fan speed still 0ff or 0% so plz can someone give me the right code

  • giorgos81

    THE CODE does not work, there is a plan CONNECTION;

  • dougwirelesskiwi-com

    I’m a newbie to arduino .
    On this line of code: “(temp < tempMax)) { // if temperature is higher than minimum temp ” I get the error message “exit status 1
    ‘lt’ was not declared in this scope” when I try to verify the code. What have I done wrong? What does &lt mean anyway?
    This is a great project for a ‘newbie” and I hope to expand on it when I learn more about programming arduino.

    • T.K.Hareendran

      &lt stands for less than sign,and hence you should replace the &lt with the less than sign < . Seems it's inserted in the code window by mistake!

  • contactfredrikjohnsson-se

    Great post! This inspired me a lot when doing my own version of a temperature controlled fan using an Arduino. Read about my project at


  • benc-sreckogmail-com

    I have a problem with compiling that code. i am newbie in this so i dont know how to set the code that is behind backslash.line with map function…please help

  • imalan54yahoo-com

    I think this is a dead artical. I have questions posted back to july 2016 and have had no responce from the author. I received a reply to 1 of 3 questions from another person on this site. I still can’t get this circuit to work. Help! I’ve tried using an Arduino Unio R3, Duemilanove, and Pro Mini with no luck at all. My fan will not even spin unless I hook it up to 12v directly.

  • Steve_Robbins

    Just out of curiosity, what Arduino board are you running here?

  • ae_g

    The BD139 only have a hFE of about 40, are you still able to saturate it? Guess it can run a bit hot if not. I thought of using it driving a couple of leds but i think ill add a smaller transistor to get it to work like a darlington.

Looking for the latest from TI?