#include <Adafruit_Sensor.h>
#include <OneWire.h> // DS18B20
#include <DallasTemperature.h>
#include <Wire.h>
#include <DHT.h>
#define DS18B20_PIN 22 // Liquid temperature sensor
#define HEATER_PIN 16 // Heater control
#define LEVEL_TRIGGER_PIN 32 // HC-SR04 Trigger
#define LEVEL_ECHO_PIN 33 // HC-SR04 Echo
#define WATER_PUMP_PIN 18 // Water pump
#define PH_SENSOR_PIN 25 // pH sensor
#define LIGHT_SENSOR_PIN 34 // Light intensity
#define LED_PIN 26 // Light control
#define DHTPIN 15 // Humidity sensor
#define DHTTYPE DHT22 // DHT sensor type
#define AIR_PUMP_PIN 19 // Air pump (CO2 or O2)
#define CO2_SENSOR_PIN 14 // CO2 sensor (MG811 analog output)
#define O2_SENSOR_PIN 12 // Oxygen sensor (Analog output)
#define DOSING_PUMP_PIN 5 // Dosing pump for pH control
// Threshold values for algae growth
#define TEMP_THRESHOLD_LOW 22 // Optimal temperature: 22–30 °C
#define TEMP_THRESHOLD_HIGH 30
#define DO_LOW_THRESHOLD 5.0 // Dissolved Oxygen: 5–8 mg/L
#define DO_HIGH_THRESHOLD 8.0
#define CO2_LOW_THRESHOLD 300 // CO2 level: 300–1000 ppm
#define CO2_HIGH_THRESHOLD 1000
#define LIGHT_THRESHOLD 400 // Light intensity: 400–1000 lux
#define LEVEL_THRESHOLD 20.0 // Distance threshold for low water level in cm
#define PH_THRESHOLD_LOW 6.5 // pH range: 6.5–8.0
#define PH_THRESHOLD_HIGH 8.0
// Sensor setup
OneWire oneWire(DS18B20_PIN);
DallasTemperature sensors(&oneWire);
DHT dht(DHTPIN, DHTTYPE);
float liquidTemperature = 0.0, phValue = 0.0, co2Level = 0.0, o2Level = 0.0, humidity = 0.0;
int lightIntensity = 0;
// Device statuses
bool heaterStatus = false, waterPumpStatus = false, ledStatus = false, dosingPumpStatus = false, airPumpStatus = false;
// Function Prototypes
void controlHeater();
void controlWaterPump(float distance);
void controlLED();
void controlAirPump(float co2, float o2);
void controlDosingPump(float pH);
float readLevelSensor();
float readCO2Sensor();
float readO2Sensor();
void setup() {
Serial.begin(9600);
// Initialize sensors
sensors.begin();
dht.begin();
pinMode(LEVEL_TRIGGER_PIN, OUTPUT);
pinMode(LEVEL_ECHO_PIN, INPUT);
pinMode(PH_SENSOR_PIN, INPUT);
pinMode(LIGHT_SENSOR_PIN, INPUT);
pinMode(HEATER_PIN, OUTPUT);
pinMode(WATER_PUMP_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(AIR_PUMP_PIN, OUTPUT);
pinMode(CO2_SENSOR_PIN, INPUT);
pinMode(O2_SENSOR_PIN, INPUT);
pinMode(DOSING_PUMP_PIN, OUTPUT);
// Turn off all devices initially
digitalWrite(HEATER_PIN, LOW);
digitalWrite(WATER_PUMP_PIN, LOW);
digitalWrite(LED_PIN, LOW);
digitalWrite(AIR_PUMP_PIN, LOW);
digitalWrite(DOSING_PUMP_PIN, LOW);
}
void loop() {
// Temperature
sensors.requestTemperatures();
liquidTemperature = sensors.getTempCByIndex(0);
controlHeater();
// Water level
float distance = readLevelSensor();
controlWaterPump(distance);
// pH
int phRaw = analogRead(PH_SENSOR_PIN);
phValue = phRaw * (14.0 / 4095.0); // Assuming a 14-bit ADC (0-4095 range)
controlDosingPump(phValue);
// Light
lightIntensity = analogRead(LIGHT_SENSOR_PIN);
controlLED();
// Humidity
humidity = dht.readHumidity();
// CO2 and O2
co2Level = readCO2Sensor();
o2Level = readO2Sensor();
controlAirPump(co2Level, o2Level);
// Display all readings and statuses
Serial.println("================ Sensor Readings and Device Statuses ================");
Serial.print("Temperature (°C): ");
Serial.println(liquidTemperature);
Serial.print("Heater: ");
Serial.println(heaterStatus ? "ON" : "OFF");
Serial.print("Water Level (cm): ");
Serial.println(distance);
Serial.print("Water Pump: ");
Serial.println(waterPumpStatus ? "ON" : "OFF");
Serial.print("pH: ");
Serial.println(phValue);
Serial.print("Dosing Pump: ");
Serial.println(dosingPumpStatus ? "ON" : "OFF");
Serial.print("Light Intensity (lux): ");
Serial.println(lightIntensity);
Serial.print("LED: ");
Serial.println(ledStatus ? "ON" : "OFF");
Serial.print("Humidity (%): ");
Serial.println(humidity);
Serial.print("CO2 Level (ppm): ");
Serial.println(co2Level);
Serial.print("Oxygen Level (%): ");
Serial.println(o2Level);
Serial.print("Air Pump: ");
Serial.println(airPumpStatus ? "ON" : "OFF");
Serial.println("====================================================================");
delay(2000); // Adjust delay as needed
}
void controlWaterPump(float distance) {
if (distance > LEVEL_THRESHOLD) {
digitalWrite(WATER_PUMP_PIN, HIGH);
waterPumpStatus = true;
} else {
digitalWrite(WATER_PUMP_PIN, LOW);
waterPumpStatus = false;
}
}
float readLevelSensor() {
digitalWrite(LEVEL_TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(LEVEL_TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(LEVEL_TRIGGER_PIN, LOW);
long duration = pulseIn(LEVEL_ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}
void controlHeater() {
if (liquidTemperature < TEMP_THRESHOLD_LOW) {
digitalWrite(HEATER_PIN, HIGH);
heaterStatus = true;
} else if (liquidTemperature > TEMP_THRESHOLD_HIGH) {
digitalWrite(HEATER_PIN, LOW);
heaterStatus = false;
}
}
void controlLED() {
if (lightIntensity < LIGHT_THRESHOLD) {
digitalWrite(LED_PIN, HIGH);
ledStatus = true;
} else {
digitalWrite(LED_PIN, LOW);
ledStatus = false;
}
}
float readCO2Sensor() {
int co2Raw = analogRead(CO2_SENSOR_PIN);
float co2Voltage = co2Raw * (5.0 / 1023.0); // Convert to voltage
float co2Concentration = co2Voltage * 1000; // Example conversion to ppm
return co2Concentration;
}
float readO2Sensor() {
int o2Raw = analogRead(O2_SENSOR_PIN);
float o2Voltage = o2Raw * (5.0 / 1023.0); // Convert to voltage
float o2Concentration = o2Voltage * 100; // Example conversion to % oxygen
return o2Concentration;
}
void controlAirPump(float co2, float o2) {
if (co2 < CO2_LOW_THRESHOLD) {
digitalWrite(AIR_PUMP_PIN, HIGH);
airPumpStatus = true;
Serial.println("Air Pump: Adding CO2");
} else if (o2 < DO_LOW_THRESHOLD) {
digitalWrite(AIR_PUMP_PIN, HIGH);
airPumpStatus = true;
Serial.println("Air Pump: Adding O2");
} else {
digitalWrite(AIR_PUMP_PIN, LOW);
airPumpStatus = false;
}
}
void controlDosingPump(float pH) {
if (pH < PH_THRESHOLD_LOW) {
digitalWrite(DOSING_PUMP_PIN, HIGH);
dosingPumpStatus = true;
} else if (pH > PH_THRESHOLD_HIGH) {
digitalWrite(DOSING_PUMP_PIN, LOW);
dosingPumpStatus = false;
}
}
Loading
ds18b20
ds18b20