#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize DHT sensor.
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Initialize pins for LED and buzzer.
const int lampuPin = 9;
const int buzzerPin = 7;
// Initialize the LCD.
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change 0x27 to your LCD address if different
void setup() {
Serial.begin(9600);
dht.begin();
// Initialize pins for output.
pinMode(lampuPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
//LCD
lcd.init();
lcd.backlight();
// Display names on the LCD
lcd.setCursor(0, 0);
lcd.print("UAS_Embedded System");
lcd.setCursor(0, 1);
lcd.print("Dosen: Rudi H,MT");
delay(3000); // Display for 3 seconds
lcd.clear();
// Display names on the LCD
lcd.setCursor(0, 0);
lcd.print("Mahasiswa:ASNAWI");
lcd.setCursor(0, 1);
lcd.print("NIM:1083232130");
delay(3000); // Display for 3 seconds
lcd.clear();
}
void loop() {
float suhu = dht.readTemperature();
if (!isnan(suhu)) {
Serial.print("Suhu: ");
Serial.print(suhu);
Serial.println("°C");
// Display temperature on the LCD.
lcd.setCursor(0, 0);
lcd.print("Suhu: ");
lcd.print(suhu);
lcd.print(" C");
if (suhu > 40.0 || suhu < 40.0) {
digitalWrite(lampuPin, HIGH);
tone(buzzerPin, 1000);
delay(1000);
digitalWrite(lampuPin, LOW);
noTone(buzzerPin);
} else {
digitalWrite(lampuPin, LOW);
noTone(buzzerPin);
}
} else {
Serial.println("Gagal membaca data dari sensor suhu!");
lcd.setCursor(0, 0);
lcd.print("Error baca suhu");
}
delay(2000);
}