Pulse Output Water Flow Sensor - Frequency Counter


I need to use a Water Flow Sensor like this one in order to measure water consumption. Some considerations:

  1. The sensor outputs a square signal which frequency is proportional to the water flow going through the sensor. The pulses are NOT proportional to a specific amount of water, so pulses can NOT be counted to obtain a final amount of water. The frequency and the flow rate are related on the equation Freq = (GPM/K) - offset. The only way to measure water consumption is to calculate it by multiplying the GPM during an elapsed time.

My current approach to obtain the frequency of the sensor is to write a Frequency Counter and implement it on the Spark Core, can anyone help me on how to write a frequency counter for the spark? Any link or hints on how to implement it.

2.The sensor needs 8-24 VDC to operate correctly.

Any ideas on how to connect the sensor to a Digital Pin of the core? I have thought about using a voltage divider, however I later want to run the system on batteries, so I think that a voltage divider might not be the best approach on this.

Best Regards.

Hey, that sounds like an interesting project.
I don’t think I can be of much help with the first two points, as I lack the knowledge. Providing the input power can be done by using a voltage regulator I guess. Your Core uses one to regulate its 5V input to 3.3V which is needed. I don’t see why you can’t do the same for 8-24 VDC to 5VDC. Should work, although it doesn’t hurt to ask some confirmation from some of the other forum members.

Furthermore, I’ve recategorised your topic since you’re asking how to make something work, rather than explaining how to do so (which is what the tutorial category is meant for, if I’m not mistaken). To prevent confusion for others, I’ve placed it in the “Hardware” category, where I believe it’ll be more relevant. Should the project come to a successful end (which I surely hope for) we can always move it back to serve as a guide for others.

Best of luck!

@sparklos, from what I can see from the datasheet, the frequency output range of the device is 3.2-200Hz which is slow enough do measure using polling depending on what else you are doing in your loop() function. You could also use interrupts. I found what appears to be a great Arduino frequency counter library that can operate in both polling and interrupt mode. It should be straight forward to port to the Spark (which I can do if you wish).

As for powering the device, you will need a voltage regulator to get either 5v or 3.3v to power the Spark from the 8-35V you use for the sensor. The tricky part will be level shifting the 8-35V (depending on the supply voltage) output to 3.3v. This calls for high voltage to low voltage level shifter between the device and the Spark. Some research is required to identify the correct part. :smile:


I think @peekay123 is spot on here with his advice. I would use a rising edge interrupt and measure the difference in millis() from one rising edge to the next to get the period of sensor input. Since Freq = GPM/K - offset, then solving for GPM, you would do GPM = K/Period + K*Offset.

1 Like

An optocoupler is one way to connect the sensor signal to your Core. It only takes a few mA to power the LED inside it, so it should be good for using with batteries.

1 Like

@ih57452, excellent point! From the datasheet, the device can sink 40ma when it is active (low) so there is plenty to sink an LED current and create a nice isolation at the same time :stuck_out_tongue:

@peekay123 : Thank you a lot for researching and finding the Arduino Frequency Counter Library it comes in completely handy, and I would be more than thankful with you if you could port the library to the Spark.

Thank you as well for your suggestion on the Voltage Level Shifter.

Regarding the programming I plan on doing, I want the Spark to keep track on the measurements so they can later be sent over the cloud to a computer that will do the computations to obtain the final amount of water consumed. Hopefully this approach will save some energy from the batteries. So, as @bko suggests I think I’ll go with the Interrupt approach.

@ih57452 thank you as well for your suggestion about the optocoupler, it’s the best approach to the problem I have so far, and I’ll go with this one. Thank you again.

Regarding the battery usage, do you have any suggestions, links or info I should take into consideration before trying to run devices on batteries? I have never ran electronics on batteries and I have the feeling that there’s more than just battery connectors and voltage regulators to it.

Thank you all for your advice and help, I really appreciate it.


@sparklos, I have ported the library though I am still working on the unit test code. I am adapting @BDub’s adjustable PWM code to emulate the Arduino TimerOne PWM library code so the test code can function as intended.

As for battery operation, the Core supports various very low power deep sleep modes including one that awakens the Core when an interrupt is received on a designated pin. These sleep modes require some software design consideration as the core is reset when it is woken up so the code restarts from “zero”. HOWEVER, these modes are only available if you locally compile since they are still in testing. :smile:

Hope to see you get this up and running.

Consider using www.Ubidots.com as a place to send, store, graph, and create SMS or Email alerts based on predefined settings that you create. There is a tutorial around here that shows how to send your data from the Spark to Ubidots that was pretty easy for me to figure out as a new guy.


I haven’t started with the project yet since I need to decide some things.

I am a little confused about the connectivity the spark (and other WiFi devices) offer, for example the Spark Core offers an “easy” way of getting data from the spark throug an API, which in turn needs a Internet connection, but how about reading data of the spark core from a PC through a router (Acces Point), without the need of an internet connection??

I’m an electronics engineer with a lower-intermediate programming level, and I don’t know any of network programming, so am I choosing a too-hard path if I want the spark to communicate and exchange data with a PC through a router??

Hey @peekay123 , I’m interested in that frequency counter library port for an induction loop project I’m working on. Did you ever get that done?

@techbutler, I did port the library but stalled at porting the test code which included a variable frequency PWM library. I will see where I was at an post a link to the code when I get a chance in the next few days. :smile:

1 Like

Awesome! Thanks much.

Hi @sparklos please also keep me in the loop as I’m interested in building a similar project.

Btw the calculations for calibration (i.e. how many cycles does the water flow sensor needs per volume unit) can be done from the Ubidots cloud using the option “derived variables”

Here’s a tutorial on how to push sensor data to Ubidots (I’m also an electronics engineer - no need to be a network expert ;))

1 Like

@techbutler, I had some fun getting the library working on the Core but it’s all working now. In the absence of a variable frequency PWM library for the Core, I simplified the test program to use the basic (500Hz) PWM output and set it to 50% duty cycle. I works just fine to illustrate the interrupt driven polling the library allows. The library is on my github here. Let me know how it goes! :smile:


@peekay123 thanks a lot for the work on the library.

I’ll keep you all updated on how the project goes.


1 Like

Thanks so much @peekay123 !, Can’t wait to put it to work.

@sparklos can I ask what you are using a sensor that big on?

The largest one would team up nicely with my future mini-hydro plant I plan on throwing together next summer.

Right now I’m working with a municipal sized pulse-per-qty for frozen pipe detection in my house.

hey @JackANSI !!

Those are used for measuring water consumption of irrigation systems.


Hi. I have a related and hopefully simpler project that I am stuck. For water leak detection, I want a sensor to detect an audio frequency in the range of 8mhz, which the water pipe to my house generates when it’s running. I have hooked up a Particle Photon with an Electret Microphone Amplifier. I’m able to get it to alert me based on how loud the noise is (ie amplitude), but am not sure how to capture frequency. Any pointers on whether I need a specific think like the Arduino Frequency Counter, or some clever calculation or different hardware would be appreciated.