PWM Fan Controller


I have an external Ethernet enabled hard drive that is connected to my LAN and it gets lots of use. I have noticed that after a few hours of hard use the case is quite hot to the touch. I’m sure the design is fine and the heat that is generated is within the tolerance of the drive but I wanted to give it some ventilation and some forced air cooling. This project adds a cooling feature that only functions when it’s needed.


The heart of the PWM Fan Controller is a PIC 12F675 microcontroller. This microcontroller is reading the analog output of a LM35 temperature sensor using a ADC (analog to digital converter) . The resulting digital value is converted to a temperature and a fan is powered proportionally to how hot the  sensor is. The sensor is mounted against the hard drive chassis so it is measuring the actual drive temperature and not just the air temperature inside the housing.

Video 1 of 4 – Intro and LM35 Sensor Overview

Block Operation

The system is quite simple, a temperature sensor will be connected to the input of a microcontroller. There will be two outputs, one for operating the fan and one for operating a status LED. During construction a serial output will also be used for debugging purposes.

LM35 Temperature Sensor

The LM35 temperature sensor package being used is the TO-92, it is small enough for this project and is leaded so that we can easily connect it to some wires. It is also nice since we can extend it by simply plugging it into a 0.1 inch female wire harness. This is a Celsius device and since the output is 10mV per degree it is very simple to calculate the temperature. It is even nice and simple to directly view the temperature on a multimeter as can be seen in the video.

Video 2 of 4 – Reading Data with the Microcontroller

Analog to Digital Temperature Conversion

The LM35 outputs 10mV per degree Celsius, this can be read very easily on the meter but unfortunately is isn’t quite that simple to read it within the microcontroller. The output of the LM35 is connected to an ADC (analog to digital converter) so that we can get a usable reading from it, but first we need to do a small calculation.

The analog input has a range of 0 to 5 volts and the digital resolution is 10 bits. This means that the 0 to 5 volt range will be represented by a number from 0 to 1023 in the microcontroller. If we divide 5 volts by 1024 we get the number of volts per digital increment. In this case it is 0.004883 volts per increment.  So if the voltage was 1 volt on the microcontroller pin we would expect to have a reading of around 204 as the digital number (1/0.004883).

To convert the digital value to a degrees Celsius number we need to do a bit of math. If we take the digital result and multiply it by 49 then divide it by 100 we will get a result that is very close to the correct result. The calculation looks a bit weird since we are working with integer math. With integer math the information after the decimal is lost. For example 25/3 would be 8 (not 8.333).

Here is a full example. If we have a temperature of 15 degrees Celsius the voltage out of the LM35 would be around 10mV * 15 = 0.15V we know that each 0.004883 volts is one count for the digital side of the ADC therefore 0.15V / 0.004883 V = 30.73 this would provide a value of simple 30. Finally if we use the formula 30 * 49 / 100 = 14.7 but it would be stored as 14.

Video 3 of 4 – PWM Control Setup

Fan PWM Temperature Range

The intent of this project is to keep the fan off when the drive is cool and since the drive spins down after about 5 minutes of inactivity the drive is allowed to naturally cool down. Also when the temperature is moderate the fan speed should be slow which will be as quiet as it can be. To come up with the temperature range of just running slow to running at full fan speed I used the data from the Google hard drive failure report.

Here is the table of fan speeds based on temperature that was used.

Temperature (deg C)
Fan PWM Value
35 0
36 30
37 30
38 30
39 40
40 50
41 60
42 70
43 80
44 90
45 100

There is no speed of 10% and 20% since many fans don’t like to be run with that little power. In this design 30% is the lowest amount of power used. When the fan initially gets powered up from an off state it gets about 1 second of full power to ensure it starts up properly.

Video 4 of 4 – Completion

Ventilation Holes

The air will enter an exit the enclosure through a series of holes. There were done on my V90 CNC machine but it could have just as easily been completed using a drill press.  Ensure that there is enough holes that air can easily pass but there is still some structure.

Perf Board Circuit Assembly

The final circuit was built onto a perf board. The circuit had to be kept quite small so that it would fit in the small area beside the fans in the fan enclosure. A piece of a business card was attached to the bottom of the perf board to prevent the circuit from shorting out to the metal case of the hard drive enclosure. Hot glue was used to isolate some parts of the circuit as well as providing some strain relief in other areas. Hot glue isn’t conductive which makes it perfect for these quick and dirty applications.

Cooling Fan

The cooling fan that is being used is a hard drive fan, it would normally be bolted to an internal hard drive and provide constant cooling. In this normal state it would always be running as long as the computer is on. In my case that was definitely not an option since the external drive is always powered up meaning the fan would be running 24 hours a day. The cooling is provided through a series of holes that were drilled into the the external hard drive enclosure. One of the fans was also turned around so that one fan would blow into the enclosure and the other would suck the hot air out.


The code is very simple. A temperature reading is taken and based on the table shown in the Fan PWM Temperature Range the correct PWM value is output to the fan and the indicator LED.



Click on the image for a large version of the schematic.


We have most of the items available in the online store.

Download Code

If you are interested in burning your own chip you can get the HEX file here. If you would like to have a look at the source code that is available as a free item in the online store.


152 Responses to ' PWM Fan Controller '

Subscribe to comments with RSS or TrackBack to ' PWM Fan Controller '.

  1. Alan Parekh said,

    on December 17th, 2012 at 4:46 pm

    Hi Shawn,

    That fan should be fine, you would just need to use a larger transistor since the one we use would not be able to power that size of fan.

  2. Faraaz said,

    on December 21st, 2012 at 8:47 am

    Hey Alan,

    I’m trying to build your PWM fan controller but I’m running into some problems. When I set up the circuit, the light and fan are always on. It doesn’t depend on temperature and I know the temperature sensor is working. Also where does the ground/negative of the 12 volt supply go?


  3. Syed said,

    on December 21st, 2012 at 8:57 am

    Hi Alan,

    Can a 6 V supply be used to power the chip for this project?


  4. Alan Parekh said,

    on December 21st, 2012 at 11:37 am

    Hi Faraaz,

    Use a multimeter to measure the temperature voltage being fed into the chip. Sounds like either your PIC isn’t powered or it isn’t getting the correct voltage from the temperature sensor..

    The negative of the 12 volt gets connected to the negative of the 5 volt supply. This would be done within the power supply already if you are using a computer power supply.

  5. Alan Parekh said,

    on December 21st, 2012 at 11:37 am

    Hi Syed,

    No, 6 volts will damage the PIC microcontroller.

  6. Abdullahi said,

    on December 27th, 2012 at 2:47 am

    Please which terminal program will be suitabe to run on the computer.
    Thank you.

  7. Alan Parekh said,

    on December 29th, 2012 at 7:45 am

    Hi Abdullahi,

    You can use the one that comes with windows. You will need to recompile the code to turn on the serial output. It was only used for debugging and demonstration though so it does interfere with the fan operation as currently coded.

  8. Abdullahi said,

    on January 7th, 2013 at 12:41 am

    Hi Alan,

    My temperature sensor is sensing the heat, the LED blinks and goes off when it cools . Surprisingly the fans which I have connected to a 12V dc power supply aren’t coming on. What seems to be the problem ?
    Thank you.

  9. Alan Parekh said,

    on January 8th, 2013 at 2:35 pm

    Hi Abdullahi,

    Is the positive lead of the motor connected to 12 volt positive?


  10. Nicole said,

    on January 21st, 2013 at 12:14 am

    Hi Alan,

    Thank you for this wonderful project! I tried out your circuit and it worked with one fan but I used 2N222A for the transistor since the one you used is not available. Can you help me in modifying this circuit so I could use around 4 fans? 2 fans are already not working.

    Thank you and good day!

  11. Alan Parekh said,

    on January 21st, 2013 at 12:19 am

    Hi Nicole,

    Please let me know how much current all 4 fans need. Sounds like you will just need a larger transistor to handle the current.

  12. Nicole said,

    on January 22nd, 2013 at 12:01 am

    Hi Alan,

    3 of the fans that I’m using has a current rating of .22 A and 1 is .25 A. Sorry, I’m new at this and thank you very much for your help.

  13. Jabara said,

    on February 6th, 2013 at 5:30 am

    Hi Alan,

    I’m a newbie in programming i want to work with this project because it looks so awesome and i can use it in many ways. Can you please teach me how did you make the codes in you micro controller and how did you burn it? please siree? Im begging you

  14. Jabara said,

    on February 6th, 2013 at 5:47 am

    Sir Alan, in addition i want to learn how did you make the whole program 🙁 and when i download your hex file in the above link is that it really should look like? lots of numbers? i thought it should look like a program? please help me siree

  15. Jabara said,

    on February 7th, 2013 at 7:34 pm

    Hi Alan,

    Siree, can i have a copy in of the codes in notepad? please? I’m really fascinated of your project but i dont know how to start the programming part. please help me

  16. Alan Parekh said,

    on February 8th, 2013 at 3:52 pm

    Hi Jabara,

    Sure, there is a link to the code location in the article. When you get it just open it with notepad (it is all just text).

  17. Jabara said,

    on February 9th, 2013 at 12:58 pm

    Hi Alan,

    Siree im sorry but everytime i open the download link of your hex file it only appears in numbers. Siree, can you please send me the codes copied in notepad? that is my email please siree. Please help me sir i really wanted to learn how did you program this one. Thank you. God bless!

  18. Jabara said,

    on February 10th, 2013 at 7:58 pm

    Hi Alan,

    Ow i get the codes now sorry, my bad. Thank you again. Your project really rocks man! God bless!!

  19. Jabara said,

    on February 17th, 2013 at 10:12 am

    Hi Alan,

    can i ask for a little favor? If any instance do you have the code programmed in C language? can i have a copy of that? please?

  20. Alan Parekh said,

    on February 17th, 2013 at 12:51 pm

    Hi Jabara,

    Sorry there is no C version.

  21. Abbie said,

    on February 25th, 2013 at 3:57 am

    Hi, I would just like to clear out some things regarding your project. I am using 8 fans and paralleled 8 transistors (IRF520) and the circuit works. But after I added a voltage regulator (L7805), the fans turn on for about 5-8 seconds the instance I turned on the power supply. Where can this “leakage current” possibly comes from? I’m just curious, the circuit still works perfectly. Thanks!

  22. Jabara said,

    on March 1st, 2013 at 10:50 am

    Sir Allan, can i ask a liitle favor? can you please add in your code that when the temperature reached 50degrees celcius the fan will off? please sir? kindly send me the new hex file and even the new codes at

  23. SADANAND PATIL said,

    on March 11th, 2013 at 9:40 am

    sir i like your project the same am implementing as my miniproject in this am using picaxe and ds1820 temp sensor ,please help me,and give me some referrences….

  24. Biswarup said,

    on March 24th, 2013 at 3:21 am

    Hi Alan,
    I’m really impressed with your concept. I wish to make a similar project but using op-amps in controlling the speed of the fan, and also using two seven segment displays to display the temperature.
    Can you please suggest how to display the temperature without using micro-controller.

  25. tahitiansoul said,

    on April 11th, 2013 at 1:39 pm

    Is there a possibility to use this project for controlling efan in a car (with AC, ignition on/off)?

  26. Stnmr said,

    on May 7th, 2013 at 8:56 am

    Hi Allan

    Q1: What is PWM frequency? Assuming “Fan PWM Value” (30,40,50-100%) is PWM Duty cycle.
    By Intel spec is 25 KHz.
    Q2: Probably, only way to set another temp range is to recompile you source, right?

  27. Alan Parekh said,

    on May 16th, 2013 at 5:06 am

    Yes, you would need to recompile to adjust the temperature range. Not exactly sure what the frequency is off hand, if humming or buzzing is your concern, it doesn’t make the fan motors buzz or hum.

  28. Frank said,

    on May 31st, 2013 at 4:25 pm

    Great project that I found on Youtube! Thanks for shareing your expertise!
    I am wanting to connect a PWM fan ( 4 wire) to a video card which already supports its own PWM ( 4 wire) fan. My idea was to just use the PWM line from the video card to the extra fan and use 12v and ground from a separate power source (PSU), thus not burdening the video card with powering multiple fans.
    The question is, what of the “tach”/ sensor wire of the PWM fan? The GPU will be using the tach of its own fan, but will not know of the other fan. The feedback will be incomplete. Should this work as expected?

  29. on July 24th, 2013 at 1:37 pm

    Hey, Alan….

    Thanks alot dude…i just burned your hex code and its magic for me :p…. i need this for my Inverter Box and you really helped me….

    thanks alot dude…

  30. jeremy adams said,

    on August 12th, 2013 at 5:54 pm

    love the project , thinking of doing it myself , but i need a transistor that can drive a 5 amp fan , and was wondering what transistor do you suggest that will work witht he switching output volatge of the pic chip

  31. gowriel said,

    on August 18th, 2013 at 3:55 pm

    I want a controller like this!
    How much would it cost?

  32. mark philip said,

    on November 24th, 2013 at 9:41 am

    pls hw can i use it to control an AC fan

  33. Xander said,

    on November 25th, 2013 at 2:59 am


    Great project! I’ve built this and ran into a few issues… The LM35 was soldered directly to the board. When the fan had been running legitimately ( game console in cabinet produces a lot of heat), it would never stop completely afterwards. It turns out the transistor was producing heat of it’s own and kept the fan going for sake of keeping the fan going 🙂 Took the lm35 off and connected it through some wires. Solved.

    The PWM procedure caused my fan to make quite a racket. So i replaced it with the pwm command.. shorter and makes for a way smoother fan!

    Thanks for this project, it was a fun experience.

  34. Scott said,

    on February 1st, 2014 at 12:32 am

    Hi Alan,

    Thanks for the project. I plan on adding your fan controller to a retro video game emulator that I have built. Unfortunatly I cannot find a reliable source for the LM35DZ now that its not made. Do you think that the LM35DZ/NOPB would work?

  35. Alan Parekh said,

    on February 1st, 2014 at 1:19 am

    Hi Mark,

    This circuit is only for DC motors.

  36. Alan Parekh said,

    on February 1st, 2014 at 1:19 am

    Hi gowriel,

    Depends on what you need. All of the parts are available in the store.

  37. Alan Parekh said,

    on February 1st, 2014 at 1:20 am

    Hi jeremy adams,

    Yes, but you would need to change out the transistor to a larger device. I would suggest a nice big mosfet.

  38. Alan Parekh said,

    on February 1st, 2014 at 1:21 am

    Hi tahitiansoul,

    Sorry, not sure what an efan is.

  39. Alan Parekh said,

    on February 1st, 2014 at 1:23 am

    Hi Biswarup,

    The design would be completely different, I would google it since I am sure there will be some examples like that out there.

  40. Alan Parekh said,

    on February 1st, 2014 at 1:23 am


    Sorry I have never done and Picaxe programming…

  41. Alan Parekh said,

    on February 1st, 2014 at 1:25 am

    Hi Abbie,

    You should just use a single large mosfet since the base current of all those transistors are probably too much for the PIC to output.

  42. Alan Parekh said,

    on February 1st, 2014 at 1:25 am

    Thanks Jabara!

  43. Alan Parekh said,

    on February 1st, 2014 at 1:29 am

    Hi Scott,

    Not sure but we have the regular ones back in stock now.

  44. Anonymous said,

    on February 23rd, 2014 at 8:44 am

    […] […]

  45. Theo said,

    on March 18th, 2014 at 6:14 pm

    Can you give a schema for a 1a fan,

  46. Alan Parekh said,

    on March 18th, 2014 at 11:44 pm

    Hi Theo,

    Use a mosfet that can supply 1 amp and be biased by voltage of 5 volts.

  47. Marko said,

    on July 1st, 2014 at 8:37 am

    Best PWM project. Thank you for sharing your projects with us. I like your PWM fan controller and I’d like to build one. Unfortunately I’m still very green to PIC programing and I was wondering if it is possible to increase output PWM frequency to 25kHz and increase the temperature zone between 30 °C and 45 °C. I’ve downloaded your source code and I’m not sure what to change to increase the frequency and modify the PWM value according to my temperature zone. Can you please help me. I,m willing to pay for your help. Thank you!!!

  48. Lenny said,

    on August 15th, 2014 at 6:12 am

    Hi Allan

    I have just put it togehter and put the LM35 directly on the harddrive and works very nice.
    Pic 12f675 can I use pic12f683 to I have many of these

  49. Lenny said,

    on August 19th, 2014 at 12:11 pm

    Hi Allan

    I have just made this project and it works nice, very good
    I have no more pic 12f675 but plenty of 12f629 can I use that instead

  50. Nick said,

    on October 3rd, 2014 at 3:28 pm

    Hi Alan,

    im trying to make a circuit that will increase the existing fan speeds by 50 % for my xbox 360 slim which gets really hot . any idea how to code the hex ?


    PS Great videos

Leave a reply

Search Articles

Read or sign my guestbook, the most recent entry will be shown at the bottom of each page of this web site!

Home | Store | Kits | Hard Drive Clock | Stair Lights | Rotating Display| Fish Tank Lights | Small Projects | Links | Hacked Gadgets | Contact Information | Mailing List
              (c) 2005 - 2012 Alan Parekh