Temperature Controlled Fan Using Arduino Uno (Step-by-Step Guide)

Temperature Controlled Fan
MakeMindz Summer Class · Intermediate

Temperature
Controlled Fan

Build a smart fan that reads room temperature and automatically turns on — or speeds up — when it gets too hot. Real automation, real sensors.

⏱ ~50 min 🎯 Intermediate 🌡️ LM35 Sensor ⚡ PWM Speed Control 🌐 Tinkercad
28°C
🟢 FAN ON

What Are We Building?

This project uses an LM35 temperature sensor to measure ambient temperature. When the temperature exceeds a set threshold (30°C), the Arduino automatically turns on a DC fan via a transistor. In the advanced PWM version, the fan speed continuously increases as temperature rises — no fixed threshold needed. This is exactly how computer cooling systems work.

🏋 What you'll learn:
🌡️Reading an analog temperature sensor (LM35)
🔢ADC voltage-to-temperature conversion
Transistor switching for high-current loads
🔁PWM fan speed control with analogWrite()
▶ Mode 1: ON/OFF Control

Fan is either fully ON or fully OFF. Simple threshold logic: if temperature > 30°C, fan ON.

▶ Mode 2: PWM Speed Control

Fan speed increases proportionally with temperature. 20°C→0%, 50°C→100% speed.

How the LM35 Works

The LM35 outputs a voltage directly proportional to temperature — 10 mV per °C. No calibration required.

🔢 Converting Sensor Reading to Temperature

// Step 1: Read analog value (0–1023)
int sensorValue = analogRead(A0);

// Step 2: Convert ADC value to voltage
float voltage = sensorValue * (5.0 / 1023.0);

// Step 3: Convert voltage to °C (LM35: 10mV/°C)
float temperature = voltage * 100;

// Example: reading=614 → voltage=3.0V → temp=30°C
❄ 0°C
0V out
Freezing
🌞 25°C
0.25V out
Room temp
☀️ 30°C
0.30V out
Fan ON
🔥 50°C
0.50V out
Max speed

The LM35 outputs exactly 10 mV for every 1°C. At 30°C it outputs 300mV = 0.3V. The Arduino's ADC reads this as approximately 0.3 / 5.0 × 1023 ≈ 61.

What You Need

#ComponentQtyRole
1Arduino UNO×1Reads LM35 and controls fan via PWM
2LM35 Temperature Sensor×1Outputs 10mV/°C — reads ambient temperature
3DC Fan×1The cooling device being controlled
4NPN Transistor (BC547 / 2N2222)×1Acts as a switch/driver for the high-current fan
5Diode (1N4007)×1Protects circuit from back-EMF when fan stops
610 kΩ Resistor×1Base resistor for transistor — limits base current
7External Power Supply×15V/9V for fan (Arduino 5V can't power it directly)
8Breadboard & Jumper Wires×1Clean circuit assembly

Why a transistor? Arduino digital pins can only supply ~40mA. A DC fan may draw 200mA+. The transistor (BC547/2N2222) acts as an electronically-controlled switch — the Arduino's 40mA signal controls the transistor gate, which then switches a much larger current from the external supply to the fan.

Drag the Temperature Slider

See how the fan responds in both ON/OFF mode and PWM speed control mode. Watch the sensor readout values update in real time.

Temperature Controlled Fan — Live Simulation

— OFF
PWM: 0 / 255
25
°C
🟢 COOL
15°CCold → Hot55°C
analogRead
307
Voltage
1.50V
PWM Out
0
Fan turns ON above 30°C, fully OFF below.

Two Versions

Choose the mode that fits your project. Both use the same LM35 reading logic — they differ only in how they control the fan.

fan_onoff.ino — Simple threshold control
int sensorPin = A0;   // LM35 output pin
int fanPin    = 9;    // Fan control via transistor

float temperature;

void setup() {
  pinMode(fanPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {

  // ── Read LM35 ──────────────────────────────
  int   sensorValue = analogRead(sensorPin);
  float voltage     = sensorValue * (5.0 / 1023.0);
  temperature         = voltage * 100;   // 10mV per °C

  Serial.print("Temp: ");
  Serial.println(temperature);

  // ── Control fan ────────────────────────────
  if (temperature > 30) {
    digitalWrite(fanPin, HIGH);   // Fan ON
  } else {
    digitalWrite(fanPin, LOW);    // Fan OFF
  }

  delay(1000);
}
fan_pwm.ino — Variable speed with analogWrite()
int sensorPin = A0;
int fanPin    = 9;   // Must be a PWM pin (~)

void setup() {
  pinMode(fanPin, OUTPUT);
}

void loop() {

  // ── Read LM35 ──────────────────────────────
  int   sensorValue = analogRead(sensorPin);
  float voltage     = sensorValue * (5.0 / 1023.0);
  float temperature  = voltage * 100;

  // ── Map temp (20–50°C) to PWM (0–255) ─────
  int speed = map(temperature, 20, 50, 0, 255);

  // ── Clamp to valid 0–255 range ─────────────
  speed = constrain(speed, 0, 255);

  // ── Set fan speed ──────────────────────────
  analogWrite(fanPin, speed);   // 0=OFF, 255=FULL

  delay(1000);
}
💡 Key functions explained:
  • analogRead(A0) — reads the LM35 voltage as 0–1023
  • voltage * 100 — converts 0.30V to 30°C (since LM35 outputs 10mV/°C)
  • analogWrite(pin, val) — outputs PWM signal (0=0%, 255=100% duty cycle)
  • constrain(val, 0, 255) — prevents map() from returning values outside the PWM range

Simulate in Tinkercad

The complete circuit with LM35, transistor, diode, and fan is pre-wired. Start simulation and adjust the LM35 temperature to watch the fan respond.

🧪 Try these challenges:
  1. Run simulation — set LM35 to 25°C and confirm fan is OFF, then 35°C and confirm fan is ON.
  2. Open Serial Monitor to read live temperature values.
  3. Switch to the PWM code — set temperature to 35°C and observe partial fan speed.
  4. Challenge: Change the threshold from 30°C to 25°C and test again.
  5. Bonus: Add an LED that lights up whenever the fan is running.

Quick Quiz

Click an option to check your answer instantly.

Q1. The LM35 sensor outputs 10mV per °C. What voltage does it output at 45°C?

0.30V
0.45V
0.55V

Q2. Why can't the Arduino directly power the DC fan without a transistor?

The voltage is too high
Arduino pins can only supply ~40mA but the fan needs much more
Arduino doesn't support PWM

Q3. In PWM mode, if temperature is 35°C and map(temperature, 20, 50, 0, 255) is used, what is the fan speed value?

127
255
0
~127 (about 50% speed)

Q4. What does the flyback diode (1N4007) protect against?

Temperature spikes from the LM35
Reversed power supply polarity
Back-EMF voltage spike when the fan motor stops

Where Is This Used?

💻CPU CoolingAuto fan speed in laptops and desktops
🏠Smart HomeAutomatic room ventilation system
🏭IndustrialTemperature control in server rooms
🌿GreenhouseAuto-ventilation for plant climate
Energy SavingFan only runs when needed
🔌IoT ClimateRemote temperature monitoring
Fan doesn't spin at all

Check transistor wiring — Base to Pin 9 (via 10kΩ), Collector to fan negative, Emitter to GND. Also verify external power supply is connected and GND is shared with Arduino.

🌡️
Temperature reads 0°C or very wrong value

Check LM35 orientation — flat face forward with VCC on left, GND on right, OUT in the middle. Reversed connections will give wrong or no readings.

Arduino resets when fan turns on

The fan is drawing too much current from the Arduino 5V pin. Use an external 5V/9V supply for the fan and only share GND with the Arduino.

🚀 Ready to level up?
  • Add a 16×2 LCD to display the live temperature and fan speed
  • Add a buzzer alert when temperature exceeds a critical level (e.g. 45°C)
  • Add a manual override button to force the fan ON regardless of temperature
  • Use a DHT11 sensor instead to also measure humidity
  • Add Wi-Fi (ESP8266) to monitor temperature remotely from a web browser

Comments

try for free