4 Channel PWM Arduino Fan Controller

Discussion in 'The Projects Forum' started by james211, Apr 18, 2014.

  1. james211

    Thread Starter Member

    May 29, 2012
    210
    2
  2. GopherT

    AAC Fanatic!

    Nov 23, 2012
    5,981
    3,712
    Yes, either I possible. The software will be different but both are possible.
     
  3. james211

    Thread Starter Member

    May 29, 2012
    210
    2
    So the attached schematic seems like a good option for my fans, but I haven't a clue where to begin with code?

    My goal is to have the arduino control the fan speed based on a temperature reading.

    Any ideas on where to start with code?
     
  4. james211

    Thread Starter Member

    May 29, 2012
    210
    2
    Ok, I figured out some code that works almost 100% the way I want it to with one exception, it doesn't turn off when I want it to. Right now if the temp is above 80 it turns on high, below 80 turns to half speed. But below 77 I want it to turn off and thats what I can't figure out. Can you input a temp range..any ideas?

    Here is the code:
    Code ( (Unknown Language)):
    1. #include <OneWire.h>
    2. #include <DallasTemperature.h>
    3. #define fanPin 9
    4.  
    5. int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
    6.  
    7. //Temperature chip i/o
    8. OneWire ds(DS18S20_Pin);  // on digital pin 2
    9.  
    10. void setup() {
    11.     Serial.begin(9600);
    12.     pinMode(fanPin, OUTPUT);
    13.     digitalWrite(fanPin, LOW);
    14. }
    15.  
    16. void loop(void) {
    17.   float temperature = getTemp();
    18.   float tempF = (temperature * 9.0)/ 5.0 + 32.0;
    19.   Serial.print(tempF);
    20.   if (tempF > 80) {
    21.                 Serial.print("high");
    22.         digitalWrite(fanPin, HIGH);
    23.     } else if (tempF < 80){
    24.                 Serial.print("med");
    25.         analogWrite(fanPin, 127);
    26.         } else {
    27.                 Serial.print("off");
    28.                 digitalWrite(fanPin, LOW);
    29.     }
    30.  
    31.   delay(1000); //just here to slow down the output so it is easier to read
    32.  
    33. }
    34.  
    35.  
    36. float getTemp(){
    37.   //returns the temperature from one DS18S20 in DEG Celsius
    38.  
    39.   byte data[12];
    40.   byte addr[8];
    41.  
    42.   if ( !ds.search(addr)) {
    43.       //no more sensors on chain, reset search
    44.       ds.reset_search();
    45.       return -1000;
    46.   }
    47.  
    48.   if ( OneWire::crc8( addr, 7) != addr[7]) {
    49.       Serial.println("CRC is not valid!");
    50.       return -1000;
    51.   }
    52.  
    53.   if ( addr[0] != 0x10 && addr[0] != 0x28) {
    54.       Serial.print("Device is not recognized");
    55.       return -1000;
    56.   }
    57.  
    58.   ds.reset();
    59.   ds.select(addr);
    60.   ds.write(0x44,1); // start conversion, with parasite power on at the end
    61.  
    62.   byte present = ds.reset();
    63.   ds.select(addr);    
    64.   ds.write(0xBE); // Read Scratchpad
    65.  
    66.  
    67.   for (int i = 0; i < 9; i++) { // we need 9 bytes
    68.     data[i] = ds.read();
    69.   }
    70.  
    71.   ds.reset_search();
    72.  
    73.   byte MSB = data[1];
    74.   byte LSB = data[0];
    75.  
    76.   float tempRead = ((MSB << 8) | LSB); //using two's compliment
    77.   float TemperatureSum = tempRead / 16;
    78.  
    79.   return TemperatureSum;
    80.  
    81. }[/i]
     
  5. lastRites

    New Member

    Jul 9, 2011
    13
    1
    A slight modification of the loop code snippet will get you temperature proportional fan control ;) :

    Code ( (Unknown Language)):
    1. void loop(void)
    2.  {  
    3.  float temperature = getTemp();
    4.  float tempF = (temperature * 9.0)/ 5.0 + 32.0;
    5.  Serial.print(tempF);
    6.  if (tempF<77)
    7.   {
    8.     digitalWrite(fanPin, LOW);
    9.   }
    10. else
    11.  {
    12.     tempF = map(tempF,77,80,50,255);
    13.     // speed = map(temp,leasttemp,maxtemp,leastspeed,maxspeed);
    14.     Serial.println("Speed:");
    15.     Serial.print(tempF);
    16.     analogWrite(fanPin, tempF);
    17.   }
    18. }
     
  6. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    You probably don't want to turn the fans completely off; it depends on how you're sensing the temperature.

    One common method of ambient temp sensing is to have a thermistor mounted in the intake airstream of a fan, and have the speed of the fan be a function of the thermistor's temperature. However, you can't turn the fan completely off, or the thermistor will have a very delayed reaction to whatever it is that you are trying to keep cool.
     
Loading...