Control PWM fan speed depending on tempature

I am looking at getting 2 80x10mm 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:

Does that seem correct? I will be using the DS18B20 Thermometer Temperature Sensor in order to see how fast i need to spin the fan.

Any helpful feedback would be great! :slight_smile:

David

It looks like that would work fine. If you wanted you could also likely read the speed of the fan back using another pin on the Arduino to connect to the sense pin, but that is probably over-complicating things.

@StealthRT can you point to a source for this type fan?? I'd like to understand better...

It would be a fan like so: FrozenCPU Product Catalog

Pin 1 Black (GND), Pin 2 Yellow (PWR), Pin 3 Green (SGN), Pin 4 Blue (PWM)

I would hook the 12v (Yellow) to a power supply and ground (Black) to both the power supply and the arduino. Finally hook the PWM digital pin 9 to the PWM wire (Blue) on the fan. Do not know what to do with the signal (Green) wire?

Still looking for code to read the PWM from the fan though....

David

StealthRT:
It would be a fan like so: FrozenCPU Product Catalog

Pin 1 Black (GND), Pin 2 Yellow (PWR), Pin 3 Green (SGN), Pin 4 Blue (PWM)

I would hook the 12v (Yellow) to a power supply and ground (Black) to both the power supply and the arduino. Finally hook the PWM digital pin 7 to the PWM wire (Blue) on the fan. Do not know what to do with the signal (Green) wire?

Still looking for code to read the PWM from the fan though....

David

IIRC, the green wire is a tach signal, that you can use to determine the current speed of the fan.

Still looking for code to read the PWM from the fan though....

Surely the PWM is the signal you provide to the fan to adjust its speed?

The green wire is the tach, which you would presumably read if you need to know what the speed actually is.

Have you seen this?

I gather that the PWM would be in the range of 0-255 when writing out to it from the ardunio?

int pwmPin   = 9;   // fan PWM -> connected to digital pin 9
int pwmVal   = 0;
int DEBUG    = 1; // DEBUG counter; if set to 1, will write values back via serial

void setup()
{
  pinMode(pwmPin, OUTPUT);   // sets the pin as output

  if (DEBUG) {
    Serial.begin(9600);
  }
}

// Main program
void loop()
     analogWrite(pwmPin, pwmVal);

     if (DEBUG) { // If we want to read the output
      if (pwmVal != 255) {
         pwmVal += 10;
         Serial.print(pwmVal);  // Print red value
         Serial.print("\t");    // Print a tab
      } else {
         Serial.print('at max high');  // Print red value
         Serial.print("\t");    // Print a tab
      }
    }
    delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}

I gather that the PWM would be in the range of 0-255 when writing out to it from the ardunio?

Yes.... except that the Arduino PWM is under 500Hz, and the datasheet says it needs 25kHz PWM. The other ref I gave says it must be between 21 and 28kHz....

The datasheet shows the relationship between duty cycle and speed. PWM = 0 is 0%, 255 is 100%. As you'll see, PWM duty cycle below 30% (~80) still gives 1200rpm; above that it looks more or less linear up to full speed.

Arduino PWM can be messed with. See the http://ArduinoInfo.Info WIKI:

http://arduino-info.wikispaces.com/Arduino-PWM-Frequency

terryking228:
Arduino PWM can be messed with. See the http://ArduinoInfo.Info WIKI:

http://arduino-info.wikispaces.com/Arduino-PWM-Frequency

I looked at the sheet and i must say, i don't understand the code on there and what it is doing in order to produce the values needed to send to the PWM fan?

Mind throwing together some code for me to visualize it better?

Seems good, but consider geting some electronics to handle the current required for the fan and as for a termometer consider buying some thermistors:

mixania:
Seems good, but consider geting some electronics to handle the current required for the fan and as for a termometer consider buying some thermistors:
Thermistor 10K - SEN-00250 - SparkFun Electronics

I already have DS18B20 Thermometer Temperature Sensor as the temp sensor for it to use.

consider geting some electronics to handle the current required for the fan

The Fan current is controlled by the PWM signal. No Arduino added Arduino power control needed...

Still in need of some code to work the PWM for the fan...

David

I haven't tried it myself, but the article that terryking228 linked to lists the available PWM frequencies and I don't see any listed within the 21-28 KHz range that you're looking for.

You could try searching Nick Gammon's forum for his articles on timers - he covers this area very well and I expect you'll find a definitive answer there as to whether it's possible to achieve the frequency you need.

I don't see any listed within the 21-28 KHz range that you're looking for.

I suspect that is a 'recommended" range and I bet that any somewhat lower frequency would work. Just test the fan speed separately to start. Let us know what you find...

The recommended frequency range looks like it might have been set to make sure that the switching frequency is above the range of human hearing. This keeps the fan from making an annoying high pitched noise. If this is the reason for the recommendation you could push the frequency lower without too much issue. There could be other reasons for the recommendation, but this is a likely one. I would raise my PWM frequency as much as I could, but them just go with whatever I had.

Alright i finally got around to testing it and this is the code i am using:

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);
}

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): http://arduino.cc/en/uploads/Main/ArduinoNano30Schematic.pdf

What else am i doing incorrect?

Try setting the PWM pin to HIGH or LOW? One should turn the fan fully on, and the other should turn it off.

I hooked the incorrect pin on the fan for the PWM so now it does change speeds but when i put it on "1" its still pretty loud/fast. Its spinning about 2,040 RPMs on "1".