Flow sensor - rising edge detection using interrupt not working

@OP

You can place the images on line by following these tips:

1. After attaching the image file, save the post.

2. Right Click on the attached file name and copy the link address.

3. Open the post in modify mode.

4. Place the cursor where you want to place the image.

5. Click on the Insert an image icon of the Tools bar. A window will appear and place the cursor in the input box. Press button cntrl+V on the keyboard. The link will appear in the input box. Click on OK button of the window. Save the post. Check that the image has appeared online.

But when the image is an image of text, then don't do it like that. Instead, copy and paste the text between code tags. That's much easier to deal with. Pictures of text are just dumb.

Hi all,

Firstly thanks for taking the time to help with this issue and apologies for not giving all the information.

What I want to do is measure the volume of water coming from a water dispenser using a flow sensor (Gems flow sensor 173940-C) and an arduino (TTN UNO based on the Arduino Leonardo)

The flow sensor outputs pulses and according to the data sheet 1 pulse = 1 ml of water so from my understanding no calibration factor is needed

The issue I'm having is that when I try to count the amount of pulses using an ISR I get a value thats approx double what I'm expecting (for a 200ml cup I'm getting around 400 pulses)

When I constantly poll the the pin and manually count the pulses I get a value of approx 200 pulses (I say approx because I dont always fill the cup to exactly 200ml)

What I wanted to know is why the ISR is miss-firing/detecting additional pulses.

I havent done PulseCount = 0 purely because it makes reading the total amount of water easier ( seeing that I've just filled 200ml rather than seeing and having to add 54ml ,32 ,76 and 38ml etc) at a later stage I will implement PulseCount = 0 after storing the value elsewhere but for now I just want to get the correct value using ISR if possible

I would like to get the ISR method working as from my understanding, constantly polling the input is not an efficient method and I also would like to have other processes occurring at a later stage in the project

@OP

When I am polling the output of my ~54 Hz oscillator, I am getting exactly the same value that I was getting in the interrupt driven process.
scrshot-4.png

The Codes (Polling Process)

//#define debugSerial Serial

const int flowPin = 2;    //This is the input pin on the Arduino
volatile unsigned int PulseCount = 0; //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.
unsigned long currentTime ;
unsigned long StartTime = 0;
bool flag1 = LOW;

void setup()
{

  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(flowPin, INPUT_PULLUP);           //Sets the pin as an input
  Serial.begin(9600);
  // attachInterrupt(digitalPinToInterrupt(2), Flow, FALLING);  //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function "Flow"
}

void loop()
{
  if (flag1 != HIGH)
  {
    if (digitalRead(2) == LOW)
    {
      PulseCount++;
      flag1 = HIGH;
    }
  }

  if (digitalRead(2) == HIGH)
  {
    flag1 = LOW;
  }

  if (millis() - StartTime == 1000)
  {
    Serial.print ("Pulse Count: ");
    Serial.println (PulseCount);
    StartTime = millis();
    PulseCount = 0;
  }

}

scrshot-4.png

GolamMostafa:
Figure-2: Output after correcting the mistakes of the program

Did you post the "corrected" code - interrupt version? I don't see it.

  1. Are you providing power to the flow meter from the Arduino ?
  2. Have you checked your wiring (specifically earthing)

I had a very similar issue a while back where I seemed to be getting double pulses from a device which should not bounce (an ESP8266 sending a pulse)
My issue was that I was powering the ESP8266 from an Arduino, and I was powering the Arduino with a battery.
Once I gave each device its own reliable power supply, the issue went away.

gfvalvo:
Did you post the "corrected" code - interrupt version? I don't see it.

The Interrupt Codes Tested using a ~54 Hz (555 based) oscillator + UNO

//#define debugSerial Serial

const int flowPin = 2;    //This is the input pin on the Arduino
volatile unsigned int PulseCount = 0; //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.
unsigned long currentTime ;
unsigned long StartTime = 0;


void setup() 
{

  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(flowPin, INPUT_PULLUP);           //Sets the pin as an input + external 2.2K pull-up
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(2), Flow, FALLING);  //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function "Flow"
  interrupts();
}

void loop() 
{

 // currentTime = millis();
  if(millis() - StartTime !=1000)
  {
    ; 
  }
  //(currentTime >= (StartTime + 1000)) //occurs every 1 seconds - take a reading
  else
  {
    Serial.print ("Pulse Count: ");
    Serial.println (PulseCount);
    StartTime = millis();
    PulseCount = 0;
  }
}

void Flow() 
{
  PulseCount++; //Every time this function is called, increment "PulseCount" by 1
}

@GolamMostafa:

As I pointed out in Reply #5, you should protect non-atomic accesses to PulseCount using a critical section in loop().

Also, there's a good chance that this will fail you at some point:

if(millis() - StartTime !=1000)

One missed tick and you're screwed.

Can't understand why you're using such convoluted logic anyway. Much more straight forward is:

if(millis() - StartTime >=1000)
{
    StartTime += 1000;
    // Do the printing stuff here
}

@whizz5, do not cross-post. Threads merged.

darrob:

  1. Are you providing power to the flow meter from the Arduino ?
  2. Have you checked your wiring (specifically earthing)

I had a very similar issue a while back where I seemed to be getting double pulses from a device which should not bounce (an ESP8266 sending a pulse)
My issue was that I was powering the ESP8266 from an Arduino, and I was powering the Arduino with a battery.
Once I gave each device its own reliable power supply, the issue went away.

I am indeed powering the flowmeter (Gems flow sensor 173940-C) directly from the Arduino's 5v pin and the Arduino is being powered via its USB port

And if by earthing you're referring to grounding, I've simply connected the flowmeter's GND wire to the Arduino's GND pin