# Arduino + flow sensor

I'm trying to use a flow sensor (http://www.swissflow.com/en/SF800/Flow_Meter_Specifications) to measure the flow of liquid, but I can't seem to get very good data. I had a constant stream of water through the tube and I figured it was flowing at about 2.5 L/s. Can anyone see why I'd get data like this:

0 0 0 0 0 61 0 0 0 0 0 0 354 0 0 0 0 0 0 0 0 0 2 763 0 0...

I just had the data line of the flow sensor plugged in to an analog input pin. Any help would be much appreciated

Well, depending on how quickly you were reading the analog signal it looks like you're reading a random point on the output waveform (square wave?) of the sensor.

Not much detail in the way of technical information but I believe the way this sensor is intended to be used is to count the pulses on the output. Each count will indicate 1/6100ths of a Litre. I expect you should be using a digital input and an interrupt configured for rising edge.

Well your first error is trying to read it as a analog input voltage. The limited spec sheet says it outputs a frequency, so you will have to wire it to a digital input pin and have a frequency counter function written or installed in your sketch. No wiring diagram shown so I would assume it's three wires, voltage excitation (5-12vdc, so use the Arduino +5vdc pin), ground wire and digital frequency signal wire.

Lefty

Note this sensor has a minimum flow rate. That is anything below 0.5 l/m will not be recorded accurately. I imagine you will still get 100Hz out of the sensor when there is nothing flowing.

...so you will have to wire it to a digital input pin and have a frequency counter function written or installed in your sketch

I don't understand what you mean by a frequency counter function. How would I go about doing this?

Thank you all for helping me!

I don't understand what you mean by a frequency counter function. How would I go about doing this?

Well your flow meter outputs a frequency that is proportional to the flow rate, so you will have to be able to measure the frequency to determine the flow rate in your code. There are frequency counter software libraries around and there are also Arduino statements ( see: http://arduino.cc/en/Reference/PulseIn) that can be used to obtain frequency information.

Lefty

Well, if you get pulses out for a fixed displacement of fluid (which the K-count implies) then counting pulses is the same as counting small packets of fluid.

A counting function...

``````static unsigned long TotalFlow = 0;  // measures how many 1/6100th Litres of fluid have passed since sketch started
...
void LittleBitOfFlow()
{
TotalFlow++;
}
``````

Hook the output to pin 13 and then in your sketch setup...

``````attachInterrupt(0, LittleBitOfFlow, RISING);
``````

This works if you're trying to measure flow volume. If you want to measure flow-rate you need to introduce the measure of time to the calculation.

On another note, this flow meter is only rated to 20 L / min. Your original post said you thought you were running at 20 L / s (1200 L / min). Typo?

@Mitch: Yes, it was a typo. I meant 2.5 L/min. Haha, big difference there... Also, how would I know when to call this?

I'm using this flow sensor to determine if there is still liquid flowing (if the liquid's reservoir is empty), so I don't need an extremely accurate value

You don't call it. It calls itself. That's what an interrupt does. Check the reference material for some more info on interrupts.

I get that, but I guess I am confused as to how to use this to achieve the goal. It’ll end up counting spurts, but I don’t know where to place it in the program. I’ll think about it some more. Thanks for all of the help!

• First you need to understand the signal your sensor is outputting. It rises and falls. By counting the rises (or for that matter the falls) you can then calculate the flow rate I.E (You get 1000 pulses per second (1000Hz) means you are flowing X amount).

-When you setup an interrupt with a rising edge, the interrupt gets called when it sees a rise on that digital pin (its called an interrupt because it pulls the processor away from whatever it is doing to focus on this digital pin). You can then set a counter inside the interrupt. The program will go back to doing its thing, until the interrupt is called again (when the signal of the sensor goes from low back to high). Then you compare your previous time (last pulse), with the new time (current pulse), do your math to calculate pulses per second, and then figure your flow rate based on those pulses per second (Hz).
(NOTE: Try to do as little as possible inside an interrupt, as the processor is being pulled from other tasks, and interrupts are disabled when the processor is inside a current interrupt)

• If you want HIGH frequency output you can take that reading after figuring your flow rate and output/update it straight away. If your reading it on some sort of an output the number may bounce so much you wont be able to read it easily.
• If you want a more consistent output you can take several readings, average them, and output/update them every so often. This gives you some control over things, and tends to give better results.
• You could throw in an error control where if the reading comes back outside of a certain range, you disregard it.
• If your doing a lot of other calculations and work, and dont need REAL quick updating (meaing near instant), then you can setup a flag or some control, so that the interrupt is only active every 250ms, for you to get one or two readings, and then its off for another 250ms, giving the processor plenty of time to do a million other tasks your asking it to perform.

Just some clues and helps. Good Luck!

A little external hardware would make this SO much easier....

And with the frequencies that the meter delivers, perhaps "necessary"??

Frequency-to-voltage converters must be easy to find via Google.

=== Note what GM said about it not "working" to detect NO flow. And I note that you said you only wanted to know if resevoir was empty. What's wrong with a float switch?

But... for other projects... :-) ... what do these cost?

Thanks for all of the help! @Mitch: I tried using the attachInterrupt command in the setup, but it didn't record anything. But, when I tried the following code:

``````  flow = digitalRead(flowPin);
if ( flow == 1) {
flowCount = flowCount + 1;
}
time = millis();
time = long(time);
time = unsigned(time);
time = time/1000;
freq = (flowCount / time);
mod = millis() % 1000;
if (mod == 0) {
Serial.print("freq = ");
Serial.println(freq);
Serial.print("flowCount = ");
Serial.println(flowCount);
Serial.print("time = ");
Serial.println(time);
}
``````

I did get some values that are possible values. Does this make sense logically?

@tkbyd: I don't think a float sensor would work because the reservoir will be moved around and jostled while testing. The sensor I'm using cost about \$30.

Does this make sense logically?

Well it's a bit clack handed and doesn't synchronise to the start of a pulse and just drops through when the pulse is low.

Why not use the simple PulseIn() function.

http://www.arduino.cc/en/Reference/PulseIn