pulsein vs counting pulses using interrupts

Hi!
I'm building a solution for measuring fuel consumption in my boat. It's based on an arduino (duemilianove) and a flow sensor.

The sensor generates 10 000 pulses per liter that passes, and given the fuel consumption of my boat I expect to get between 2,5 and 340 pulses per second.

I'm struggling with deciding on how to measure the flow through the flow sensor, if I should use a.) pulsein to measure the time between pulses or if I should b.) use an interrupt to count pulses (per second).

The drawback of using interrupts to count pulses (and once a second see how many pulses has occurred) is that it will have a low resolution at low flows, since there are so few pulses per second then.

What are your thought on this? Should I use pulsein och does that have som other drawbacks?

I'm planning on sending the readings to my phone via Bluetooth where an app inventor 2 app will plot a graph showing fuel consumption (liters per hour & liters per nautical mile)

annerborn:
The drawback of using interrupts to count pulses (and once a second see how many pulses has occurred) is that it will have a low resolution at low flows, since there are so few pulses per second then.

The even bigger drawback of pulseIn is it only measures pulse durations and not pulse counts and is blocking so no other commands can run while pulseIn is working so you will loose pulses and lock up your code so it spends nearly all it's time measuring pulses.
Interrupts are less likely to miss pulses and you can do a lot of other stuff at the same time.

If the sensor is accurate and linear then one pulse is still 0.1ml, no matter how seldom it occurs. However, it's performance at low rates will depend strongly on the design.

Try passing 1 liter of fluid through the sensor slowly and see if you get 10K counts.

Also, if you measure the time between pulses as an inverse of the rate then you only have the duration of a pulse to process the data. You could easily miss the start of the next interval.

annerborn:
The sensor generates 10 000 pulses per liter that passes, and given the fuel consumption of my boat I expect to get between 2,5 and 340 pulses per second.


The drawback of using interrupts to count pulses (and once a second see how many pulses has occurred) is that it will have a low resolution at low flows, since there are so few pulses per second then.

For your input rate of 2.5 to 340 Hz, there's no need to use a 1 second timing interval for measurement. If you use pulse edge to edge timing measurements (RISING to RISING or FALLING to FALLING) with micros(), then the resolution will be ±4µs.

340Hz = 2941µs period with ±4µs measurement error (±0.136%).
2.5Hz = 400000µs period with ±4µs measurement error (±0.001%).

This error is non-cumulative, so you can average some readings to get lower measurement errors if needed, but I suspect this is already much better than the accuracy of the fuel sensor.

Your measurement time will be quite fast ... 0.4s to 2.9ms for 2.5Hz to 340Hz.

You may want to set a measurement calculation and reporting (print) interval of say 0.5s or 1s for consistent display updates.

For fuel consumption, all you need is a simple counter that increments at each interrupt.

Thanks for your replies!

Just to verify what you are suggesting (I'm quite new to arduino programming).

You are saying is that I should create an interrupt procedure that trigger on the event RISING on the pin where I have connected the flow sensor.

In this procedure I shall check how much time has lapsed since the last event, using the the micros() method. That will give me the time between pulses (ni microseconds).

I can then convert time between pulses to current consumption (in liters/hour) by dividing a constant (360 000) by the number of micros between pulses.

Does that seem right?

Close ... you'll get flow rate (in liters/hour) by using the time interval that you measure in microseconds. So it's just:

Flowrate (litres/hour) = Pulse Period (µs) x 1,000,000 / 360
Consumption (liters) = Pulse Count / 10,000 pulses/litre

annerborn:
Hi!

The drawback of using interrupts to count pulses (and once a second see how many pulses has occurred) is that it will have a low resolution at low flows, since there are so few pulses per second then.

Adopt a hybrid strategy of measuring the period at low flows, and measuring the
frequency at higher flows.

Or take the raw frequency reading once a second and brick-wall low-pass filter it with a digital filter with a 1Hz
cutoff.