Electronic – Arduino controlled PWM PC Fan

arduinofanpwmsensortemperature

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:

enter image description here

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

enter image description here

Any helpful feedback would be great!

Best Answer

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.