Practical 1

Temperature-Controlled Automatic Cooling System

Auto fan control based on a temperature threshold

Aim

To design a system that measures ambient temperature with a sensor and automatically switches a fan (or any cooling device) ON or OFF according to a set threshold.

Overview

An analog temperature sensor module (NTC thermistor based, e.g. LM35 or an NTC module) outputs a voltage proportional to the room temperature. The Arduino reads this value on an analog input, converts it to degrees Celsius, and compares it with a user-defined threshold. When the temperature rises above the threshold, the controller drives a relay/transistor that powers the cooling fan. When the temperature falls back below the threshold (with a small hysteresis band to avoid rapid switching), the fan is turned OFF.

Components Required

  • Arduino Uno
  • NTC temperature sensor module (or LM35)
  • Relay module (or transistor driver, e.g. 2N2222 + diode)
  • DC fan (5V/12V)
  • Green LED + 220 ohm resistor (fan ON indicator)
  • Breadboard and jumper wires

Circuit Connections

FromToNote
Sensor VCCArduino 5V-
Sensor GNDArduino GND-
Sensor AO (analog out)Arduino A0Analog voltage 0-5V
Relay INArduino D8Drives the fan through the relay
LED anode (via 220R)Arduino D9Fan status indicator
LED cathodeArduino GND-
Fan +Relay NO contact and external supply-
Fan -Common ground-

Interactive Circuit Diagram

Simulate the circuit: adjust the sensor inputs below and watch the outputs react according to the control logic.

Loading Wokwi components...

Working

  1. Sensor continuously outputs a voltage proportional to temperature.
  2. Arduino samples the analog channel every second and smooths it by averaging a few readings.
  3. The averaged value is converted to Celsius using the sensor's transfer function.
  4. If temperature > THRESHOLD + HYST, the relay pin is driven HIGH and the fan runs.
  5. If temperature < THRESHOLD - HYST, the relay pin is driven LOW and the fan stops.
  6. The status LED mirrors the relay state so the fan state is visible even when the fan is not connected.

Control Logic

Two-level (bang-bang) control with hysteresis: FAN = ON when T > T_HI, OFF when T < T_LO. With T_HI = 30 C and T_LO = 28 C the fan cannot chatter around a single threshold. This is the same structure used in real thermostats.

Arduino Code

1// Practical 1: Temperature-Controlled Automatic Cooling System
2const int SENSOR_PIN = A0; // Analog output of the temperature sensor
3const int RELAY_PIN = 8; // Relay / driver input for the fan
4const int LED_PIN = 9; // Fan status LED
5
6const float T_HI = 30.0; // Fan turns ON above this temperature
7const float T_LO = 28.0; // Fan turns OFF below this temperature
8
9float readTemperature() {
10 // Average 10 samples for a stable reading
11 long sum = 0;
12 for (int i = 0; i < 10; i++) {
13 sum += analogRead(SENSOR_PIN);
14 delay(5);
15 }
16 float adc = sum / 10.0; // 0..1023
17 float volts = adc * 5.0 / 1023.0; // 0..5 V
18 return volts * 100.0; // LM35: 10 mV per degree C
19}
20
21void setup() {
22 pinMode(RELAY_PIN, OUTPUT);
23 pinMode(LED_PIN, OUTPUT);
24 Serial.begin(9600);
25}
26
27void loop() {
28 float tempC = readTemperature();
29 Serial.print("Temp: ");
30 Serial.print(tempC);
31 Serial.print(" C -> ");
32
33 if (tempC > T_HI) { // Hot: start cooling
34 digitalWrite(RELAY_PIN, HIGH);
35 digitalWrite(LED_PIN, HIGH);
36 Serial.println("FAN ON");
37 } else if (tempC < T_LO) { // Cooled down: stop fan
38 digitalWrite(RELAY_PIN, LOW);
39 digitalWrite(LED_PIN, LOW);
40 Serial.println("FAN OFF");
41 }
42 delay(1000);
43}