#include <LiquidCrystal.h> // Include library for LCD display (optional)
const int tempPin = A0; // Pin connected to LM35 sensor
const int fanPin = 9; // Pin connected to fan (PWM pin)
const int transistorPin = 3; // Pin connected to transistor base
int tempValue;
int fanSpeed;
// Create LCD object (if using an LCD display)
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
pinMode(fanPin, OUTPUT);
pinMode(transistorPin, OUTPUT);
Serial.begin(9600); // Initialize serial communication (for debugging)
// Initialize LCD (if using)
lcd.begin(16, 2);
lcd.print("Temp: ");
}
void loop() {
tempValue = analogRead(tempPin);
float temperature = tempValue * 5.0 / 1024.0 * 100; // Convert to Celsius
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" C");
fanSpeed = map(temperature, 25, 40, 0, 255); // Map temperature to fan speed
analogWrite(fanPin, fanSpeed);
// Display temperature on LCD (optional)
lcd.setCursor(6, 0);
lcd.print(temperature);
}