Week -2
Use a thermistor to sense the temperature of a heater & turn on or off the fan as per the below conditions:
Temperature source: 20 °C from 0 to 10 seconds, 27 °C from 10 to 30 seconds, 23 °C from 30 to 50 seconds
Fan conditions: ON if the temperature is above 25 °C, OFF otherwise
To set up a system using a thermistor to control a fan based on temperature readings, you'll need some basic electronic components, including a thermistor, a microcontroller (like an Arduino), and a relay or transistor to control the fan. Here's a step-by-step guide on how to achieve this:
### Components Needed:
1. Thermistor: A type of resistor whose resistance varies significantly with temperature.
2. Microcontroller: For this example, we'll use an Arduino board.
3. Relay or Transistor: To switch the fan on or off based on the microcontroller's output.
4. **Fan**: The fan that you want to control.
### Circuit Setup:
1. Connect the transistor to Arduino:
- Wire the thermistor to form a voltage divider with a known resistor (e.g., 10kΩ). This setup will allow you to measure the voltage across the thermistor, which varies with temperature.
Connect one end of the thermistor to the 5V supply.
Connect the other end of the thermistor to one end of the fixed resistor.
Connect the other end of the fixed resistor to ground.
Connect the point between the thermistor and resistor to one of the analog input pins (e.g., A0) of the Arduino.
2. Control the Fan via relay or transistor:
Connect the control input of a relay or transistor to one of the digital output pins (e.g., D7) of the Arduino.
Connect the positive terminal of the fan to a suitable power supply.
Connect the negative terminal of the fan to one terminal of the relay or transistor.
Connect the other terminal of the relay or transistor to ground.
### Arduino Code:
Now, write the Arduino code to read the analog input from the thermistor, determine if the temperature exceeds 25°C, and then control the fan accordingly.
```cpp
const int thermistorPin = A0; // Analog pin connected to the thermistor
const int fanControlPin = 7; // Digital pin connected to the relay or transistor
void setup() {
pinMode(fanControlPin, OUTPUT);
digitalWrite(fanControlPin, LOW); // Ensure fan starts in OFF state
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
Read the analog value from the thermistor
int sensorValue = analogRead(thermistorPin);
// Convert the analog reading to temperature in degrees Celsius
float temperature = map(sensorValue, 0, 1023, 0, 100); // Assuming appropriate calibration
// Print temperature for debugging (optional)
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Control the fan based on temperature conditions
if (temperature > 25) {
digitalWrite(fanControlPin, HIGH); // Turn ON the fan
} else {
digitalWrite(fanControlPin, LOW); // Turn OFF the fan
}
delay(1000); // Adjust delay as needed for temperature sampling rate
}
```
### Explanation of the Code:
**`thermistorPin`**: This variable holds the analog pin number to which the thermistor is connected.
**`fanControlPin`**: This variable holds the digital pin number used to control the relay or transistor connected to the fan.
**`setup()`**: In the setup function, we set the `fanControlPin` as an output.
**`loop()`**: The loop function continuously reads the analog value from the thermistor, converts it to temperature, and then checks if the temperature exceeds 25°C. If it does, it sets the `fanControlPin` HIGH to turn on the fan; otherwise, it sets it LOW to turn off the fan.
### Note:
Ensure your thermistor is calibrated appropriately to convert the analog readings into accurate temperature values.
Adjust the threshold temperature (`25°C` in this case) in the code according to your specific requirements.
The `delay(1000)` at the end of the loop sets the sampling rate to approximately 1 second. You can adjust this delay based on your desired temperature reading frequency.