Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP32-C3 Controlled Water Level Monitoring and Pump Management System

View and Edit Project In Cirkit DesignerImage of ESP32-C3 Controlled Water Level Monitoring and Pump Management System

Circuit Documentation

Summary

The circuit in question is designed to monitor and control the water levels in three separate tanks using ultrasonic distance sensors. It also includes servo motors to control valves and a relay to operate a water pump. The central processing unit for this system is a NodeESP32-C3 microcontroller, which is responsible for reading sensor data, controlling the servo motors, and switching the water pump on or off via the relay.

Component List

NodeESP32-C3

MKE-S01 Ultrasonic Distance Sensor

Servo

12V Single Channel Relay

Water Pump

Power Supply

Wiring Details

NodeESP32-C3

MKE-S01 Ultrasonic Distance Sensor

Servo

12V Single Channel Relay

Water Pump

Power Supply

Documented Code

#include <NewPing.h>
#include <Servo.h>

// Pin configuration for ultrasonic sensors (adjust for NodeMCU pins)
#define TRIG_PIN_1 D1
#define ECHO_PIN_1 D2
#define TRIG_PIN_2 D3
#define ECHO_PIN_2 D4
#define TRIG_PIN_3 D5
#define ECHO_PIN_3 D6

// Motor relay control pin and servo control pins
#define RELAY_PIN D7       // Relay control pin for motor
#define SERVO_PIN_1 D8     // Adjust for servo 1
#define SERVO_PIN_2 D0     // Adjust for servo 2
#define SERVO_PIN_3 D9     // Adjust for servo 3

// Tank capacities (in liters)
#define TANK_CAPACITY 1000  // Liters for each tank

// Max distance to measure (in cm)
#define MAX_DISTANCE 400  // Adjust based on tank height

// Water level thresholds (in cm)
#define MIN_WATER_LEVEL 50  // Low level threshold (tank needs filling)
#define MAX_WATER_LEVEL 10  // Full level threshold (tank is full)

// Variables to store water consumption
int tank1_consumed = 0;
int tank2_consumed = 0;
int tank3_consumed = 0;

// Define the ultrasonic objects for 3 tanks
NewPing sonar1(TRIG_PIN_1, ECHO_PIN_1, MAX_DISTANCE);
NewPing sonar2(TRIG_PIN_2, ECHO_PIN_2, MAX_DISTANCE);
NewPing sonar3(TRIG_PIN_3, ECHO_PIN_3, MAX_DISTANCE);

// Servo motor objects
Servo servo1, servo2, servo3;

// Function to get water level (in cm)
int getWaterLevel(NewPing &sonar) {
  int distance = sonar.ping_cm();  // Get distance in cm
  if (distance == 0) {
    distance = MAX_DISTANCE;  // If no echo, assume out of range
  }
  return distance;
}

void setup() {
  Serial.begin(115200);
  pinMode(RELAY_PIN, OUTPUT);       // Set relay pin as OUTPUT
  digitalWrite(RELAY_PIN, LOW);     // Ensure motor (relay) is off initially
  
  // Attach servo motors
  servo1.attach(SERVO_PIN_1);
  servo2.attach(SERVO_PIN_2);
  servo3.attach(SERVO_PIN_3);

  // Initialize servos to closed position
  servo1.write(0);  // Closed position
  servo2.write(0);  // Closed position
  servo3.write(0);  // Closed position
}

void loop() {
  int level1 = getWaterLevel(sonar1);
  int level2 = getWaterLevel(sonar2);
  int level3 = getWaterLevel(sonar3);
  
  Serial.print("Tank 1 Water Level: ");
  Serial.println(level1);
  Serial.print("Tank 2 Water Level: ");
  Serial.println(level2);
  Serial.print("Tank 3 Water Level: ");
  Serial.println(level3);

  // Control servo motors based on water levels
  if (level1 > MIN_WATER_LEVEL) {
    servo1.write(90);  // Open servo for tank 1
  } else {
    servo1.write(0);   // Close servo for tank 1
    tank1_consumed += TANK_CAPACITY;  // Store the tank capacity when filled
  }
  
  if (level2 > MIN_WATER_LEVEL) {
    servo2.write(90);  // Open servo for tank 2
  } else {
    servo2.write(0);   // Close servo for tank 2
    tank2_consumed += TANK_CAPACITY;
  }
  
  if (level3 > MIN_WATER_LEVEL) {
    servo3.write(90);  // Open servo for tank 3
  } else {
    servo3.write(0);   // Close servo for tank 3
    tank3_consumed += TANK_CAPACITY;
  }

  // Check if any tank is below minimum water level to turn motor (relay) ON
  if (level1 < MIN_WATER_LEVEL || level2 < MIN_WATER_LEVEL || level3 < MIN_WATER_LEVEL) {
    Serial.println("Water level is low. Turning motor ON...");
    digitalWrite(RELAY_PIN, HIGH);  // Turn motor on via relay
  }
  
  // Check if all tanks are full to turn motor (relay) OFF
  else if (level1 < MAX_WATER_LEVEL && level2 < MAX_WATER_LEVEL && level3 < MAX_WATER_LEVEL) {
    Serial.println("Tanks are full. Turning motor OFF...");
    digitalWrite(RELAY_PIN, LOW);  // Turn motor off via relay
  }

  // Display the water consumed after each fill cycle
  Serial.print("Tank 1 Water Consumed: ");
  Serial.println(tank1_consumed);
  Serial.print("Tank 2 Water Consumed: ");
  Serial.println(tank2_consumed);
  Serial.print("Tank 3 Water Consumed: ");
  Serial.println(tank3_consumed);

  delay(2000);  // Delay between measurements
}

Note: The pin definitions in the code (e.g., D1, D2, etc.) are placeholders and should be adjusted to match the actual GPIO pins used on the NodeESP32-C3 as per the wiring details provided above.