#include <Arduino.h>
#include <Servo.h> // Library untuk mengendalikan servo
#include <LiquidCrystal_I2C.h> // Library untuk mengendalikan LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2); // Inisialisasi objek untuk mengontrol LCD
Servo fanServo; // Objek untuk mengontrol servo
// Define the pins for the servo, LED, and NTC temperature sensor
const int fanServoPin = 9;
const int lampLedPin = 11;
const int temperatureSensorPin = A1;
// Define the thresholds for temperature control
const int fanOnThreshold = 28; // Fan will turn on when the temperature exceeds this value
const int fanOffThreshold = 25; // Fan will turn off when the temperature is below this value
// Define servo positions for different temperature conditions
const int servoPosition1 = 45; // Servo position for abnormal temperature (misalnya)
const int servoPosition2 = 90; // Servo position for normal temperature (misalnya)
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Attach the servo to the fan pin
fanServo.attach(fanServoPin); // Menghubungkan servo dengan pin
// Set LED and servo pins as OUTPUT
pinMode(lampLedPin, OUTPUT);
pinMode(fanServoPin, OUTPUT);
// Inisialisasi LCD
lcd.begin(16, 2);
lcd.print("System started!");
// Print initial message on the Serial Monitor
Serial.println("System started!");
}
void loop() {
// Read the temperature from the NTC sensor
int rawValue = analogRead(temperatureSensorPin);
float voltage = rawValue * (5.0 / 1023.0); // Convert raw value to voltage (assuming 5V reference)
float resistance = (5.0 * 10000.0 / voltage) - 10000.0; // Calculate the resistance of the NTC
float steinhart;
steinhart = resistance / 10000.0; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= 3380.0; // 1/B * ln(R/Ro)
steinhart += 1.0 / (25 + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // Convert to Celsius
// Display the temperature on the Serial Monitor
Serial.print("Temperature: ");
Serial.print(steinhart);
Serial.println(" °C");
// Check temperature and control fan and lamp accordingly
if (steinhart > fanOnThreshold) {
// Turn on the fan and move servo to position 1
digitalWrite(lampLedPin, LOW); // Turn off the lamp LED
digitalWrite(fanServoPin, HIGH);
fanServo.write(servoPosition1);
Serial.println("Fan: ON");
} else if (steinhart < fanOffThreshold) {
// Turn off the fan and move servo to position 2
digitalWrite(lampLedPin, LOW); // Turn off the lamp LED
digitalWrite(fanServoPin, HIGH);
fanServo.write(servoPosition2);
Serial.println("Fan: OFF");
} else {
// Temperature is in the middle range, turn off the fan and move servo to position 2
digitalWrite(lampLedPin, HIGH); // Turn on the lamp LED
digitalWrite(fanServoPin, LOW);
fanServo.write(servoPosition2);
Serial.println("Fan: OFF");
}
// Update the LED indicators based on temperature range
if (steinhart < 20) {
// Cold temperature: Turn on the blue LED
digitalWrite(lampLedPin, HIGH);
Serial.println("LED: BLUE");
} else if (steinhart >= 20 && steinhart <= 30) {
// Normal temperature: Turn on the green LED
digitalWrite(lampLedPin, LOW);
Serial.println("LED: GREEN");
} else {
// Hot temperature: Turn on the red LED
digitalWrite(lampLedPin, LOW);
Serial.println("LED: RED");
}
// Update the LCD display with the temperature
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(steinhart);
lcd.print(" C");
delay(5000); // Add a delay to avoid rapid temperature updates
}