# Arduino controlled PWM PC Fan

I am looking at getting 2 Evercool EC8015HH12BP 80x10mm (4-wire) PWM fans. However, I am looking at controlling the fan speed with the arduino via the PWM pins.

I found the following diagram that seems to be what I am looking for in order to hook the fan up to the Arduino:

The code that I think I can use is this:

int pwmPin = 9;      // digital pin 9
int pwmVal = 10;

void setup()
{
pinMode(pwmPin, OUTPUT);   // sets the pin as output
Serial.begin(9600);
}

void loop()
{
if (pwmVal != 255) {
analogWrite(pwmPin, pwmVal);
//pwmVal += 10;
Serial.print(pwmVal);  // Print red value
Serial.print("\n");    // Print a tab
} else {
Serial.print('at max high');  // Print red value
Serial.print("\n");    // Print a tab
}
delay(1000);
}


I gather that the PWM would be in the range of 0-255 when writing out to it from the ardunio? I will be using the DS18B20 Thermometer Temperature Sensor in order to see how fast I need to spin the fan.

However, the fan speed (max 12V right now) never slows down one bit.

I'm using D9 on the Arduino Nano ATmega 328.

The reference is here for the board (in case i have the board pin wrong): enter link description here

Any helpful feedback would be great!

• If I may without offending, one lovely thing about the Arduino platform is that you can simply try it and see if it works. Even better, modularize your problem, and test each individual subsystem. You will learn WAY MORE about how to approach problems like this by trying them than you ever will by asking "What do you think?" Nobody should help you debug your code before you try to debug it yourself. Mar 25 '13 at 23:13
• @ScottSeidman I have updated my OP with more information on my test. Mar 25 '13 at 23:47
• That's a hair better. What does your serial out tell you pwmVal is doing? Mar 25 '13 at 23:54
• @ScottSeidman Its just set to 10 right now over and over again.. which should slow the fan down but its still spinning MAX speed. Mar 26 '13 at 0:18
• PWM frequency needs to be between 21kHz and 28kHz according to formfactors.org/developer/specs/4_Wire_PWM_Spec.pdf . I don't know how to set PWM frequency on an Arduino, but I suspect it needs to be set Mar 26 '13 at 0:27

One thing to note is that


if (pwmVal != 255) {
pwmVal += 10;
...


is not going to stop at 255, since pwmVal will be 250, then 260, e.t.c. One way to fix this would be write


if(pwmVal < 255)
{
pwmVal += 10;
}
else
{
pwmVal = 255;
}


By the way your code is written, if DEBUG is set to 0, nothing will happen (pwmVal will be set to 0). If DEBUG is set to 1, the fan speed will increase at a rate set by "wait" (which isn't defined in this code), until it gets to it's maximum. The value will also be printed to the serial port.

• pwmVal=min(pwmVal, 245); pwmVal += 10; Mar 25 '13 at 23:23
• Thanks for the reply help, gbmhunter. I have updated my OP with more information from my current test. Mar 25 '13 at 23:48

PWM frequency needs to be between 21kHz and 28kHz according to formfactors.org/developer/specs/4_Wire_PWM_Spec.pdf . I don't know how to set PWM frequency on an Arduino, but I suspect it needs to be set.

The same doc suggests that you may have reversed your sense and PWM pins (though your diagram and photo show two different things!). I can't verify this or take responsibility for your fan if its wrong.

• The PWM frequency when using the analogWrite() function is about 490Hz, and this cannot be changed. However, you can manipulate the PWM registers directly (look at the ATmega datasheet or examples online), to set the PWM frequency to basically anything you want. Mar 26 '13 at 19:43

Sorry for adding this as another answers, but I wasn't allowed to add a comment to Scott Seidman's answer and further add information to gbmhunters comment.

The PWN frequency can be controlled using the PWM registers, something which is also required if you want to use the Arduino as an IR transmitter, like a programmable remote control.

More information on the PWN registers can be found in this post, initially written by Ken Shirriff: https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM

• Hello dazo and welcome on this site. Thanks for contributing. As you may have guessed, adding an answer wasn't the best solution, and adding a comment would be a good alternative solution. But the best one is IMHO suggesting an improvement to the answer: meta.stackexchange.com/questions/76251/… Nov 19 '15 at 10:07
• Did you read my first paragraph? It said "but I wasn't allowed to add a comment" ... Otherwise, I would have done that. It seems I was missing enough "karma" to be allowed to add a comment. I also don't see any "Improve" button, and I didn't have Edit privileges either ... however, I seem to have more privileges today.
– dazo
Nov 20 '15 at 10:45