Having trouble with flow meter still showing value when not running

I am trying to calculate volume. Basically I want to pump x amount of gallons out of my aquarium every x minutes.

This is my first time using a flow meter and I am a little lost. I have found some code that I pieced together and made work but the flow rate never resets to zero even when water is not flowing.

I’m probably way off with this but what I was trying to do to get volume is calculate the flow rate and add the number every loop to get a total. Problem is that it will not count anything if flow rate is less than 1 and when the flow stops the flowrate number does not reset to zero.

// This #include statement was automatically added by the Particle IDE.
#include <SparkIntervalTimer.h>

    Water flow sensor test sketch



IntervalTimer msTimer;	

volatile uint32_t usLastTime  = 0;
volatile uint32_t usDeltaTime = 0;
volatile uint32_t msCount     = 0;
volatile double   revPerSec   = 0;
volatile double   revPerMS    = 0;
 float flowRate =0.0;
 float litersPerMinute=0.0;

void msISR() {
  revPerMS = msCount;
  msCount = 0;

void senseISR() {
  uint32_t us = micros();
  usDeltaTime = us - usLastTime;
  usLastTime = us;
  revPerSec =  1000000.0 / usDeltaTime;

// This #include statement was automatically added by the Particle IDE.
#include "Adafruit_SSD1306/Adafruit_SSD1306.h"

Particle Maker Kit Tutorial #2: Next Bus Alert

This tutorial uses a Particle Photon and the OLED screen from the Particle
Maker Kit. It uses a webhook to retrieve bus prediction times from the
NextBus Public XML feed, which must be set up first along with the webhook.
See http://docs.particle.io/tutorials/topics/maker-kit to learn how!

NOTE: This code example requires the Adafruit_SSD1306 library to be included,
so make sure to add it via the Libraries tab in the left sidebar.

// use hardware SPI
#define OLED_DC     D3
#define OLED_CS     D4
#define OLED_RESET  D5
Adafruit_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS);
int i = 0;
int totalFlow = 0;

void setup()   {
msTimer.begin(msISR, 1000, uSec);  // trigger every 1000µs

attachInterrupt(FLOWSENSORPIN, senseISR, FALLING);

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)

  display.setTextSize(2);       // text size
  display.setTextColor(WHITE); // text color


void loop() {
  flowRate = (revPerSec * 2.25);        //Take counted pulses in the last second and multiply by 2.25mL 
  flowRate = flowRate * 60;         //Convert seconds to minutes, giving you mL / Minute
  litersPerMinute = flowRate / 1000;       //Convert mL to Liters, giving you Liters / Minute
  totalFlow = totalFlow + litersPerMinute;



What meter are you using ?

The reason I ask is because many of the cheap pulse output meters are already volume based, even though the datasheet expresses as a frequency. Each pulse represents a give volume.

You can skip the frequency& Interval Library and just count pulses in your ISR.
I use a counter for a totalizer and a counterPerInterval (if flow calculation is required) with good-ole millis().
Reset the counterPerInterval back to zero after a flow rate calculation over any given time interval.

If your meter truly operates as a flow meter, then dis-regard what I’ve mentioned.

Model: Of05ZAT

Datasheet says 0.46mL/P
Or that’s 8228 Pulses per gallons.

Start your pump and watch for a volatile int counter to reach 8228 or more (assuming you want to pump 1 gallon). Reset Counter before next pumping interval.
You may need to Change your interrupt to RISING if interrupt doesn’t fire.
After you get your CODE working, you can add safety checks such as maxRunTime, etc.

Not tested, but this might get you started:


unsigned long interval =   60 * 60 * 1000;   // How often to Pump the Water
unsigned long previousMillis =      0   ;    // store last Pumping Time
volatile int waterPulse =   0;               // Pulse Counter
int targetPulse         =  8228;             //  8228 Pulses = 1 Gallon.  Verify with a pump test

void setup() {

  // Flow Meter
  pinMode(D6, INPUT_PULLUP);         // THE WATER METER 
  attachInterrupt(D6, flow, FALLING);   // Interrupt for Water Meter
}  // End Setup

void loop() {

  if (millis()  - previousMillis >= interval) {
    pumpWater();      // Pump the Water Down
} // End LOOP

void pumpWater() {
  previousMillis = millis();
  // Turn ON your Pump Here
  while ( waterPulse <=  targetPulse  )    {
  // Turn off your Pump Here
  waterPulse = 0;   // reset the pulse counter

void flow ()   //  ISR

Thanks so much. This has me on the right track now.