/* Fanduino: 2 channels DS1820 based temperature fan controller Fans are controlled by a PWM output connected on * D3 for Fan 1 (PinA) * D11 for fan 2 (PinB) Onewire lib reads is values on D10 Sample drive circuit for pwm outputs : +12V N CHANNEL FET | D11 (ex:VN67AF) ------------ | or __-----------| -fan +fan | D3 Gate / \ Drain ------------ ---\/\/\/---*---| | 12V Fan on 120ohms | \ / arduino \ -- / 10K | Source \ | / | | | GND GND DS1820 wiring : 1: GND 2: D10 on arduino. A pull up of 4K7 MUST be wired also on pin 10. 3: VCC (5V) TOP VIEW --- / \ | | ------- 1 2 3 */ #include #include int pinA = 3; // pin 3 and 11 are PWM output controled by Timer2 int pinB = 11; // connect pinA/B to H-Bridge // Data wire is plugged into port 2 on the Arduino #define ONE_WIRE_BUS 10 // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) OneWire oneWire(ONE_WIRE_BUS); // Pass our oneWire reference to Dallas Temperature. DallasTemperature sensors(&oneWire); // Compute linearization constant // Fan1 float Fan1Min=50; // PWM is between 40 and 255 for that fan (8 bit pwm) float Fan1Max=255; float Temp1Min=25.0; // relinearization will be done between 20 Celcius (pwm=Fan1Min) float Temp1Max=48.0; // to 48 celcius (pwm=Fan1Max) // Fan2 float Fan2Min=50; float Fan2Max=255; float Temp2Min=25.0; float Temp2Max=48.0; // float K1=(Fan1Max-Fan1Min)/(Temp1Max-Temp1Min); float K2=(Fan2Max-Fan2Min)/(Temp2Max-Temp2Min); void setup(){ // See wxxxxxxxxxxxxxxx //__________________________________TIMER2_for_Motor_PWM_________________________________ // set TIMER2 for PWM 31 kHz // // clear all prescaler bits in TCCR2B = the last 3 Bits // leave other bits as set by arduino init() in wiring.c byte mask = B11111000; TCCR2B &= mask; // TCCR2B is now xxxxx000 // // set CS22:20 in TCCR2B see p 156 of datasheet TCCR2B |= (0<-127.00 ) && ( T2 > -127.0) ) { // print them on serial Serial.print("Temp1: "); Serial.print(T1); Serial.print(" Temp2: "); Serial.print(T2); // normalize values between TempXMin and TempXMax if (T1 < Temp1Min) T1 = Temp1Min; if (T1 > Temp1Max) T1 = Temp1Max; if (T2 < Temp2Min) T2 = Temp2Min; if (T2 > Temp2Max) T2 = Temp2Max; // compute pwm value // when temp will be from TempXMin celcius to TempXMax, pwm will be from FanXMin to FanXMax float Pwm1=((T1-Temp1Min)*K1)+Fan1Min; float Pwm2=((T2-Temp2Min)*K2)+Fan2Min; // print pwm values on serial for debugging Serial.print (" PWM1: "); Serial.print ( Pwm1 ); Serial.print (" PWM2: "); Serial.println ( Pwm2 ); // Assign pwm analogWrite(pinA, Pwm1 ); analogWrite(pinB, Pwm2 ); // and sleep 1s until next computation delay (1000); } }