Go Down

Topic: Using two flow sensors connected to one Arduino. (Read 5846 times) previous topic - next topic

robtillaart

Quote
Liter per time: 24.8400
...and that is not what I had expected.
The engines I develop this flow meter to, uses between 3 and 50 liters per hour.

24 is  between 3 and 50, so ?? Be aware that this is the consumption per hour!

The device gives 10.000 pulses per liter according to spec. So the formula seems OK
pulseCount = (pulseCountIN - pulseCountOUT) / 10000.0 * 3600;

better split the two (and give the vars good names) to debug them seperately.

float LPH_IN = pulseCountIN/ 10000.0 * 3600;   // pulses to Liter/hour         
float LPH_OUT = pulseCountOUT/ 10000.0 * 3600;

The fact that you get ~24 Liter means that the incounter get far more pulses than the outcounter. This makes sense as the device consumes fuel.

About Accuracy
The minimum consumption it can detect is one pulse. If you measure a delta of 1 pulse per second it equals a difference of 1/10000*3600 = 0.36L

Look at your measurements

Liter per time: 23.4000
Liter per time: 24.1200
Liter per time: 24.8400
Liter per time: 25.2000
Liter per time: 25.5600
Liter per time: 24.8400

and you see the delta's between the measurements are 0.36 or 0.72. That is one or two pulses difference.

The best way to have a better accuracy is to measure the fuel-consumption over the last minute * 60. A delta of 1 pulse will result in a delta of 0.006 L
The best way to do this is to make measurements per second and use them to fill a circular buffer of 60 elements.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart


Delta meter with a circular buffer.

Code: [Select]

#define IRQ_A 0
#define IRQ_B 1
#define FlowA 2
#define FlowB 3

volatile unsigned int countIN = 0;
volatile unsigned int countOUT = 0;
unsigned long oldTime  = 0;

unsigned long totalPulses = 0;

unsigned int circBuf[60];
uint8_t idx = 0;

void setup()
{
  Serial.begin(57600);

  pinMode(FlowA, INPUT);
  pinMode(FlowB, INPUT);
  digitalWrite(FlowA, HIGH);
  digitalWrite(FlowB, HIGH);

  attachInterrupt(IRQ_A, CounterIN, FALLING);
  attachInterrupt(IRQ_B, CounterOUT, FALLING);
}

void loop()
{
  unsigned long now = millis();
  if(now - oldTime >= 1000)
  {
    unsigned long duration = now - oldTime;
    oldTime = now;

    // store usage of last second in circular buffer
    idx++;
    if (idx == 60) idx = 0;
    // disable interrupts while reading & reset counters.
    cli();
    circBuf[idx] = countIN - countOUT;
    countIN = 0;
    countOUT = 0;
    sei();

    totalPulses += circBuf[idx];

    long pulsesLastMinute = 0;
    for (uint8_t i=0; i<60; i++)
    {
      pulsesLastMinute += circBuf[i];
    }
    // LPH based upon last minute
    float LPH_M = pulsesLastMinute/10000.0 * 60;
    // LPH based upon last second
    float LPH_S = circBuf[idx]/10000.0 * 3600;
    // Total Liters since start
    float liters = totalPulses/10000.0;

    // DISPLAY USAGE
    Serial.print(now);
    Serial.print("\tLPH_M:\t"); 
    Serial.print(LPH_M, 3);
    Serial.print("\tLPH_S:\t");
    Serial.print(LPH_S, 3);
    Serial.print("\tLiters:\t");
    Serial.println(liters, 3);
  }
}

void CounterIN()
{
  countIN++;
}

void CounterOUT()
{
  countOUT++;
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

KapteinFredrik

Thanks for very good help robtillaart!  8) 8) 8)

I Tried this sketch with my sensors, but one of them shows something like 100 times higher value than the other(about 700 liters per secund). It does not seems that the subtraction function works either as it did before.

Regards, Fredrik.
Best regards, Fredrik

robtillaart

What happens if you switch the lines ?
just to test if this happens in the sensor/wiring or in the software?


100 times higher could mean more noise on the line, which is seen as pulses for the interrupt handler. (Or leakage?)
Is the factor a constant or irregular?. If it is a constant (exact 100x) you might just have 2 different devices !
If it is irregular it looks more like noise.


You can add this modification to the sketch to remove the noise. It ignores pulses that are too short after each other.
Be aware that micros() returns multiples of 4. So testing with 8,9,10,11 give the same behaviour.
Code: [Select]

void CounterIN()
{
  static unsigned long lastTime = 0;
  if (micros()  - lastTime > 10) // to be adjusted
  {
    lastTime = micros();
    countIN++;
  }
}

void CounterOUT()
{
  static unsigned long lastTime = 0;
  if (micros()  - lastTime > 10)
  {
    lastTime = micros();
    countOUT++;
  }
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up