0% found this document useful (0 votes)
6 views2 pages

PHTMCODE

This document contains an Arduino sketch for a temperature and humidity monitoring system using a DHT11 sensor and an ultrasonic distance sensor. It controls a fan based on temperature readings and activates a buzzer if water levels are low. The system displays the temperature, humidity, fan status, and water level on an I2C LCD screen.

Uploaded by

Arabel Fig
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

PHTMCODE

This document contains an Arduino sketch for a temperature and humidity monitoring system using a DHT11 sensor and an ultrasonic distance sensor. It controls a fan based on temperature readings and activates a buzzer if water levels are low. The system displays the temperature, humidity, fan status, and water level on an I2C LCD screen.

Uploaded by

Arabel Fig
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <LiquidCrystal_I2C.

h>
#include <DHT.h>

#define DHTPIN 2
#define DHTTYPE DHT11

#define TRIGPIN 3
#define ECHOPIN 4

#define FANPIN 8
#define BUZZERPIN 7

DHT dht(DHTPIN, DHTTYPE);


LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C LCD address

void setup() {
lcd.init();
lcd.backlight();
dht.begin();

pinMode(TRIGPIN, OUTPUT);
pinMode(ECHOPIN, INPUT);
pinMode(FANPIN, OUTPUT);
pinMode(BUZZERPIN, OUTPUT);

lcd.setCursor(0, 0);
lcd.print("PH&TM READY! :)");
delay(2000);
lcd.clear();
}

void loop() {
float temp = dht.readTemperature();
float hum = dht.readHumidity();

// NaN Check
if (isnan(temp) || isnan(hum)) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sensor Error");
digitalWrite(FANPIN, LOW);
noTone(BUZZERPIN);
delay(2000);
return;
}

digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);

long duration = pulseIn(ECHOPIN, HIGH, 30000);


float distanceCM = duration * 0.034 / 2;

bool fanOn = temp >= 28;


digitalWrite(FANPIN, fanOn ? HIGH : LOW);
bool waterLow = (duration == 0 || distanceCM > 8);
if (waterLow) {
tone(BUZZERPIN, 1000);
delay(100);
noTone(BUZZERPIN);
} else {
noTone(BUZZERPIN);
}

// LCD Display
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(temp, 1);
lcd.print((char)223);
lcd.print("C H:");
lcd.print(hum, 0);
lcd.print("%");

lcd.setCursor(0, 1);
lcd.print("FAN:");
lcd.print(fanOn ? "ON " : "OFF");
lcd.print(" W:");
lcd.print(waterLow ? "LOW " : "OK ");

delay(1000);
}

You might also like