Created
August 4, 2017 12:06
-
-
Save nlm/b020b6c0d49e0dc52cace199fdbf7917 to your computer and use it in GitHub Desktop.
Arduino Fan Control
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <math.h> | |
/* PWM fan control for arduino */ | |
const int rlyPin = 3; // digital 3 (optional relay) | |
const int pwmPin = 4; // digital 4 (connected to fan pwm pin) | |
const int ctrPin = A5; // analog 5 (connected to potentiometer measurement pin) | |
const int tempPin = A6; // analog 6 (connceted to temperature probe) | |
const int runCycles = 10000; // check new value every 10k cycles (2/sec) | |
const uint32_t cycleDuration = 50; // 50us for 20kHz | |
int rlyStatus = 0; // relay is off at start | |
uint32_t ctrPinVal = 0; // control pin value | |
uint32_t tempPinVal = 0; // temperature pin value | |
uint32_t cycleHighDuration = 0; // duration of the high phase of pwm cycle | |
double celcius(int RawADC) { | |
double Temp; | |
Temp = log(10000.0 * ((1024.0 / RawADC - 1))); | |
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp)) * Temp); | |
Temp = Temp - 273.15; // Convert Kelvin to Celcius | |
//Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit | |
return Temp; | |
} | |
void setup() { | |
pinMode(pwmPin, OUTPUT); | |
pinMode(rlyPin, OUTPUT); | |
pinMode(ctrPin, INPUT); | |
//pinMode(tempPin, INPUT); | |
//digitalWrite(rlyPin, HIGH); | |
//Serial.begin(9600); | |
} | |
void loop() { | |
// Measurement Cycle | |
// check the value of the control pin to compute | |
// to compute the cycle high phase duration | |
{ | |
ctrPinVal = analogRead(ctrPin); | |
cycleHighDuration = cycleDuration * ctrPinVal / 1024; | |
// tempPinVal = analogRead(tempPin); | |
// Serial.println(celcius(tempPinVal)); | |
// Serial.println(cycleHighDuration); | |
// delay(1000); | |
} | |
if (cycleHighDuration < 2) | |
{ | |
if (rlyStatus == 1) | |
{ | |
// switch the relay to open state | |
// if the potentiometer value is very low | |
digitalWrite(rlyPin, LOW); | |
rlyStatus = 0; | |
} | |
} | |
else if (rlyStatus == 0) | |
{ | |
digitalWrite(rlyPin, HIGH); | |
rlyStatus = 1; | |
} | |
// Run Control Cycles | |
if (rlyStatus == 1) | |
{ | |
for (int i = 0; i < runCycles; i++) | |
{ | |
// PWM high phase | |
digitalWrite(pwmPin, HIGH); | |
delayMicroseconds(cycleHighDuration); | |
// PWM low phase | |
digitalWrite(pwmPin, LOW); | |
delayMicroseconds(cycleDuration - cycleHighDuration); | |
} | |
} | |
else | |
{ | |
// wait if relay is open (fan off) | |
delayMicroseconds(runCycles * cycleDuration); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment