#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include "DHT.h"
// Define DHT pin and type
#define DHTPIN 13
#define DHTTYPE DHT22
// Define objects for sensor, LCD, servo, and buzzer
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo myservo;
const int buzzerPin = 8; // Define buzzer pin
// Define LED pin
int led = 2;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
Serial.println("Monitoring Suhu Ruang");
Serial.println("By Saiful");
dht.begin();
myservo.attach(3);
pinMode(led, OUTPUT);
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
}
void loop() {
// Read temperature and humidity
float h = dht.readHumidity();
float t = dht.readTemperature();
delay(100);
// Display on LCD
lcd.setCursor(0, 0);
lcd.println("Suhu:");
lcd.print(t);
lcd.println(" C");
lcd.setCursor(0, 1);
lcd.println("Lembab:");
lcd.print(h);
lcd.println(" %");
// Display on serial monitor
Serial.print("Suhu: ");
Serial.print(t);
Serial.print(" *C\t");
Serial.print("Kelembaban: ");
Serial.print(h);
Serial.println(" %");
// Check temperature and control buzzer
if (t > 40) {
digitalWrite(led, HIGH); // Turn LED on
tone(buzzerPin, 1000); // Play buzzer sound at 1000 Hz
delay(1000); // Buzz for 1 second
noTone(buzzerPin); // Stop buzzer sound
} else {
digitalWrite(led, LOW); // Turn LED off
noTone(buzzerPin); // Ensure buzzer is off if temperature is below 40°C
}
// Servo control logic (unmodified in this example)
// ... your servo control code here ...
delay(500); // Delay between readings
}