From 1779d50df92622e16511effc446d34ef39096faa Mon Sep 17 00:00:00 2001 From: tolerryan <105945216+tolerryan@users.noreply.github.com> Date: Sat, 21 Nov 2026 00:02:11 -0600 Subject: [PATCH] Fan Controller project created --- Fan Controller/README.md | 46 ++++++++++ Fan Controller/firmware | 183 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 229 insertions(+) create mode 100644 Fan Controller/README.md create mode 100644 Fan Controller/firmware diff --git a/Fan Controller/README.md b/Fan Controller/README.md new file mode 100644 index 0000000..0c4a32a --- /dev/null +++ b/Fan Controller/README.md @@ -0,0 +1,46 @@ +# ESP8266 Fan Controller + +This project provides an interface for controlling a 4-Wire PC Fan with a web-based UI. It uses ESP8266WiFi and ESP8266WebServer libraries from Espressif's ESP8266 Arduino Platform. + +## Getting Started + +### Hardware + +The project is designed to work with the following hardware: + +- Wemos D1 Mini +- 4-Wire PC Fan +- 12V Supply +- 5V Supply +- PWM Wires (blue: D5, green: D6) +- Tachometer (if using) + +### Software + +The code is written in Arduino language and should be compatible with ESP8266 boards. + +## Setup + +1. Connect the fan to the Wemos D1 Mini and power pins. +2. Connect the PWM wires to the D5 and D6 pins on the Wemos D1 Mini. +3. Connect the tachometer (if using) to the D6 pin on the Wemos D1 Mini. +4. Power the Wemos D1 Mini and power pins with a 12V supply and a 5V supply. +5. Connect the Wemos D1 Mini to your computer using a USB cable. +6. Open the Arduino IDE and upload the firmware code to the Wemos D1 Mini. + +## Usage + +The project provides a web server that serves a HTML page with sliders for controlling the fan speed and a button for turning the fan off. It also provides a JSON endpoint for getting the current fan status. + +### Monitoring + +The project includes a periodic task to monitor the fan's RPM and display it in the UI. + +## License + +This project is licensed under the MIT License - see the LICENSE.md file for details + +## Acknowledgments + +- Espressif +- TheWemos team diff --git a/Fan Controller/firmware b/Fan Controller/firmware new file mode 100644 index 0000000..3513ec1 --- /dev/null +++ b/Fan Controller/firmware @@ -0,0 +1,183 @@ +/* + Wemos D1 Mini - Web-based PWM Controller for 4-Wire PC Fan + ------------------------------------------------------------ + - Hosts a webpage with a slider (0-100%) to control fan speed + - Outputs 1kHz PWM on D5 (GPIO14) -> fan Blue (PWM) wire + - Reads RPM from fan Green (Tach) wire on D6 (GPIO12) via gated 500ms polling + + Wiring: + Fan Black (GND) -> Wemos GND (and 12V supply GND - common ground!) + Fan Yellow (12V) -> 12V supply (NOT the Wemos 3V3/5V pin) + Fan Blue (PWM) -> Wemos D5 (GPIO14) + Fan Green (Tach) -> Wemos D6 (GPIO12) [optional] +*/ + +#include +#include + +// ---------- USER CONFIG ---------- +const char* ssid = "SSID"; +const char* password = "PASSWORD"; + +const int PWM_PIN = D5; // GPIO14 -> fan PWM (blue) wire +const int TACH_PIN = D6; // GPIO12 -> fan tach (green) wire +const bool USE_TACH = true; // set false if you didn't wire the tach pin + +const unsigned long RPM_INTERVAL_MS = 3000; // check RPM every 3 seconds +const unsigned long RPM_WINDOW_MS = 500; // sample tach pulses for 500ms +// ---------------------------------- + +ESP8266WebServer server(80); + +volatile unsigned long tachPulses = 0; +unsigned long lastRpmCycle = 0; +bool samplingTach = false; +int currentRPM = 0; +int currentPercent = 50; // startup duty cycle + +void ICACHE_RAM_ATTR tachISR() { + tachPulses++; +} + +const char htmlPage[] PROGMEM = R"rawliteral( + + + + + + Fan Control + + + +

PC Fan Speed Control

+
--%
+ +
+ + + +
+
RPM: --
+ + + + +)rawliteral"; + +void handleRoot() { + server.send_P(200, "text/html", htmlPage); +} + +void handleSet() { + if (server.hasArg("value")) { + int percent = server.arg("value").toInt(); + percent = constrain(percent, 0, 100); + currentPercent = percent; + int duty = map(percent, 0, 100, 0, 1023); + analogWrite(PWM_PIN, duty); + } + server.send(200, "text/plain", "OK"); +} + +void handleStatus() { + String json = "{\"percent\":" + String(currentPercent) + + ",\"rpm\":" + String(currentRPM) + "}"; + server.send(200, "application/json", json); +} + +void setup() { + Serial.begin(115200); + + pinMode(PWM_PIN, OUTPUT); + analogWriteRange(1023); // keep 10-bit (0-1023) duty cycle scale + analogWrite(PWM_PIN, map(currentPercent, 0, 100, 0, 1023)); + + if (USE_TACH) { + pinMode(TACH_PIN, INPUT_PULLUP); + } + + WiFi.mode(WIFI_STA); + WiFi.setSleepMode(WIFI_NONE_SLEEP); // keep Wi-Fi responsive for incoming HTTP requests + WiFi.begin(ssid, password); + Serial.print("Connecting to WiFi"); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(); + Serial.print("Connected! IP address: "); + Serial.println(WiFi.localIP()); + + server.on("/", handleRoot); + server.on("/set", handleSet); + server.on("/status", handleStatus); + server.begin(); + Serial.println("HTTP server started"); + + lastRpmCycle = millis(); +} + +void loop() { + server.handleClient(); + + if (USE_TACH) { + unsigned long now = millis(); + + // Start a 500ms sampling window every 3 seconds + if (!samplingTach && (now - lastRpmCycle >= RPM_INTERVAL_MS)) { + tachPulses = 0; + samplingTach = true; + lastRpmCycle = now; + attachInterrupt(digitalPinToInterrupt(TACH_PIN), tachISR, FALLING); + } + // End sampling window and calculate RPM + else if (samplingTach && (now - lastRpmCycle >= RPM_WINDOW_MS)) { + detachInterrupt(digitalPinToInterrupt(TACH_PIN)); + samplingTach = false; + + // Scale pulses from 500ms window to 60 seconds, divided by 2 pulses/rev + currentRPM = (tachPulses * 60000UL) / (RPM_WINDOW_MS * 2); + } + } +}