Example Code for Arduino-Water Flow Measurement

This article provides a comprehensive guide on measuring water flow using Arduino, including hardware and software setup, wiring diagram, and sample code for obtaining accurate flow readings.

Hardware Preparation

Software Preparation

Wiring Diagram

Sample Code

volatile double waterFlow;
void setup() {
  Serial.begin(9600);  //baudrate
  waterFlow = 0;
  attachInterrupt(1, pulse, RISING);  //DIGITAL Pin 3: Interrupt 0
}
void loop() {
  Serial.print("waterFlow:");
  Serial.print(waterFlow);
  Serial.println("   L");
  delay(500);
}

void pulse()   //measure the quantity of square wave
{
  waterFlow += 1.0 / 150.0; // 150 pulses=1L (refer to product specification)
}

Result

Print the collected water flow value.

Was this article helpful?

TOP