Temperature-Controlled Fan Using Arduino

Temperature-Controlled Fan Using Arduino

The objective of this project is to build a temperature-controlled fan using Arduino. The fan’s speed will be automatically adjusted based on temperature readings from a temperature sensor. By using PWM (Pulse Width Modulation), the fan speed will increase or decrease depending on the temperature. This project covers how to use analog input, PWM, and if-else conditions to implement an efficient Arduino fan control system.

Fundamental Programming Concepts

  • Control Structures (if-else): Used to decide whether the fan should be turned on, off, or adjusted based on temperature.
  • Analog Input: Reads the value from the temperature sensor through the analogRead() function.
  • PWM (Pulse Width Modulation): Controls the fan’s speed by adjusting the power using analogWrite().
  • Digital Output: Sends signals to control whether the fan is on or off using digitalWrite().

Requirement Components

To complete this temperature-controlled fan Arduino project, you will need:

  • Arduino Uno Board
  • Temperature Sensor (LM35 or similar)
  • DC Fan (PWM-capable)
  • NPN Transistor (e.g., 2N2222) (to control the fan)
  • Diode (to prevent back EMF from the fan)
  • 10kΩ Resistor
  • Breadboard
  • Jumper Wires
  • USB Cable (for connecting the Arduino to your computer)

Circuit Diagram

Circuit Connection

Component Arduino Pin
Temperature Sensor (Vcc) 5V
Temperature Sensor (GND) GND
Temperature Sensor (Output) A0 (Analog Input)
Fan (Positive) Transistor Collector
Fan (Negative) GND
NPN Transistor Base Pin 9 (PWM Output)
Diode Between fan and power supply

How to Connect the Circuit

  1. Connect the temperature sensor to the A0 pin for analog input. The other pins go to 5V and GND.
  2. Connect the fan between the collector of the NPN transistor and GND.
  3. The base of the NPN transistor should be connected to Pin 9 (PWM-capable pin) on the Arduino through a 10kΩ resistor.
  4. Place a diode across the fan to prevent back EMF from damaging the transistor.

Explanation of Circuit

  • The temperature sensor reads the ambient temperature and outputs a voltage that corresponds to the current temperature.
  • The Arduino reads the sensor’s value using analog input (A0) and adjusts the fan’s speed based on the temperature. This is done through PWM signals.
  • The transistor acts as a switch to control the fan, while the diode protects against voltage spikes when the fan turns off.

Programming Section for Temperature-Controlled Fan Using Arduino

Arduino Syntax

Topic Name Syntax Explanation
Analog Read analogRead(pin) Reads analog value from the specified pin (0-1023).
Analog Write (PWM) analogWrite(pin, value) Outputs a PWM signal (0-255) to control fan speed.
Digital Write digitalWrite(pin, value) Sets a digital pin to HIGH or LOW.
If-else Condition if (condition) { } else { } Executes code based on whether a condition is true or false.

Arduino Code:

Here’s the Arduino code to control the fan based on temperature readings:

// Global variables
const int tempSensorPin = A0;  // Pin connected to the temperature sensor
const int fanPin = 9;          // PWM-capable pin connected to the transistor
int tempValue = 0;             // Variable to store the temperature reading
int fanSpeed = 0;              // Variable to store the fan speed
void setup() {
  pinMode(fanPin, OUTPUT);     // Set the fan pin as output
  Serial.begin(9600);          // Initialize serial communication for debugging
}
void loop() {
  // Read temperature sensor value (0-1023)
  tempValue = analogRead(tempSensorPin);
  // Convert the analog value to Celsius (assuming LM35 sensor)
  float voltage = tempValue * (5.0 / 1023.0);
  float temperatureC = voltage * 100.0;
  // Print temperature value to the Serial Monitor
  Serial.print("Temperature: ");
  Serial.print(temperatureC);
  Serial.println(" °C");
  // Control fan speed based on temperature thresholds
  if (temperatureC < 25) {
    fanSpeed = 0;                // Turn off the fan if temperature is below 25°C
  } else if (temperatureC >= 25 && temperatureC < 30) {
    fanSpeed = 128;              // Set fan to half speed for temperatures between 25°C and 30°C
  } else if (temperatureC >= 30) {
    fanSpeed = 255;              // Set fan to full speed if temperature exceeds 30°C
  }
  // Set the fan speed using PWM
  analogWrite(fanPin, fanSpeed);
  delay(1000);  // Wait 1 second before reading temperature again
}

Steps to Upload Code:

  1. Connect your Arduino to your computer using the USB cable.
  2. Open the Arduino IDE and select the correct Board and Port.
  3. Copy and paste the code into a new sketch.
  4. Click the Upload button to transfer the code to your Arduino.
  5. Open the Serial Monitor to view the temperature readings and see how the fan responds to changes in temperature.

Check Output

Once the code is uploaded, the fan should turn on or off based on the temperature:

  • Below 25°C: The fan should remain off.
  • Between 25°C and 30°C: The fan should run at half speed.
  • Above 30°C: The fan should run at full speed.

The temperature values will be printed in the Serial Monitor.

Troubleshooting Tips

  • Fan not turning on? Double-check the transistor connections and ensure the base is connected to a PWM-capable pin.
  • Temperature not reading correctly? Ensure the temperature sensor is connected properly, and verify the sensor’s output range.
  • No output on Serial Monitor? Verify that the baud rate in the Serial Monitor is set to 9600 and that the correct COM port is selected.

Further Exploration

  • More Temperature Ranges: Add more temperature thresholds to create a finer control over the fan speed (e.g., low, medium, high).
  • LCD Display: Display the current temperature and fan speed on an LCD instead of the Serial Monitor.
  • Control Multiple Fans: Expand the project to control multiple fans at different speeds based on varying temperature ranges.

Note

This project introduces key concepts such as analog input, PWM control, and if-else conditions in Arduino programming. These concepts are essential for building more complex Arduino-based control systems like temperature-controlled devices.

FAQ

Q1: What does analogWrite() do in Arduino?
The analogWrite() function outputs a PWM signal that simulates an analog signal, allowing you to control devices like motors or fans with varying speeds.

Q2: How does the temperature sensor work?
The temperature sensor outputs a voltage that corresponds to the ambient temperature. This value is read by the Arduino using analogRead() and converted into degrees Celsius.

Q3: What is PWM, and why is it used for controlling fan speed?
PWM (Pulse Width Modulation) allows you to control the speed of a fan by varying the amount of time the signal is on versus off. This gives more control over devices that don’t support direct analog input.

Q4: Can I add more temperature ranges for finer control of the fan speed?
Yes, you can add more if-else conditions to create additional fan speed levels based on different temperature ranges.

Q5: Can I use a relay instead of a transistor to control the fan?
Yes, you can use a relay for switching the fan on or off, but you won’t be able to control the speed unless you use a transistor or a dedicated PWM controller.