#include <DHT.h> // DHT library for temperature and humidity
#include <Wire.h> // For I2C communication (pH sensor and light sensor if needed)
#include <Adafruit_Sensor.h>
#include <OneWire.h> // For DS18B20 sensor
#include <DallasTemperature.h>
#define DHTPIN 15 // DHT sensor pin
#define DHTTYPE DHT22 // Define DHT sensor type (DHT22)
DHT dht(DHTPIN, DHTTYPE);
#define TEMP_THRESHOLD_HEATER 30 // Example temperature <=temp in Celsius
#define TEMP_THRESHOLD_COOLER 37 // Example temperature >=temp in Celsius
#define HUMIDITY_THRESHOLD 70 // Example humidity threshold in %
#define LEVEL_TRIGGER_PIN 32 // HC-SR04 Trigger pin
#define LEVEL_ECHO_PIN 33 // HC-SR04 Echo pin
#define PH_SENSOR_PIN 25 // Define pH sensor pin (analog)
#define LIGHT_SENSOR_PIN 34 // Define light sensor pin (analog)
#define DS18B20_PIN 4 // DS18B20 sensor pin (liquid temperature)
#define HEATER_PIN 14 // Heater control pin
#define PUMP_PIN 12 // Pump control pin
#define MOTOR_PIN 13 // DC motor control pin
#define PELTIER_PIN 27 // Peltier module control pin
#define LED_PIN 26 // LED light control pin
float phValue = 0.0; // pH value to be read from sensor
int lightIntensity = 0; // Light intensity value
float temperature = 0.0, humidity = 0.0;
long duration; // Duration of pulse from HC-SR04
float distance; // Calculated distance from HC-SR04
float liquidTemperature = 0.0; // Liquid temperature from DS18B20
// Setup for DS18B20
OneWire oneWire(DS18B20_PIN);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
// Initialize sensors
dht.begin();
pinMode(LEVEL_TRIGGER_PIN, OUTPUT);
pinMode(LEVEL_ECHO_PIN, INPUT);
pinMode(PH_SENSOR_PIN, INPUT);
pinMode(LIGHT_SENSOR_PIN, INPUT);
// Initialize output devices
pinMode(HEATER_PIN, OUTPUT);
pinMode(PUMP_PIN, OUTPUT);
pinMode(MOTOR_PIN, OUTPUT);
pinMode(PELTIER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
// Turn off all output devices initially
digitalWrite(HEATER_PIN, LOW);
digitalWrite(PUMP_PIN, LOW);
digitalWrite(MOTOR_PIN, LOW);
digitalWrite(PELTIER_PIN, LOW);
digitalWrite(LED_PIN, LOW);
}
void loop() {
// Read temperature and humidity
temperature = dht.readTemperature();
humidity = dht.readHumidity();
// Read pH level (analog)
int phValueRaw = analogRead(PH_SENSOR_PIN);
phValue = (phValueRaw * (14.0 / 4095.0)); // Scale raw value to pH range (0-14)
// Read light intensity
lightIntensity = analogRead(LIGHT_SENSOR_PIN);
// Read level sensor (HC-SR04)
distance = readLevelSensor();
// Read liquid temperature from DS18B20
sensors.requestTemperatures();
liquidTemperature = sensors.getTempCByIndex(0); // Assuming one DS18B20 sensor
// Classify the pH value
String phClassification = classifyPH(phValue);
// Print sensor readings
Serial.print("Room Temperature: "); Serial.print(temperature); Serial.println(" *C");
Serial.print("Humidity: "); Serial.print(humidity); Serial.println(" %");
Serial.print("pH: "); Serial.println(phValue); Serial.print(" - ");
Serial.println(phClassification); // Print the classification
Serial.print("Light Intensity: "); Serial.println(lightIntensity);
Serial.print("Water Level Distance: "); Serial.print(distance); Serial.println(" cm");
Serial.print("Liquid Temperature: "); Serial.print(liquidTemperature); Serial.println(" C");
// Control logic for output devices
controlHeater();
controlPump(distance);
controlDCMotor();
controlPeltier();
controlLED();
delay(2000); // Adjust delay as per required sampling rate
}
String classifyPH(float ph) {
if (ph < 7.0) {
return "Acidic";
} else if (ph == 7.0) {
return "Neutral";
} else {
return "Alkaline";
}
}
// Function to read level sensor using HC-SR04
float readLevelSensor() {
// Trigger the sensor
digitalWrite(LEVEL_TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(LEVEL_TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(LEVEL_TRIGGER_PIN, LOW);
// Read the echo pin and calculate distance
duration = pulseIn(LEVEL_ECHO_PIN, HIGH);
distance = duration * 0.034 / 2; // Convert time to distance (cm)
return distance;
}
void controlHeater() {
if (liquidTemperature < TEMP_THRESHOLD_HEATER) {
digitalWrite(HEATER_PIN, HIGH); // Turn on heater if temperature is below threshold
Serial.println("Heater ON");
} else {
digitalWrite(HEATER_PIN, LOW); // Turn off heater
Serial.println("Heater OFF");
}
}
void controlPump(float distance) {
if (distance > 20) { // Assume distance > 20 cm indicates low water level
digitalWrite(PUMP_PIN, HIGH);
Serial.println("Pump ON");
} else {
digitalWrite(PUMP_PIN, LOW);
Serial.println("Pump OFF");
}
}
void controlDCMotor() {
if (humidity > HUMIDITY_THRESHOLD) {
digitalWrite(MOTOR_PIN, HIGH); // Run motor if humidity is high
Serial.println("DC Motor ON");
} else {
digitalWrite(MOTOR_PIN, LOW);
Serial.println("DC Motor OFF");
}
}
void controlPeltier() {
if (liquidTemperature > TEMP_THRESHOLD_COOLER) {
digitalWrite(PELTIER_PIN, HIGH); // Cool down if temperature exceeds threshold
Serial.println("Peltier ON (Cooling)");
} else {
digitalWrite(PELTIER_PIN, LOW);
Serial.println("Peltier OFF");
}
}
void controlLED() {
if (lightIntensity < 500) { // Assuming a threshold for low light
digitalWrite(LED_PIN, HIGH); // Turn on LED if light intensity is low
Serial.println("LED ON");
} else {
digitalWrite(LED_PIN, LOW);
Serial.println("LED OFF");
}
}