ESP32-C3 Controlled Water Level Monitoring and Pump Management System
View and Edit Project In Cirkit Designer
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
- Description: A microcontroller unit based on the ESP32-C3 chip, suitable for IoT applications.
- Pins: A0, RSV, GND, 3V3, EN, RST, 5V, GPIO2, GPIO19, GPIO18, GPIO9, GPIO10, GPIO3, GPIO4, GPIO5, GPIO8, RX0, TX0
MKE-S01 Ultrasonic Distance Sensor
- Description: An ultrasonic distance sensor used for measuring the distance to an object, in this case, the water level in a tank.
- Pins: GND, 5V, TRIG, ECHO
Servo
- Description: A rotary actuator or linear actuator that allows for precise control of angular or linear position.
- Pins: gnd, vcc, pulse
12V Single Channel Relay
- Description: An electrically operated switch that allows you to turn on or off a higher power circuit using a lower power signal.
- Pins: NC, COM, NO, IN, GND, VCC
Water Pump
- Description: A pump used for moving water from one place to another.
- Pins: +, -
Power Supply
- Description: Provides electrical power to the circuit.
- Pins: +, -
Wiring Details
NodeESP32-C3
- 5V connected to Servos' VCC, Ultrasonic Sensors' 5V, and Relay's VCC
- GND connected to Servos' GND, Ultrasonic Sensors' GND, Relay's GND, Water Pump's -, and Power Supply's -
- GPIO19 connected to Ultrasonic Sensor's TRIG
- GPIO18 connected to Ultrasonic Sensor's ECHO
- GPIO9 connected to Ultrasonic Sensor's TRIG
- GPIO10 connected to Ultrasonic Sensor's ECHO
- GPIO3 connected to Ultrasonic Sensor's TRIG
- GPIO4 connected to Ultrasonic Sensor's ECHO
- GPIO5 connected to Relay's IN
- GPIO8 connected to Servo's pulse
- RX0 connected to Servo's pulse
- TX0 connected to Servo's pulse
MKE-S01 Ultrasonic Distance Sensor
- 5V connected to NodeESP32-C3's 5V
- GND connected to NodeESP32-C3's GND
- TRIG connected to NodeESP32-C3's GPIO19, GPIO9, or GPIO3
- ECHO connected to NodeESP32-C3's GPIO18, GPIO10, or GPIO4
Servo
- VCC connected to NodeESP32-C3's 5V
- GND connected to NodeESP32-C3's GND
- Pulse connected to NodeESP32-C3's GPIO8, RX0, or TX0
12V Single Channel Relay
- VCC connected to NodeESP32-C3's 5V
- GND connected to NodeESP32-C3's GND
- IN connected to NodeESP32-C3's GPIO5
- COM connected to Power Supply's +
- NO connected to Water Pump's +
Water Pump
-
- connected to Relay's NO
-
- connected to NodeESP32-C3's GND
Power Supply
-
- connected to Relay's COM
-
- connected to NodeESP32-C3's GND
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.