Smart Fan Controller Design

Smart Fan Controller Design

animation
electronics

Smart Fan Controller Design

A safe fan circuit must read temperature, switch the motor, and handle faults. Follow each path from the sensor to the load before choosing parts.

Temperature-Activated Fan Circuit

The transistor examples above give the parts. This challenge asks you to assemble them into one reviewable design: sensing, switching, protection, and code behavior.

Your Challenge: Design a circuit that automatically turns on a 12V DC fan (300mA) when temperature exceeds 30°C, using an ESP32 and LM35 temperature sensor.

Use Figure 1 to identify the sensor package. Check the pinout against the exact data sheet before wiring it, and place the sensor where it measures the air or object the fan should control. Nearby heat sources can distort the reading.

A close-up of an LM35 analog temperature-sensor integrated circuit in a three-lead package
Figure 1: The LM35 in this fan controller is a three-lead analog sensor: supply, ground, and a voltage output that changes by 10 mV per degree Celsius. Its transistor-like package makes pin identification and thermal placement part of the circuit design, not details the ESP32 code can repair later. Photo: Nevit Dilmen, CC BY-SA 3.0

Inspect the three leads visible in Figure 1: they correspond to supply, ground, and the analogue output. The caption’s 10 mV per degree Celsius links that output to the ADC calculation, while pin identification and thermal placement warn that the transistor-like package is not self-explanatory. Confirm the flat-face orientation against the datasheet and place the body in representative airflow; neither a swapped pin nor heat from the fan driver can be corrected later by ESP32 code.

Work through the design as one signal-and-power chain. First translate the LM35 voltage into a temperature threshold, then choose a switch that survives the fan’s stall current, add an inductive-current path for switch-off, and only then write the GPIO logic. The component list below is the evidence set for those linked decisions, not a menu of interchangeable parts.

Given Components:

  • Classic ESP32 example (3.3 V I/O, 12-bit ADC); assume a 12 mA GPIO design limit for this exercise and verify the exact chip, drive setting and board
  • LM35 temperature sensor (outputs 10mV/°C, e.g., 300mV @ 30°C)
  • 12V DC fan (running current: 300mA, stall current: 800mA)
  • Power supply: 12V, 1A
  • Available transistors: 2N2222 NPN BJT, IRLZ44N N-channel MOSFET
  • Resistors: assorted 1kΩ - 100kΩ
  • Diodes: 1N4001, 1N5819 Schottky

Step 1: Sensor Interface

The LM35 nominally outputs 300 mV at 30°C. ADC raw counts are not a calibrated 0–3.3 V scale. Set the attenuation for the target ESP32 and input range, use calibrated millivolts, and check accuracy against a meter.

Question: Can you connect LM35 directly to ESP32 ADC? - Hint: Check the sensor supply, output range, ADC attenuation and calibrated range for the chosen chip.

Step 2: Transistor Selection

The fan draws 300mA running, 800mA stall.

Questions:

  • 2N2222 is rated for 600mA max collector current. Is it suitable?
  • What base current does the chosen part require for saturation at the running and stall currents? Can the GPIO supply it?
  • Does the MOSFET data sheet specify on-resistance at the available gate voltage? If not, choose another part or a gate driver.

Step 3: Protection Circuit

DC motors generate inductive spikes when switched off.

Questions:

  • Which diode is better for flyback protection: 1N4001 or 1N5819 Schottky? Why?
  • Where should the flyback diode be placed (anode/cathode orientation)?

Step 4: ESP32 Code Logic

Study the conversion lines in this Arduino sketch, then modify the program to add hysteresis. You can test your solution on real ESP32 hardware or in the Wokwi online simulator (search for “ESP32 + LM35” templates).

// Fan controller challenge: verify the conversion and add hysteresis
const int tempPin = 34;  // ADC pin
const int fanPin = 25;   // GPIO output
const float lm35VoltsPerC = 0.010;

void setup() {
  pinMode(fanPin, OUTPUT);
  digitalWrite(fanPin, LOW);
  analogSetPinAttenuation(tempPin, ADC_11db);
}

void loop() {
  // Calibrated millivolts; verify attenuation and range for the target board.
  float voltage = analogReadMilliVolts(tempPin) / 1000.0;
  float tempC = voltage / lm35VoltsPerC;

  if (tempC > 30.0) {
    digitalWrite(fanPin, HIGH);  // Turn fan ON
  } else {
    digitalWrite(fanPin, LOW);   // Turn fan OFF
  }

  delay(5000);  // Check every 5 seconds
}

Your Tasks:

  1. Draw the complete circuit schematic
  2. Select BJT or MOSFET and justify your choice
  3. Calculate all resistor values
  4. Explain the ADC-to-temperature conversion in the starter code
  5. Add hysteresis to prevent fan chattering (turn ON at 30°C, turn OFF at 28°C)

Click to reveal the solution

Step 1 Solution: Sensor Interface

The LM35 needs at least 4 V, so use a suitable regulated 5 V supply. Its nominal output is 10 mV/°C, but check output extremes, ADC pin voltage limits, attenuation, and calibrated range for the exact ESP32. Check the reading against a meter and account for sensor and ADC errors.

Wiring map:

LM35 Pin / Node Connects To Note
VCC +5V from USB or regulated supply LM35 needs at least about 4V
Vout ESP32 ADC Pin 34 Output is 10mV/°C, max about 1.5V, safe for the ESP32 ADC
GND Common ground ESP32 and sensor must share ground

Step 2 Solution: Transistor Selection

Switch choice: The example fan may draw 800 mA at stall, above the assumed 600 mA rating of this 2N2222. Do not choose it from the running current alone. A BJT switch needs base current based on the data sheet’s saturated test condition, not its small-signal gain. Check the GPIO current limit and the full base drive path at stall.

For a MOSFET, select a part rated for the fan’s stall current and voltage, with an on-resistance specified at the available gate voltage. The IRLZ44N value of 0.022 ohm cannot be assumed at 3.3 V. With a suitable driver that supplies the data sheet’s specified gate voltage, an illustrative 0.022-ohm resistance gives 0.3² × 0.022 = 2.0 mW at running current and 0.8² × 0.022 = 14.1 mW at stall, before switching and temperature effects. Verify the actual part, drive and heat path. Use a gate resistor and pull-down as required by the selected circuit.

Step 3 Solution: Protection Circuit

Diode choice: Compare each diode at the expected current and temperature. Its reverse-voltage rating must cover the supply and transients, and its current and surge ratings must cover the inductive pulse. For PWM, also check recovery behavior at the actual switching conditions. A lower forward drop changes clamp voltage and current decay; it does not alone prove lower total energy or better protection.

Placement:

Component Node Connects To
Fan positive side +12V
Fan negative side MOSFET drain
1N5819 cathode Fan positive side / +12V
1N5819 anode Fan negative side / MOSFET drain
MOSFET source GND

Anode to motor-, Cathode to motor+ (or +12V rail). When motor turns off, collapsing magnetic field tries to maintain current → current flows through diode instead of spiking MOSFET.

Step 4: Circuit Design Exercise (Driver Not Yet Selected)

Subsystem Connection
Fan power +12V → DC fan positive side
Fan switch path DC fan negative side → MOSFET drain → MOSFET source → GND
Flyback protection 1N5819 across fan, cathode to +12V and anode to MOSFET drain
Temperature sensor power +5V → LM35 VCC, LM35 GND → common ground
Temperature signal LM35 Vout → ESP32 ADC Pin 34
Motor-control signal ESP32 Pin 25 → gate driver → gate resistor → MOSFET gate; choose and size the driver and resistor before building
Boot safety Gate-to-ground pull-down plus a driver whose input/enable defaults OFF; verify all power-up and reset sequences

Step 5: Example Code with Hysteresis

This sketch illustrates ADC conversion and hysteresis on a compatible classic ESP32 using the Arduino framework. The circuit remains an unfinished design exercise: the component list does not include a selected gate driver. Before driving a fan, select and specify a driver, its supply, connections, resistor values, input and enable defaults, and test power sequencing. Verify the ADC calibration and sensor range on the target board.

const int tempPin = 34, fanPin = 25;
const float onTemp = 30.0, offTemp = 28.0; // 2 °C hysteresis band

void setup() {
  pinMode(fanPin, OUTPUT);
  digitalWrite(fanPin, LOW);
  analogSetPinAttenuation(tempPin, ADC_11db);
  Serial.begin(115200);
}

void loop() {
  unsigned int millivolts = analogReadMilliVolts(tempPin);
  float voltage = millivolts / 1000.0;
  float tempC   = voltage / 0.01;          // LM35: 10 mV/°C

  static bool fan = false;
  if (tempC > onTemp && !fan)  { digitalWrite(fanPin, HIGH); fan = true;  }
  if (tempC < offTemp && fan)  { digitalWrite(fanPin, LOW);  fan = false; }

  Serial.printf("ADC %u mV | %.3fV | %.1f°C | Fan %s\n",
                millivolts, voltage, tempC, fan ? "ON" : "OFF");
  delay(5000);
}

Key Improvements in This Solution:

  1. Hysteresis: Prevents fan from rapidly cycling on/off at exactly 30°C
  2. State variable: Tracks fan state to implement hysteresis logic
  3. Flyback diode: Choose voltage, current, recovery and clamp behavior for the load; a lower clamp voltage slows current decay
  4. MOSFET choice: checked on-resistance and drive voltage, with stall and thermal margins
  5. Boot state: A gate pull-down helps, but the driver input and enable must default OFF through power-up and reset; test power sequencing
  6. Serial debug: Helps verify sensor readings and fan operation

Real-World Enhancements (try these next!): - Add PWM for variable fan speed (map temperature 25-40°C to 0-100% speed) - Use exponential smoothing to filter sensor noise: temp = 0.9*temp + 0.1*newReading - Add watchdog timer to reboot ESP32 if it hangs - Log temperature data to SD card or cloud (ThingSpeak, AWS IoT)

Key Lessons:

  1. Provide suitable inductive-energy suppression for the load and switching topology; a diode is one option
  2. Choose the switch by drive, current, voltage, switching and thermal limits
  3. Hysteresis prevents chattering in threshold-based control
  4. ADC-to-physical-unit conversion requires careful calculation
  5. Check both the gate pull-down and the driver input/enable default, then test startup and loss of power

Back to the chapter