Skip to content

Instantly share code, notes, and snippets.

@tonylegrone
Created August 27, 2019 20:07
Show Gist options
  • Save tonylegrone/17d76c834a3dde95422b3aac59b30852 to your computer and use it in GitHub Desktop.
Save tonylegrone/17d76c834a3dde95422b3aac59b30852 to your computer and use it in GitHub Desktop.
#include <OneWire.h>
#include <DallasTemperature.h>
#include <FanController.h>
#define ONE_WIRE_BUS 7
#define SENSOR_PIN 2
#define SENSOR_THRESHOLD 1000
#define PWM_PIN 9
#define GATE_PIN 4
FanController fan(SENSOR_PIN, SENSOR_THRESHOLD, PWM_PIN);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
byte dutyCycle;
int realTemp;
int limitTemp;
void setup() {
fan.begin();
pinMode(GATE_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Get temp from sensor
sensors.requestTemperatures();
realTemp = sensors.getTempFByIndex(0);
Serial.print("Temp F: ");
Serial.println(realTemp);
// Set limited temp range for dutyCycle
limitTemp = min(max(realTemp, 90), 130);
Serial.print("limit temp: ");
Serial.println(limitTemp);
// Set temp scale for PWM duty cycle
dutyCycle = map(limitTemp, 90, 130, 0, 100);
Serial.print("Current speed: ");
// Send the command to get RPM
unsigned int rpms = fan.getSpeed();
Serial.print(rpms);
Serial.println(" RPM");
// Print obtained value
Serial.print("Setting duty cycle: ");
Serial.println(dutyCycle, DEC);
// Set fan duty cycle
fan.setDutyCycle(dutyCycle);
// Get duty cycle
byte dutyCycle = fan.getDutyCycle();
Serial.print("Duty cycle: ");
Serial.println(dutyCycle, DEC);
// Control MOSFET gate to fan.
if (dutyCycle > 0) {
digitalWrite(GATE_PIN, HIGH);
}
else {
// Cuts fan power when PWM is 0%.
digitalWrite(GATE_PIN, LOW);
}
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment