Water Flow Sensor coding

Hi guys, I am suppose to connect a water flow sensor to calculate the water flow rate using Arduino UNO. I have connect it to the Arduino, but I don't really know how to code it out. It would be great if anyone can help me out.

We need a lot more info!
What does the sensor put out… pulses, analog, RS232 ??
.
Show us your best effort so far (in code tags), and we can move on from there.

looks like a pulse output flow sensor to me, so basically,

flow rate = number of pulses per second x flow rate constant

loads of pulse counter example codes out there!

here's one :wink:
(Compiles, NOT tested!)

#define FlowSensor_INPUT 2 //arduino interrrupt pin connected to flow sensor 
#define PERIOD 1000 //1000ms 

volatile unsigned long pulse_counter = 0;
unsigned long old_time;


void setup()
{
  Serial.begin(115200);

  // For noise suppression, enable pullup on interrupt pin
  digitalWrite(FlowSensor_INPUT, HIGH);
  old_time = millis();
  pulse_counter = 0;
  attachInterrupt(digitalPinToInterrupt(FlowSensor_INPUT), interrupt_handler, RISING);
}


void loop() {
  unsigned long  new_time = millis() - old_time;

  if (new_time >= PERIOD) {
    noInterrupts();
    unsigned long pulse_count = pulse_counter;
    pulse_counter = 0;
    interrupts();
    
    old_time = millis();
    
    float pulse_rate = (pulse_count * 1000.0) / new_time; //x1000 to converts ms to seconds

    Serial.print("Pulse Rate: ");
    Serial.println(pulse_rate, 2); //rate per second

  }
}

void interrupt_handler()
{
  pulse_counter = pulse_counter + 1;
}

hope that helps.

1 Like

ermmmm.. i dont really get what u mean

…and that’s why I tried to walk OP in slowly…

1 Like

Generally we don't write code for you in this forum. We will help you if you at least make an attempt at writing the code. You did not provide the details of your sensor or any code that you attempted. You also didn't provide details of what you expect the code to do (i.e., just display the current flow rate, display the average flow rate, etc.)

We can't magically create a program that works and does what you want.

Thanks for helping... maybe i will try it out myself first as i am quite new for this.... then i will see my progress on it

what is the purpose of using the pulse count here?

see reply #3

the picture you posted does not show the flow sensor clearly but I'm assuming its got 3 wire - power, ground and signal(pulse out)

hope that helps...

so now right, let say i want to calculate the volume of the water how should i do it ?

Is that a school project?

yes, it is

Try to find a data sheet for the flow meter. It will tell you the relationship between meter pulses and delivered volume.

If yoy cannot find a datasheet for your flow sensor, you could always pour a known volume of fluid out through the flow sensor and see how many pulses were generated... from there you can then get the 'volume per pulse' value.

hope that helps....

It's probably a good idea to do this even if you do find the data sheet just to verify that it's true for your particular hardware.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.