improving flow sensor output

Whenever I'm using interrupts INT0 and INT1 on the Arduino Uno, and signal pin 2 and 3. I get good flow numbers on these digital inputs on the board, but once I try to move the output pin to 4-13, I get some bogus flow numbers. I would like to use the flow sensor outputs on the higher pins, instead of sticking with digital pins 2 or 3.

For example, the code will alway set flow to 0 until the flow sensor is spinning the internal wheel (thru air or water flow), but that only happens with #2 or #3 and their associated interrupts. So how can I get the 2 built-in interrupts to help reset flow numbers and show reasonably accurate flow rates when I start moving up the line on pins 4 and higher? They don't seem to work with those pin numbers.

I'm using the original flow sensor code given by Arvind Sanjeev at DIYhacking.com. It works pretty good for what I'm doing. But I want to use several flow sensors and get different flow rates based on different water valves they will be monitoring.

Hi,

Look at this link; I think it will help you!!!

C:\Arduino\reference\arduino.cc\en\Reference\AttachInterrupt.html

I would like to use the flow sensor outputs on the higher pins, instead of sticking with digital pins 2 or 3.

On the UNO you will have to use pin change interrupts. If you are a "newbie" you could use a library. This one GitHub - NicoHood/PinChangeInterrupt: A simple & compact PinChangeInterrupt library for Arduino
is pretty good and user friendly. It's also available through the library manager with the IDE.

I used the link to github and Ardunio IDE automatically added it in to the program after going to (S)ketch, Include Library menu:

#include <PinChangeInterruptPins.h>
#include <PinChangeInterrupt.h>
#include <PinChangeInterruptSettings.h>
#include <PinChangeInterruptBoards.h>

So that part is in.

Now I moved flow sensor signal wire to pin 4, so I think I would have to add this to the program:

#define sensorPin 4
....
....
....

and then put in:

void setup()
{
attachPinChangeInterrupt();

// Initialize a serial connection for reporting values to the host
Serial.begin(38400);

pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);

...
...
...

I know the instructions are clear for the LED example, however in the function attachPinChangeInterrupt(), isn't there going something in the parenthesis to associate the interrupt with the sensor wire plugged into pin4? Or do you just add "void" in there?

The syntax to use is similar to the attachInterrupt() syntax, but use attachPinChangeInterrupt().

In your case using the model of the Sanjeev code you linked, with the #define sensorPin 4, with the pulseCounter interrupt service routine, and a FALLING trigger the syntax will be like this

attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(sensorPin), pulseCounter, FALLING);

There is a good Readme explanation about the library on GitHub, and it is worth reading.

Thanks for the info. I've changed to code which appears to show progress.

I'm slowly digesting some of this complex stuff on interrupts, but learning curve is steep for me. Previously I've been going around in circles, thinking I had a hardware problem when it was just this matter of getting interrupts to work inside code with the pulse signals coming in onto the Arduino board.

I haven't done any real programming since school, but now I see the need to have software and hardware operate together for a 'smart' project.

I'm getting started on this concept, it will be an irrigation system with 4 flow sensors, eventually to upload data into a cloud server for monitoring water use:

http://tinyurl.com/jby5g7w

I've installed plug n' play hardware with stuff already on the market: ethernet gateways, wireless pulse sensors and pulse-output water meters. It's really awesome when you see it come together on the cloud with graphs to show real time sensor data. And SMS text alerts when thresholds are set. Basic IoT technology.

Previously I had no clue how that was working internally. Until now, and I want to keep learning this system and Arduino is really helping me to get started.

From the github ReadMe, I see: "A PinChangeInterrupt will only be triggered for the attached pins per port. Meaning if you set PCINT for a pin and another pin on the same port is changing a lot it will not interrupt your code.. PinChangeInterrupts are different than normal Interrupts."

so I define the I/O pin as a constant with:

#define 4

and likewise I commented out the original attachInterrupt with the new attachPinChangeInterrupt:

// attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(sensorPin), pulseCounter, FALLING);

likewise for detaching:

// detachInterrupt(sensorInterrupt);
detachPinChangeInterrupt(digitalPinToPinChangeInterrupt(sensorPin);

However as I'm not using the external interrupts on the Uno (0 corresponding to GPIO pin 2, 1 corresponding to GPIO pin 3), is this saying I've just internally assigned pin#4 as a signal input pin and an interrupt pin into the same address?

It looks like my flow rate now is either getting reset back to 0 or gets stuck on a nominal value until it detects flow again. Which is to say I'm almost there, that is, moving the signal to higher pin numbers without being limited to the 2 default Arduino external interrupts.

I'll keep going over the ReadMe stuff on interrupts but it's not all clear to me just yet.

thanks for the help!

However as I'm not using the external interrupts on the Uno (0 corresponding to GPIO pin 2, 1 corresponding to GPIO pin 3), is this saying I've just internally assigned pin#4 as a signal input pin and an interrupt pin into the same address?

I'm not sure what you are asking but you are making pin 4 an input pin, and there is a pin change interrupt activated by that pin. Its the same as making pin 2 and input pin for an external interrupt activated by that pin.

Here is the code from your original DIYhacking reference converted to a pin change interrupt. I tested it with a 45 Hz square wave on pin 9 jumpered to pin 4 and see the 10l/min expected flow.

#include <PinChangeInterrupt.h>
#include <PinChangeInterruptBoards.h>
#include <PinChangeInterruptPins.h>
#include <PinChangeInterruptSettings.h>

/*
  Liquid flow rate sensor -DIYhacking.com Arvind Sanjeev

  Measure the liquid/water flow rate using this code.
  Connect Vcc and Gnd of sensor to arduino, and the
  signal line to arduino digital pin 2.

*/


byte statusLed    = 13;

//byte sensorInterrupt = 0;  // 0 = digital pin 2
byte sensorPin       = 4;

// The hall-effect flow sensor outputs approximately 4.5 pulses per second per
// litre/minute of flow.
float calibrationFactor = 4.5;

volatile byte pulseCount;

float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;

unsigned long oldTime;

void setup()
{

  // Initialize a serial connection for reporting values to the host
  Serial.begin(38400);

  // Set up the status LED line as an output
  pinMode(statusLed, OUTPUT);
  digitalWrite(statusLed, HIGH);  // We have an active-low LED attached

  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);

  pulseCount        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0;
  totalMilliLitres  = 0;
  oldTime           = 0;

  // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
  // Configured to trigger on a FALLING state change (transition from HIGH
  // state to LOW state)
  //attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(sensorPin), pulseCounter, FALLING);

  // tone(9,45);// 45 Hz test pulse for 10 l/min jumper pin 9 to pin 4
}

/**
   Main program loop
*/
void loop()
{

  if ((millis() - oldTime) > 1000)   // Only process counters once per second
  {
    // Disable the interrupt while calculating flow rate and sending the value to
    // the host
    // detachInterrupt(sensorInterrupt);
    detachPinChangeInterrupt(digitalPinToPinChangeInterrupt(sensorPin));

    // Because this loop may not complete in exactly 1 second intervals we calculate
    // the number of milliseconds that have passed since the last execution and use
    // that to scale the output. We also apply the calibrationFactor to scale the output
    // based on the number of pulses per second per units of measure (litres/minute in
    // this case) coming from the sensor.
    flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;

    // Note the time this processing pass was executed. Note that because we've
    // disabled interrupts the millis() function won't actually be incrementing right
    // at this point, but it will still return the value it was set to just before
    // interrupts went away.
    oldTime = millis();

    // Divide the flow rate in litres/minute by 60 to determine how many litres have
    // passed through the sensor in this 1 second interval, then multiply by 1000 to
    // convert to millilitres.
    flowMilliLitres = (flowRate / 60) * 1000;

    // Add the millilitres passed in this second to the cumulative total
    totalMilliLitres += flowMilliLitres;

    unsigned int frac;

    // Print the flow rate for this second in litres / minute
    Serial.print("Flow rate: ");
    Serial.print(int(flowRate));  // Print the integer part of the variable
    Serial.print(".");             // Print the decimal point
    // Determine the fractional part. The 10 multiplier gives us 1 decimal place.
    frac = (flowRate - int(flowRate)) * 10;
    Serial.print(frac, DEC) ;      // Print the fractional part of the variable
    Serial.print("L/min");
    // Print the number of litres flowed in this second
    Serial.print("  Current Liquid Flowing: ");             // Output separator
    Serial.print(flowMilliLitres);
    Serial.print("mL/Sec");

    // Print the cumulative total of litres flowed since starting
    Serial.print("  Output Liquid Quantity: ");             // Output separator
    Serial.print(totalMilliLitres);
    Serial.println("mL");

    // Reset the pulse counter so we can start incrementing again
    pulseCount = 0;

    // Enable the interrupt again now that we've finished sending output
    // attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
    attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(sensorPin), pulseCounter, FALLING);
  }
}

/*
  Interrupt Service Routine
*/
void pulseCounter()
{
  // Increment the pulse counter
  pulseCount++;
}

For code advice, please post your code, using the code tags </> found at the top left of the tool bar.
You will see the code tags and you place your code between them

[code]your code here[/code]

It will appear in box like this

your code here

Hi,

With your type of project, personaly, I would investe in an Arduino Mega or Due. They have many available interrupt pins with powerful interrupt capabilities that allows you to attach an interrupt function on all available pins.

The extra money can probably simplify your task.

The new code posted, it works like a charm. Thanks! I went thru every pin from #4 to #13 and it was perfect. Zero flow when sensor was idle, and good numbers when flow was pushing thru the pipe. I'll have to carefully examine the minute differences between old program and the new code so I can understand what I was missing and doing wrong in software.

I'm sticking with the fantastic #include PinChangeInterrupt headers in Uno until I use up all the dozen Uno digital I/O slots. Mr. NicoHood deserves an award for that software fix.

Due and Mega have 54 digital I/O pins, with Uno going for half their prices. Yes, I could conceivably run out of digital I/O pins, which would for me in this case correspond to an irrigation system with multiple controllers on a large property. Actually, that's real world – my previous employment had +60 satellite controllers running on a master PC server, distributed with over 1000 electric valves scattered about the grounds of a fairly large university campus. Mega bucks to put in all the hardware(pulse decoders, pulse transmitters, flow sensors, cluster control unit$$$, ect.). And the software was charged out separately as a commercial deal. Yet for some reason, no one at that time had a clue how to get the system to bring in any flow data! With the hardware not being set up right, a million gallons/month of reclaimed water could have been lost, and probably was, and no one would have known the difference. Except for the ones who were paying the water bill each month! Theoretically, if someone had covertly tapped into one of the 6in. mainlines, it would be the same as getting free irrigation water and never worry about paying.

But I digress here. I'll just call this one a game-changer!