Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

ESP32-Based Aquaponics Monitoring System with Relay Control

Image of ESP32-Based Aquaponics Monitoring System with Relay Control

Circuit Documentation

Summary

The circuit in question is designed to monitor various parameters of an aquaponics system, including water temperature, air temperature, humidity, total dissolved solids (TDS), pH level, and dissolved oxygen (DO). It utilizes an Arduino Nano ESP32 as the central microcontroller to read data from a suite of sensors and control a relay that can switch a water pump on or off based on the water temperature. The sensors include a TDS sensor, a PH Meter, a DO Sensor, a Temperature Sensor, and an AHT10 sensor for measuring air temperature and humidity. A buck converter is used to step down the voltage from a lipo battery to power the system, and a 1-Channel Relay is used to control the water pump.

Component List

  • TDS Sensor: Measures the total dissolved solids in water.
  • AHT10: A sensor for measuring air temperature and humidity.
  • Buck Converter: Steps down voltage from the battery to a lower voltage required by the circuit.
  • 1-Channel Relay (5V 10A): Controls the water pump based on signals from the microcontroller.
  • Arduino Nano ESP32: The microcontroller that manages sensor readings and controls the relay.
  • PH Meter: Measures the pH level of the water.
  • Lipo Battery 2200mAh 30C: Provides power to the circuit.
  • DO Sensor: Measures the dissolved oxygen in the water.
  • Temperature Sensor: Measures the water temperature.
  • Water Pump: Pumps water through the system, controlled by the relay.

Wiring Details

TDS Sensor

  • AI: Connected to Arduino Nano ESP32 A0 pin.
  • GND: Common ground with other components.
  • 5V: Powered by the output of the Buck Converter.

AHT10

  • Vcc: Powered by the output of the Buck Converter.
  • Gnd: Common ground with other components.
  • SCL: Connected to Arduino Nano ESP32 A5 pin.
  • SDA: Connected to Arduino Nano ESP32 A4 pin.

Buck Converter

  • IN+: Connected to the positive terminal of the lipo battery.
  • IN-: Connected to the negative terminal of the lipo battery.
  • OUT+: Powers multiple components including the AHT10, 1-Channel Relay, DO Sensor, PH Meter, TDS Sensor, and Temperature Sensor.
  • OUT-: Common ground with other components.

1-Channel Relay (5V 10A)

  • NC: Normally closed contact, not used in this circuit.
  • Signal: Controlled by Arduino Nano ESP32 D8 pin.
  • C: Connected to the positive terminal of the lipo battery.
  • Power: Powered by the output of the Buck Converter.
  • NO: Normally open contact, connected to the positive terminal of the water pump.
  • Ground: Common ground with other components.

Arduino Nano ESP32

  • Various Digital and Analog Pins: Connected to sensors and relay as detailed in the wiring.
  • GND: Common ground with other components.
  • 3.3V: Not used in this circuit.
  • VBUS: Not used in this circuit.
  • VIN: Not used in this circuit.

PH Meter

  • Signal: Connected to Arduino Nano ESP32 A3 pin.
  • VCC: Powered by the output of the Buck Converter.
  • GND: Common ground with other components.

Lipo Battery 2200mAh 30C

  • VCC: Provides power to the Buck Converter.
  • GND: Common ground with other components.

DO Sensor

  • Vcc: Powered by the output of the Buck Converter.
  • Gnd: Common ground with other components.
  • Out: Connected to Arduino Nano ESP32 A1 pin.

Temperature Sensor

  • Temp GND Black: Common ground with other components.
  • Temp VDD Red: Powered by the output of the Buck Converter.
  • Temp DQ Data Yellow: Connected to Arduino Nano ESP32 D5 pin.

Water Pump

  • Positive: Connected to the normally open contact (NO) of the 1-Channel Relay.
  • Negative: Common ground with other components.

Documented Code

#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <AHT10.h>
#include <DFRobot_ESP_PH.h>
#include <DFRobot_DO.h> // Added dissolved oxygen sensor library

// Pin Definitions
#define TDS_SENSOR_PIN A0
#define PH_SENSOR_PIN A3
#define DS18B20_PIN 5
#define RELAY_PIN 8
#define ONE_WIRE_BUS DS18B20_PIN
#define DO_SENSOR_PIN A1 // Added pin for dissolved oxygen sensor
#define SCL_PIN A5 // Typical SCL pin for I2C
#define SDA_PIN A4 // Typical SDA pin for I2C

// Sensor Objects
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature tempSensor(&oneWire);
AHT10 aht10;
DFRobot_ESP_PH ph;
DFRobot_DO do_sensor; // Dissolved oxygen sensor object
float tdsSensorValue = 0;
float phValue = 0;
float doValue = 0; // Dissolved oxygen value

void setup()
{
    Serial.begin(115200);

    // Initialize I2C with specific SCL and SDA pins
    Wire.begin(SDA_PIN, SCL_PIN);

    // Initialize Sensors
    tempSensor.begin();

    // AHT10 Initialization
    if (!aht10.begin())
    {
        Serial.println("AHT10 not found. Check wiring!");
    }

    // PH Sensor Initialization
    ph.begin();

    // Dissolved Oxygen Sensor Initialization
    do_sensor.begin();

    // Relay Setup
    pinMode(RELAY_PIN, OUTPUT);
    digitalWrite(RELAY_PIN, LOW);
}

void loop()
{
    // Read Temperature Sensors
    tempSensor.requestTemperatures();
    float waterTemp = tempSensor.getTempCByIndex(0);

    // Read AHT10 (Humidity and Air Temperature)
    float humidity = aht10.readHumidity();
    float airTemp = aht10.readTemperature();

    // Read TDS Sensor
    tdsSensorValue = readTDSSensor();

    // Read PH Sensor
    phValue = readPHSensor();

    // Read Dissolved Oxygen Sensor
    doValue = readDOSensor(waterTemp);

    // Print Sensor Readings
    Serial.println("--- Aquaponics Sensor Readings ---");
    Serial.print("Water Temperature: ");
    Serial.print(waterTemp);
    Serial.println(" °C");

    Serial.print("Air Temperature: ");
    Serial.print(airTemp);
    Serial.println(" °C");

    Serial.print("Humidity: ");
    Serial.print(humidity);
    Serial.println(" %");

    Serial.print("TDS Value: ");
    Serial.print(tdsSensorValue);
    Serial.println(" ppm");

    Serial.print("PH Value: ");
    Serial.print(phValue);
    Serial.println("");

    Serial.print("Dissolved Oxygen: ");
    Serial.print(doValue);
    Serial.println(" mg/L");

    // Control Relay (example: turn on if water temp is below 20°C)
    if (waterTemp < 20.0)
    {
        digitalWrite(RELAY_PIN, HIGH);
    }
    else
    {
        digitalWrite(RELAY_PIN, LOW);
    }

    delay(5000); // Read sensors every 5 seconds
}

float readTDSSensor()
{
    int sensorValue = analogRead(TDS_SENSOR_PIN);
    float voltage = sensorValue * (3.3 / 4095.0);
    float tdsValue = (133.42 * voltage * voltage * voltage - 255.86 * voltage * voltage + 857.39 * voltage) * 0.5;
    return tdsValue;
}

float readPHSensor()
{
    static unsigned long samplingTime = millis();
    static unsigned long printTime = millis();
    static float pHValue, voltage;

    if (millis() - samplingTime > 20)
    {
        pHValue = ph.readPH(analogRead(PH_SENSOR_PIN), 25.0); // 25.0°C temperature compensation
        samplingTime = millis();
    }

    return pHValue;
}

float readDOSensor(float temperature)
{
    // Read dissolved oxygen sensor with temperature compensation
    float doValue = do_sensor.readDO(analogRead(DO_SENSOR_PIN), temperature);
    return doValue;
}

This code is responsible for initializing and reading from the various sensors connected to the Arduino Nano ESP32. It also controls the relay based on the water temperature. The code includes functions to read from the TDS, pH, and DO sensors, and it prints the sensor readings to the serial monitor. The relay is controlled in the loop() function, where it is turned on if the water temperature is below 20°C. The code is structured with clear definitions for pin assignments and sensor objects, making it easy to understand and maintain.