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.
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.
Fan is either fully ON or fully OFF. Simple threshold logic: if temperature > 30°C, fan ON.
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
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
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
| # | Component | Qty | Role |
|---|---|---|---|
| 1 | Arduino UNO | ×1 | Reads LM35 and controls fan via PWM |
| 2 | LM35 Temperature Sensor | ×1 | Outputs 10mV/°C — reads ambient temperature |
| 3 | DC Fan | ×1 | The cooling device being controlled |
| 4 | NPN Transistor (BC547 / 2N2222) | ×1 | Acts as a switch/driver for the high-current fan |
| 5 | Diode (1N4007) | ×1 | Protects circuit from back-EMF when fan stops |
| 6 | 10 kΩ Resistor | ×1 | Base resistor for transistor — limits base current |
| 7 | External Power Supply | ×1 | 5V/9V for fan (Arduino 5V can't power it directly) |
| 8 | Breadboard & Jumper Wires | ×1 | Clean 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
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.
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); }
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); }
analogRead(A0)— reads the LM35 voltage as 0–1023voltage * 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.
Open Temperature Fan Simulation
Pre-built circuit ready to run. Click the LM35 in Tinkercad and set temperature values to test both ON/OFF and PWM modes.
- Run simulation — set LM35 to 25°C and confirm fan is OFF, then 35°C and confirm fan is ON.
- Open Serial Monitor to read live temperature values.
- Switch to the PWM code — set temperature to 35°C and observe partial fan speed.
- Challenge: Change the threshold from 30°C to 25°C and test again.
- 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?
Q2. Why can't the Arduino directly power the DC fan without a transistor?
Q3. In PWM mode, if temperature is 35°C and map(temperature, 20, 50, 0, 255) is used, what is the fan speed value?
Q4. What does the flyback diode (1N4007) protect against?
Where Is This Used?
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.
- 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
Post a Comment