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

Wi-Fi Controlled Water Management System with ESP32 and Ultrasonic Sensor

View and Edit Project In Cirkit DesignerImage of Wi-Fi Controlled Water Management System with ESP32 and Ultrasonic Sensor

Circuit Documentation

Summary

This circuit integrates an ultrasonic sensor, a water flow sensor, a solenoid valve, a water pump, and relays controlled by an ESP32 microcontroller. The ultrasonic sensor measures distance to control relays, while the water flow sensor measures flow rate to control a solenoid valve. The ESP32 also hosts a web interface for setting a volume threshold for the water flow sensor.

Component List

  1. HC-SR04 Ultrasonic Sensor

    • Pins: VCC, TRIG, ECHO, GND
    • Description: Measures distance using ultrasonic waves.
    • Purpose in Circuit: Controls relays based on distance measurements.
  2. Plastic Solenoid Valve

    • Pins: pin1, pin2
    • Description: Controls the flow of water.
    • Purpose in Circuit: Opens or closes based on relay control.
  3. ESP32 DEVKIT V1 (30 pins)

    • Pins: EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, GND, VIN, 3V3, D15, D2, D4, RX2, TX2, D5, D18, D19, D21, RX0, TX0, D22, D23
    • Description: Microcontroller with WiFi capabilities.
    • Purpose in Circuit: Controls sensors and relays, hosts web interface.
  4. 5V Relay

    • Pins: Normally Open, Common terminal, Normally Closed, In, GND, VCC
    • Description: Electromechanical switch.
    • Purpose in Circuit: Controls solenoid valve and water pump.
  5. Power Jack

    • Pins: POSITIF, NEGATIF
    • Description: Provides power to the circuit.
    • Purpose in Circuit: Supplies power to components.
  6. Diode

    • Pins: cathode, anode
    • Description: Allows current to flow in one direction.
    • Purpose in Circuit: Protects components from reverse voltage.
  7. YF-S201 Water Flow Meter

    • Pins: SIG, GND, VCC
    • Description: Measures water flow rate.
    • Purpose in Circuit: Provides flow rate data to the microcontroller.
  8. Water Pump

    • Pins: VCC, GND
    • Description: Pumps water.
    • Purpose in Circuit: Controlled by relay to pump water.

Wiring Details

HC-SR04 Ultrasonic Sensor

Plastic Solenoid Valve

ESP32 DEVKIT V1 (30 pins)

5V Relay (First)

5V Relay (Second)

Power Jack

Diode (First)

Diode (Second)

YF-S201 Water Flow Meter

Water Pump

Code Documentation

SWMS.ino

// This Arduino Sketch integrates an ultrasonic sensor and a water flow sensor.
// The ultrasonic sensor controls GPIO 13 and GPIO 12 based on distance.
// A web page allows the user to set a volume threshold for the water flow sensor.
// GPIO 14 is controlled based on the water flow sensor's readings.

#include <WiFi.h>
#include <WebServer.h>
#include "WebUI.h"

#define FLOW_SENSOR_PIN 5
#define TRIG_PIN 4
#define ECHO_PIN 2
#define RELAY_PIN_1 13
#define RELAY_PIN_2 12
#define RELAY_PIN_3 14

volatile int pulseCount = 0;
float flowRate = 0.0;
unsigned long oldTime = 0;
int volumeThreshold = 0;
WebServer server(80);

void IRAM_ATTR pulseCounter() {
  pulseCount++;
}

void handleRoot() {
  String html = "<form action='/setThreshold' method='POST'>";
  html += "Volume Threshold (mL): <input type='number' name='threshold'><br>";
  html += "<input type='submit' value='Apply'></form>";
  server.send(200, "text/html", html);
}

void handleSetThreshold() {
  if (server.hasArg("threshold")) {
    volumeThreshold = server.arg("threshold").toInt();
    server.send(200, "text/html", "Threshold set to " + String(volumeThreshold) + " mL");
  } else {
    server.send(400, "text/html", "Invalid Request");
  }
}

void setup() {
  Serial.begin(115200);
  pinMode(FLOW_SENSOR_PIN, INPUT);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(RELAY_PIN_1, OUTPUT);
  pinMode(RELAY_PIN_2, OUTPUT);
  pinMode(RELAY_PIN_3, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(FLOW_SENSOR_PIN), pulseCounter, RISING);
  oldTime = millis();
  WiFi.begin("yourSSID", "yourPASSWORD");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
  server.on("/", handleRoot);
  server.on("/setThreshold", HTTP_POST, handleSetThreshold);
  server.begin();
}

void loop() {
  server.handleClient();
  unsigned long currentTime = millis();
  unsigned long elapsedTime = currentTime - oldTime;
  if (elapsedTime >= 1000) {
    flowRate = ((1000.0 / elapsedTime) * pulseCount) / 7.5;
    oldTime = currentTime;
    pulseCount = 0;
    Serial.printf("Flow rate: %.2f L/min\n", flowRate);
    if (flowRate * 1000 >= volumeThreshold) {
      digitalWrite(RELAY_PIN_3, LOW);
    } else {
      digitalWrite(RELAY_PIN_3, HIGH);
    }
  }
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  long duration = pulseIn(ECHO_PIN, HIGH);
  float