This circuit involves an Arduino UNO, an ESP8266 module, a water sensor, and a water flow rate sensor (YF-S401). The Arduino UNO serves as the main microcontroller, interfacing with the water sensor and the water flow rate sensor to read and process data. The ESP8266 module is powered by the Arduino UNO but is not directly interfaced in this circuit.
const int waterSensorPin = A0; // Pin connected to the water sensor signal
void setup() {
Serial.begin(9600); // Start serial communication for debugging
}
void loop() {
int sensorValue = analogRead(waterSensorPin); // Read the analog value from the water sensor
Serial.print("Water Sensor Value: ");
Serial.println(sensorValue); // Print the sensor value to the Serial Monitor
delay(1000); // Wait for 1 second before taking another reading
}
const int flowSensorPin = 2; // Pin connected to the flow sensor signal
volatile int pulseCount = 0;
unsigned long oldTime = 0;
float flowRate = 0.0;
void setup() {
pinMode(flowSensorPin, INPUT_PULLUP); // Set the flow sensor pin as an input with an internal pull-up resistor
attachInterrupt(digitalPinToInterrupt(flowSensorPin), pulseCounter, FALLING); // Attach an interrupt to the flow sensor pin
Serial.begin(9600); // Start serial communication for debugging
}
void loop() {
if ((millis() - oldTime) > 1000) { // Only process once per second
detachInterrupt(digitalPinToInterrupt(flowSensorPin)); // Disable the interrupt while calculating
// Calculate the flow rate in liters per minute
flowRate = (pulseCount / 7.5); // YF-S401: 7.5 pulses per second per liter/minute
Serial.print("Flow rate: ");
Serial.print(flowRate);
Serial.println(" L/min");
pulseCount = 0; // Reset the pulse count
oldTime = millis(); // Update the time
attachInterrupt(digitalPinToInterrupt(flowSensorPin), pulseCounter, FALLING); // Re-enable the interrupt
}
}
void pulseCounter() {
pulseCount++; // Increment the pulse count
}