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

ESP8266-Based Smart Water Dispenser with Ultrasonic Sensor and Flow Monitoring

View and Edit Project In Cirkit DesignerImage of ESP8266-Based Smart Water Dispenser with Ultrasonic Sensor and Flow Monitoring

Smart Water Dispenser Circuit Documentation

Summary

This document details the design and implementation of a smart water dispenser circuit. The system uses an ESP8266 microcontroller to control a water pump, monitor water flow, and detect objects using an ultrasonic sensor. A servo motor is used to open and close the dispenser automatically when an object is detected within a 5 cm range. The water flow is measured using a flow sensor, and the pump is controlled via a relay.

Component List

  1. ESP-8266 Controller

    • Description: Microcontroller used to control the entire system.
    • Pins: A0, RSV, SD3, SD5, SD1, CMD, D0, D1, D2, D3, D4, 3V3, GND, D5, D6, SD0, CLK, RST, EN, D7, D8, RX, TX, Vin, 5V
  2. Ultrasonic Sensor

    • Description: Sensor used to detect objects within a certain range.
    • Pins: +VCC, Trigger, Echo, GND
  3. Connector 5V

    • Description: Provides 5V power supply to the circuit.
    • Pins: GND, VCC
  4. Micro Servo 9G

    • Description: Servo motor used to open and close the dispenser.
    • Pins: GND, +5V, PWM
  5. 12V Relay

    • Description: Relay used to control the water pump.
    • Pins: NO, COM, NC, IN, DC-, DC+
  6. Resistor (200 Ohms)

    • Description: Resistors used for various connections in the circuit.
    • Pins: pin1, pin2
  7. Water Flow Sensor

    • Description: Sensor used to measure the flow rate of water.
    • Pins: Signal, Vcc, GND
  8. Water Pump

    • Description: Pump used to dispense water.
    • Pins: VCC, GND

Wiring Details

ESP-8266 Controller

Ultrasonic Sensor

Connector 5V

Micro Servo 9G

12V Relay

Resistor (200 Ohms)

Water Flow Sensor

Water Pump

Code Documentation

/*
 * Dispenser Cerdas
 * Kode ini mengontrol dispenser air pintar menggunakan mikrokontroler ESP8266.
 * Sistem memantau dan mengeluarkan air, menghitung total air yang dikeluarkan
 * per menit (untuk kepentingan IOT monitoring akan tampil di layar device),
 * dan menggunakan servo untuk membuka dispenser secara otomatis ketika
 * objek terdeteksi dalam jarak 5 cm oleh sensor ultrasonik. Pompa air
 * dikendalikan melalui relai untuk memompa air melalui sensor aliran.
 */

#include <Servo.h>

// Pin definitions
const int trigPin = D1;
const int echoPin = D5;
const int servoPin = D2;
const int relayPin = D6;
const int flowSensorPin = D7;

// Variables
Servo myServo;
volatile int flowPulseCount = 0;
unsigned long oldTime = 0;
float flowRate = 0.0;
float totalFlow = 0.0;

void IRAM_ATTR pulseCounter() {
  flowPulseCount++;
}

void setup() {
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(relayPin, OUTPUT);
  pinMode(flowSensorPin, INPUT);
  myServo.attach(servoPin);
  attachInterrupt(digitalPinToInterrupt(flowSensorPin), pulseCounter, FALLING);
  myServo.write(0); // Close dispenser initially
  digitalWrite(relayPin, LOW); // Ensure pump is off initially
}

void loop() {
  // Measure distance using ultrasonic sensor
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;

  if (distance <= 5) {
    myServo.write(90); // Open dispenser
    digitalWrite(relayPin, HIGH); // Turn on pump
  } else {
    myServo.write(0); // Close dispenser
    digitalWrite(relayPin, LOW); // Turn off pump
  }

  // Calculate flow rate and total flow
  if ((millis() - oldTime) > 1000) { // Every second
    detachInterrupt(digitalPinToInterrupt(flowSensorPin));
    flowRate = ((1000.0 / (millis() - oldTime)) * flowPulseCount) / 7.5;
    oldTime = millis();
    totalFlow += flowRate / 60.0;
    flowPulseCount = 0;
    attachInterrupt(digitalPinToInterrupt(flowSensorPin), pulseCounter, FALLING);
    Serial.print("Flow rate: ");
    Serial.print(flowRate);
    Serial.print(" L/min");
    Serial.print("  Total: ");
    Serial.print(totalFlow);
    Serial.println(" L");
  }
}

This code controls the smart water dispenser using the ESP8266 microcontroller. It monitors the water flow, calculates the total water dispensed, and uses a servo motor to open the dispenser when an object is detected within 5 cm by the ultrasonic sensor. The water pump is controlled via a relay.