#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <LiquidCrystal.h>
// Pin configuration
const int oneWireBus = 3; // Digital pin for DS18B20
const int phPin = A1; // Analog pin for pH (Potentiometer)
const int turbidityPin = A2; // Analog pin for Turbidity (LDR)
// Initialize the LCD library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
// Setup a OneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
// Flag to check if the values have been read
bool hasExecuted = false;
void setup() {
// Initialize the LCD and Serial Monitor
lcd.begin(16, 2);
lcd.print("Water Quality");
Serial.begin(9600);
// Start up the Dallas Temperature library
sensors.begin();
}
void loop() {
if (!hasExecuted) {
// Request temperature from DS18B20
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0); // Get temperature in Celsius
// Read pH value (from potentiometer)
int phValue = analogRead(phPin);
float ph = map(phValue, 0, 1023, 0, 14); // Convert to pH range 0-14
// Read turbidity value (from LDR)
int turbidityValue = analogRead(turbidityPin);
float turbidity = map(turbidityValue, 0, 1023, 0, 100); // Convert to a scale from 0 to 100
// Display values on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("T:"); lcd.print(temperature, 1); lcd.print("C");
lcd.setCursor(8, 0);
lcd.print("pH:"); lcd.print(ph, 1);
lcd.setCursor(0, 1);
lcd.print("Turb:"); lcd.print(turbidity, 1);
// Print values to Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature, 1);
Serial.println(" C");
Serial.print("pH: ");
Serial.println(ph, 1);
Serial.print("Turbidity: ");
Serial.println(turbidity, 1);
// Set the flag to true to indicate that execution is complete
hasExecuted = true;
}
// Add a delay to avoid any rapid looping (optional)
delay(1000); // Adjust as needed or remove if no further actions are needed
}