#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h> // Include the Servo library
// Initialize the LCD with the I2C address (usually 0x27)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define the analog pin connected to the thermistor
const int thermistorPin = A0;
// Define the digital pins connected to the LED
const int redPin = 11; // Red pin of the LED
const int greenPin = 10; // Green pin of the LED
const int bluePin = 9; // Blue pin of the LED
// Define servo pin
const int servoPin = 3; // Choose the pin for the servo control
// Parameters for the thermistor and resistor
const float BETA = 3950; // Beta coefficient of the thermistor
// Temperature thresholds for the LED and servo control
const float coldThreshold = 16.0; // Below this temperature, the green and red LED will be off
const float mediumThreshold = 40.0; // Between coldThreshold and mediumThreshold, the green LED will on
const float hotThreshold = 50.0; // Above this temperature, the red LED will on
// Servo objects for fan
Servo fanServo;
void setup() {
// Initialize I2C communication
Wire.begin();
// Initialize the LCD
lcd.begin(16, 2);
lcd.backlight();
lcd.print("Temperature: ");
// Initialize serial communication
Serial.begin(9600);
// Initialize the LED pins as outputs
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// Initialize the servo pins
fanServo.attach(servoPin);
// Ensure all LEDs are off initially
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
// Set initial positions for the servos
fanServo.write(0); // Fan off
}
void loop() {
int analogValue = analogRead(thermistorPin);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
// Print the temperature to the LCD
lcd.setCursor(0, 1);
lcd.print(celsius);
lcd.print(" C ");
// Ensure all LEDs are off initially
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
// Control the LED based on the temperature
if (celsius < coldThreshold) {
// Set LED Blue for Cold and stay on
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, HIGH);
// Turn off the fan
fanServo.write(0);
} else if (celsius < mediumThreshold) {
// Set LED Green for medium and stay on
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, LOW);
// Turn on the fan with medium speed
fanServo.write(90);
} else {
// Set LED Red for Hot and stay on
digitalWrite(bluePin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(redPin, HIGH);
// Turn on the fan with high speed
fanServo.write(180);
}
delay(1000);
}