Smart Fan
Smart fan with two different modes!
Components and supplies
1
Thermistor [Tempature Sensor]
1
Jumper-Wires
1
Breadboard power supply module
1
10kΩ Resistors
1
Male to female jumper wires
1
DC motor (generic)
1
TA6586 - Motor driver chip
1
Arduino uno wifi
1
Fan Accessory, AC Motor
1
potentiometer 10K
1
BreadBoard (Full size)
1
5mm LED
1
1K resistor
Tools and machines
1
Cardboad
1
Hot Glue Gun
Apps and platforms
1
Cirkit Designer
Project description
Code
Smart Fan
cpp
This is the code.
1/* 2 This code controls a DC motor fan based on temperature readings from a thermistor. 3 It includes a manual override feature using a button. In automatic mode, the fan speed 4 is controlled according to the temperature. In manual mode, the fan runs at full speed. 5 LEDs are used to indicate the current mode (auto or manual). 6*/ 7/* 8 Board: Arduino Uno R4 9 Components: 10 - DC motor (fan) via transistor / driver 11 - Thermistor (10k NTC) 12 - Potentiometer (manual speed control) 13 - AUTO / MANUAL button 14 - ON / OFF button 15 - LEDs for AUTO and MANUAL modes 16*/ 17/* 18 Board: Arduino Uno R4 19 Components: 20 - DC motor (fan) via transistor / driver 21 - Thermistor (10k NTC) 22 - Potentiometer (manual speed control) 23 - AUTO / MANUAL button 24 - ON / OFF button 25 - LEDs for AUTO and MANUAL modes 26*/ 27 28// -------- PIN DEFINITIONS -------- 29#define MOTOR_PIN 9 30#define TEMP_SENSOR_PIN A0 31#define POT_PIN A1 32 33#define MODE_BUTTON_PIN 2 // AUTO / MANUAL toggle 34#define POWER_BUTTON_PIN 6 // ON / OFF toggle 35 36#define LED_AUTO 3 37#define LED_MANUAL 4 38 39// -------- CONSTANTS -------- 40#define TEMP_THRESHOLD 20 // °C 41#define MIN_PWM 150 // Minimum PWM so fan actually spins 42 43// -------- STATE VARIABLES -------- 44bool manualMode = false; // false = AUTO, true = MANUAL 45bool systemOn = true; // master ON / OFF 46 47void setup() { 48 pinMode(MOTOR_PIN, OUTPUT); 49 pinMode(TEMP_SENSOR_PIN, INPUT); 50 pinMode(POT_PIN, INPUT); 51 52 pinMode(MODE_BUTTON_PIN, INPUT_PULLUP); 53 pinMode(POWER_BUTTON_PIN, INPUT_PULLUP); 54 55 pinMode(LED_AUTO, OUTPUT); 56 pinMode(LED_MANUAL, OUTPUT); 57 58 Serial.begin(9600); 59} 60 61void loop() { 62 63 // -------- POWER BUTTON (ON / OFF) -------- 64 static bool lastPowerButtonState = HIGH; 65 bool powerButtonState = digitalRead(POWER_BUTTON_PIN); 66 67 if (powerButtonState == LOW && lastPowerButtonState == HIGH) { 68 systemOn = !systemOn; 69 delay(200); // debounce 70 } 71 lastPowerButtonState = powerButtonState; 72 73 // -------- MODE BUTTON (AUTO / MANUAL) -------- 74 static bool lastModeButtonState = HIGH; 75 bool modeButtonState = digitalRead(MODE_BUTTON_PIN); 76 77 if (modeButtonState == LOW && lastModeButtonState == HIGH) { 78 manualMode = !manualMode; 79 delay(200); // debounce 80 } 81 lastModeButtonState = modeButtonState; 82 83 // -------- LED STATUS -------- 84 digitalWrite(LED_AUTO, systemOn && !manualMode); 85 digitalWrite(LED_MANUAL, systemOn && manualMode); 86 87 // -------- FAN CONTROL -------- 88 int pwm = 0; 89 90 if (!systemOn) { 91 // System OFF → fan forced OFF 92 pwm = 0; 93 } 94 else if (manualMode) { 95 // MANUAL MODE → potentiometer controls speed 96 int potValue = analogRead(POT_PIN); // 0–1023 97 pwm = map(potValue, 0, 1023, 0, 255); 98 pwm = constrain(pwm, 0, 255); 99 100 Serial.print("MANUAL | PWM: "); 101 Serial.println(pwm); 102 } 103 else { 104 // AUTO MODE → temperature controls speed 105 float voltage = analogRead(TEMP_SENSOR_PIN) * 5.0 / 1023.0; 106 float temperature = voltageToTemperature(voltage); 107 108 if (temperature > TEMP_THRESHOLD) { 109 pwm = map(temperature, TEMP_THRESHOLD, 40, MIN_PWM, 255); 110 pwm = constrain(pwm, MIN_PWM, 255); 111 } else { 112 pwm = 0; 113 } 114 115 Serial.print("AUTO | Temp: "); 116 Serial.print(temperature, 2); 117 Serial.print(" C | PWM: "); 118 Serial.println(pwm); 119 } 120 121 analogWrite(MOTOR_PIN, pwm); 122 delay(300); 123} 124 125// -------- THERMISTOR FUNCTION -------- 126float voltageToTemperature(float voltage) { 127 const float R0 = 10000.0; // 10k at 25°C 128 const float B = 3950.0; 129 const float T0 = 298.15; // 25°C in Kelvin 130 const float Vcc = 5.0; 131 132 if (voltage <= 0.01) return -100; // safety 133 134 float Rt = R0 * (Vcc / voltage - 1.0); 135 float tempK = 1.0 / ((log(Rt / R0) / B) + (1.0 / T0)); 136 137 return tempK - 273.15; 138}
Comments
Only logged in users can leave comments