#include <LiquidCrystal.h>
#include <DHT.h>
// Pin Assignments
const int potentiometer_pin = A0; // Analog pin for the potentiometer (simulating pH sensor)
const int DHT_pin = 2; // Digital pin for DHT sensor
// Ultrasonic Sensor Pin Assignments
const int trigPin = 7; // Trigger pin for the ultrasonic sensor
const int echoPin = 8; // Echo pin for the ultrasonic sensor
// DHT Sensor settings
#define DHTTYPE DHT11 // Use DHT11 or DHT22
DHT dht(DHT_pin, DHTTYPE);
// Initialize the LCD (RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
Serial.begin(9600);
lcd.begin(16, 2); // Set up the LCD's number of columns and rows
dht.begin(); // Initialize the DHT sensor
// Ultrasonic sensor setup
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Display a startup message in English
lcd.print("Water Quality");
lcd.setCursor(0, 1);
lcd.print("Monitoring");
delay(2000); // Wait for 2 seconds
lcd.clear(); // Clear the LCD
}
void loop() {
// Read potentiometer value (simulating pH value)
int potentiometer_value = analogRead(potentiometer_pin);
float pH = map(potentiometer_value, 0, 1023, 0, 14); // Simulate pH between 0 and 14
// Read temperature and humidity from DHT sensor
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Read ultrasonic sensor for water level measurement
long duration, distance;
// Clear the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigPin HIGH for 10 microseconds to send a pulse
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin and calculate the time it took for the pulse to return
duration = pulseIn(echoPin, HIGH);
// Calculate the distance (in cm)
distance = duration * 0.034 / 2; // Divide by 2 because the pulse travels to the object and back
// Map distance to water level percentage (adjust max distance according to your tank size)
int waterLevelPercentage = map(distance, 0, 100, 100, 0); // Assuming 100 cm is the max height of the water level
// Ensure the value stays between 0% and 100%
waterLevelPercentage = constrain(waterLevelPercentage, 0, 100);
// Display values on the LCD
lcd.setCursor(0, 0);
lcd.print("pH: ");
lcd.print(pH);
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C ");
lcd.setCursor(9, 1);
lcd.print("Lvl: ");
lcd.print(waterLevelPercentage);
lcd.print("%");
// Print values to Serial Monitor for debugging
Serial.print("pH: ");
Serial.print(pH);
Serial.print(" | Temperature: ");
Serial.print(temperature);
Serial.print(" C | Humidity: ");
Serial.print(humidity);
Serial.print(" % | Water Level: ");
Serial.print(waterLevelPercentage);
Serial.println(" %");
delay(2000); // Delay 2 seconds before the next loop
}