School Project: Temperature Controlled fan using Arduino in Proteus

 Temperature Controlled fan using Arduino in Proteus 

Below is an Arduino code for a temperature-controlled fan using an analog temperature sensor (e.g., LM35) connected to an analog pin. The fan speed is controlled using PWM output. This code is suitable for simulation in Proteus.


Arduino Code:

Copy the code and Paste:

// Temperature-Controlled Fan Using Arduino

// Components: LM35 (Temperature Sensor), DC Fan, Arduino UNO, and NPN Transistor


// Pin configuration

const int tempPin = A0;       // Analog pin connected to LM35

const int fanPin = 9;         // PWM pin connected to the fan (via transistor)


// Temperature thresholds

const float tempMin = 25.0;   // Minimum temperature (in Celsius) for the fan to start

const float tempMax = 40.0;   // Maximum temperature (in Celsius) for full fan speed


void setup() {

  pinMode(fanPin, OUTPUT);    // Set the fan pin as an output

  Serial.begin(9600);         // Initialize serial communication for debugging

}


void loop() {

  // Read temperature from LM35

  int sensorValue = analogRead(tempPin); // Read raw analog value (0-1023)

  float voltage = sensorValue * (5.0 / 1023.0); // Convert to voltage (0-5V)

  float temperature = voltage * 100; // LM35 outputs 10mV per degree Celsius


  // Map temperature to fan speed

  int fanSpeed;

  if (temperature <= tempMin) {

    fanSpeed = 0; // Fan off

  } else if (temperature >= tempMax) {

    fanSpeed = 255; // Full speed

  } else {

    // Linearly map temperature to fan speed (0-255)

    fanSpeed = map(temperature, tempMin, tempMax, 0, 255);

  }


  analogWrite(fanPin, fanSpeed); // Control fan speed


  // Debugging output

  Serial.print("Temperature: ");

  Serial.print(temperature);

  Serial.print(" C, Fan Speed: ");

  Serial.println(fanSpeed);


  delay(1000); // Wait 1 second before the next reading

}


Explanation of the Code:

  1. Sensor Input:

    • The LM35 temperature sensor outputs 10mV per °C. The analog input is read and converted to a voltage. The temperature is calculated by multiplying the voltage by 100.
  2. Fan Speed Control:

    • The temperature is mapped to the PWM range (0–255) using the map() function.
    • Below the minimum temperature (tempMin), the fan is off.
    • Above the maximum temperature (tempMax), the fan runs at full speed.
    • Between these temperatures, the fan speed increases linearly with temperature.
  3. Proteus Setup:

    • Connect the LM35 output pin to A0 on the Arduino.
    • Use a transistor (e.g., 2N2222 or BC547) to drive the fan. The base of the transistor should be connected to pin 9 through a resistor (e.g., 1kΩ).
    • Connect the fan's positive terminal to a 12V power source and its negative terminal to the transistor's collector. The emitter should be grounded.
  4. Serial Monitor:

    • The serial output provides debugging information, displaying the temperature and fan speed.

Comments

Popular posts from this blog

Tutorial on Simulation of Servo Motor on proteus