ESP8266-Based Smart Water Dispenser with Ultrasonic Sensor and Flow Monitoring
View and Edit Project In Cirkit Designer
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
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
Ultrasonic Sensor
- Description: Sensor used to detect objects within a certain range.
- Pins: +VCC, Trigger, Echo, GND
Connector 5V
- Description: Provides 5V power supply to the circuit.
- Pins: GND, VCC
Micro Servo 9G
- Description: Servo motor used to open and close the dispenser.
- Pins: GND, +5V, PWM
12V Relay
- Description: Relay used to control the water pump.
- Pins: NO, COM, NC, IN, DC-, DC+
Resistor (200 Ohms)
- Description: Resistors used for various connections in the circuit.
- Pins: pin1, pin2
Water Flow Sensor
- Description: Sensor used to measure the flow rate of water.
- Pins: Signal, Vcc, GND
Water Pump
- Description: Pump used to dispense water.
- Pins: VCC, GND
Wiring Details
ESP-8266 Controller
- D1: Connected to Trigger pin of the Ultrasonic Sensor.
- D2: Connected to PWM pin of the Micro Servo 9G.
- D5: Connected to Echo pin of the Ultrasonic Sensor through a 200 Ohm resistor.
- D6: Connected to IN pin of the 12V Relay through a 200 Ohm resistor.
- D7: Connected to Signal pin of the Water Flow Sensor through a 200 Ohm resistor.
- Vin: Connected to Vcc pin of the Water Flow Sensor, DC+ pin of the 12V Relay, +5V pin of the Micro Servo 9G, COM pin of the 12V Relay, +VCC pin of the Ultrasonic Sensor, and VCC pin of the Connector 5V.
- GND: Connected to GND pin of the Water Flow Sensor, DC- pin of the 12V Relay, GND pin of the Micro Servo 9G, GND pin of the Ultrasonic Sensor, GND pin of the Water Pump, and GND pin of the Connector 5V.
Ultrasonic Sensor
- Trigger: Connected to D1 pin of the ESP-8266 Controller.
- Echo: Connected to D5 pin of the ESP-8266 Controller through a 200 Ohm resistor.
- +VCC: Connected to Vin pin of the ESP-8266 Controller.
- GND: Connected to GND pin of the ESP-8266 Controller.
Connector 5V
- VCC: Connected to Vin pin of the ESP-8266 Controller.
- GND: Connected to GND pin of the ESP-8266 Controller.
Micro Servo 9G
- PWM: Connected to D2 pin of the ESP-8266 Controller.
- +5V: Connected to Vin pin of the ESP-8266 Controller.
- GND: Connected to GND pin of the ESP-8266 Controller.
12V Relay
- IN: Connected to D6 pin of the ESP-8266 Controller through a 200 Ohm resistor.
- DC+: Connected to Vin pin of the ESP-8266 Controller.
- DC-: Connected to GND pin of the ESP-8266 Controller.
- COM: Connected to Vin pin of the ESP-8266 Controller.
- NO: Connected to VCC pin of the Water Pump.
Resistor (200 Ohms)
- Resistor 1: Connected between D7 pin of the ESP-8266 Controller and Signal pin of the Water Flow Sensor.
- Resistor 2: Connected between D6 pin of the ESP-8266 Controller and IN pin of the 12V Relay.
- Resistor 3: Connected between D5 pin of the ESP-8266 Controller and Echo pin of the Ultrasonic Sensor.
Water Flow Sensor
- Signal: Connected to D7 pin of the ESP-8266 Controller through a 200 Ohm resistor.
- Vcc: Connected to Vin pin of the ESP-8266 Controller.
- GND: Connected to GND pin of the ESP-8266 Controller.
Water Pump
- VCC: Connected to NO pin of the 12V Relay.
- GND: Connected to GND pin of the ESP-8266 Controller.
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.