Flow sensor

I know I’ve made a post regarding this topic before, but it died. I’m now having trouble with it again. I have a flow sensor (http://www.swissflow.com/en/SF800/Flow_Meter_Specifications) that outputs a frequency based on the flow rate. I’m having trouble getting the Arduino to give me meaningful results. So far, I’ve tried attaching an interrupt:

unsigned long flowCount = 0;
unsigned long freq = 0;
int flowPin = 42;

void setup() {
  Serial.begin(9600);
  pinMode( flowPin, INPUT);
  attachInterrupt(flowPin, flowCounter, RISING);
}

void loop() {
  if ((millis() % 1000) == 0) { 
    freq = (flowCount / 1000);
    Serial.print("freq = ");
    Serial.println(freq); 
   flowCount = 0;
  }
}

void flowCounter() {
  flowCount++;
}

but that approach didn’t give me anything. I’ve also tried the pulseIn approach:

int durationH, durationL;
unsigned long freq = 0;
int flowPin = 42;

void setup() {
  Serial.begin(9600);
  pinMode( flowPin, INPUT);
}

void loop() {
  durationH = pulseIn(flowPin, HIGH);
  durationL = pulseIn(flowPin, LOW);
  freq = 1000000/(durationH + durationL);
  Serial.print("freq = ");
  Serial.println(freq);  }
}

but all that returned was the value 4294967295 over and over again. I don’t need to know the exact flow rate or anything. I just need to know if liquid is flowing at all.

Does anyone have any suggestions?

From the vendor's web site...

Output frequency: 100 to 2000 Hz (depending on the flow velocity)

freq = (flowCount / 1000);

...this is an integer division; fractions are truncated. freq will only ever have three values: zero, one, or two. For about half the range, freq is zero.

Print flowCount instead of freq. Is the value between 100 and 2000?

In loop, you will need to protect flowCount by disabling interrupts.

Does any of that help?

Also, there may be some useful information in the Water Flow Gauge project in Practical Arduino:

http://www.practicalarduino.com/projects/water-flow-gauge

That one uses an interrupt to detect the pulses.

Jon Freetronics: www.freetronics.com

Ok, I've tried both approaches (by jonathan and coding badly), but neither have produced results. @jonathan: I tweaked my code to mimic the information on the site you provided:

float flowRate = 0;
volatile byte flowCount = 0;
unsigned long freq = 0;
int flowPin = 0, analog;
unsigned long time, volume;


void setup() {
  Serial.begin(9600);
  pinMode( flowPin, INPUT);
  attachInterrupt(flowPin, flowCounter, FALLING);
}

void loop() {
  analog = analogRead(8);
  if( analog > 0) {
    
    Serial.print("              Analog val: ");
      Serial.println(analog);
  }
  if ((millis() - time) >= 1000) {
    detachInterrupt(flowPin);
    flowRate = (1000/(millis() - time))*flowCount;
    time = millis();
    volume = (flowCount/6100)/1000;      //ml
    Serial.print("freq = ");
    Serial.println(flowRate); 

    Serial.print("volume = ");
    Serial.println(volume);
    flowCount = 0;
    attachInterrupt(flowPin, flowCounter, FALLING);
  }
}

void flowCounter() {
  flowCount++;
}

and I didn't get any results whatsoever. Everything was zeros, except for the analogRead I added: that just returned 1s constantly.

@Coding Badly:

In loop, you will need to protect flowCount by disabling interrupts.

If I do this, flowCount won't increment because the function to increment flowCount is only called inside of an interrupt. And I did try just printing flowCount, but I was still getting zeros.

The discussion so far has been about code, and we've just been assuming that the hardware is OK. How do you have the sensor connected? Can you test it in isolation from the Arduino, such as by connecting an oscilloscope to the output? A schematic of your setup would be handy: even a photo of a rough hand-drawn schematic is better than nothing.

Jon Practical Arduino: www.practicalarduino.com

Does this make more sense…

In loop, you will need to protect flowCount by disabling interrupts while the value is copied to a local variable.

Yes, if you mean that I need to detach the interrupt, read the value, then re-attach the interrupt when I'm done reading the variable.

Another question: would it be practical to have three of these sensors, each using different counters, along with a large program that's doing serial reading and printing for wireless communication?

Hey, interesting project. I would also suggest testing the hookup first, say, with a simple code. Say hook the meter to a digital input. If it shows HIGH, display on LCD HIGH, same for low. Then blow through the meter to see if your LCD changes at all. This requires no oscilloscope, although a digital scope is always a bless. I presume you have a little rotating part inside the meter to give the pulse like a SPST switch in a rotational encorder.

By the way, how much did you pay for your meter? I'm interested in getting one now that I've seem what it is.

Well, that would work for the type of sensor you’re talking about, but this flow sensor doesn’t have a small rotating piece inside. The one I have is an IR sensor, so blowing through it doesn’t do anything.

OK. Scrap that. Any chance to subject it with water flow and just test its frequency output on an arduino pin for starters?