Fuel Flowmeter - slow pulse counter periodically miscounting pulses

I am new to Arduino programming and this is my first post. Many thanks for any advice!

My project is to count pulses from an old Flowtronics fuel consumption tester. This instrument comprises of a flowmeter inserted into the petrol feed between a car engine fuel pump and the carburettor. This was connected to a modified Canola calculator which sums pulses and calculates the amount of fuel used. This was then used to calculate the mpg. Unfortunately the calculator no longer works hence my attempt to reproduce the electronics using the Arduino Uno.

Each pulse from the sensor equates to 0.668 ccs of fuel, the pulses being derived from a reed switch. 1497 pulses equates to a litre of fuel.I have already made a h/w debounce circuit for this switch.
Pulses will range from 0Hz (or very close when stationary) to 8Hz.

I am now in the process of testing a sketch using a function generator. I am counting the total number of pulses and over 1 sec to enable me to calculate the fuel used over a period of time (this part not included in the sketch at the moment).

I would like to understand why I am getting periodic pulse miscounts (see below). These do not occur at the same place if my test is repeated.

How can I improve the pulse count accuracy of the sketch? It is the best way to count slow pulses?

The sketch counts slow pulses via an interrupt on pin 2 over a period of 1 second. For testing purposes I using a RIGOL DG1022U Function generator to supply a square wave at 50% duty cycle 5V and at 10.000000 Hz

Uno compatible

Pulses Connected to pin 2

Test results below the sketch

const byte interruptPin = 2;  

volatile unsigned long count = 0;
unsigned long pulseCount = 0;
unsigned long intervalPulseCount = 0;
unsigned int lastPulseCount = 0;
unsigned long oldTime;

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

 pinMode(interruptPin, INPUT_PULLUP);

 attachInterrupt(digitalPinToInterrupt(interruptPin), pulseCounter, RISING);
}

 void loop() {

 if ((millis() - oldTime) >= 1000) {  // Process counter once per second
   oldTime = millis();

   noInterrupts();
   pulseCount = count;
   interrupts();

   intervalPulseCount = pulseCount - lastPulseCount;
   lastPulseCount = pulseCount;

   Serial.print("Pulse Counts in the interval (1 sec) = ");
   Serial.print(intervalPulseCount);

   Serial.print("    Total pulse counts = ");
   Serial.println(pulseCount);
 }
}

void pulseCounter() {
 count++;
}

All the test results below are from the same test. If I run the test again the miscounts do not appear at the same point.

First pulse miscount starts 2250 pulses then miscounts 10 pulses to 11,9,11,9,11, 9,11 then continues at 10 pulses per interval.

Second pulse miscount starts at 15041 then miscounts 10 pulses to 11,9,11,9,11,9,11 then back to 10 pulses.

Third pulse miscount starts at 27842 then miscounts 10 pulses to 11 then back to 10 pulses.

1st miscount starts at 2250

15:19:45.271 -> Pulse Counts in the interval (1 sec) = 10 Total pulse counts = 2240
15:19:46.261 -> Pulse Counts in the interval (1 sec) = 10 Total pulse counts = 2250
15:19:47.278 -> Pulse Counts in the interval (1 sec) = 11 Total pulse counts = 2261
15:19:48.277 -> Pulse Counts in the interval (1 sec) = 9 Total pulse counts = 2270
15:19:49.278 -> Pulse Counts in the interval (1 sec) = 11 Total pulse counts = 2281
15:19:50.278 -> Pulse Counts in the interval (1 sec) = 9 Total pulse counts = 2290
15:19:51.279 -> Pulse Counts in the interval (1 sec) = 11 Total pulse counts = 2301
15:19:52.263 -> Pulse Counts in the interval (1 sec) = 9 Total pulse counts = 2310
15:19:53.246 -> Pulse Counts in the interval (1 sec) = 11 Total pulse counts = 2321
15:19:54.279 -> Pulse Counts in the interval (1 sec) = 10 Total pulse counts = 2331
15:19:55.278 -> Pulse Counts in the interval (1 sec) = 10 Total pulse counts = 2341

2nd miscount starts at 15041
15:41:05.340 -> Pulse Counts in the interval (1 sec) = 10 Total pulse counts = 15041
15:41:06.357 -> Pulse Counts in the interval (1 sec) = 10 Total pulse counts = 15051
15:41:07.342 -> Pulse Counts in the interval (1 sec) = 11 Total pulse counts = 15062
15:41:08.358 -> Pulse Counts in the interval (1 sec) = 9 Total pulse counts = 15071
15:41:09.358 -> Pulse Counts in the interval (1 sec) = 11 Total pulse counts = 15082
15:41:10.358 -> Pulse Counts in the interval (1 sec) = 9 Total pulse counts = 15091
15:41:11.357 -> Pulse Counts in the interval (1 sec) = 11 Total pulse counts = 15102
15:41:12.341 -> Pulse Counts in the interval (1 sec) = 9 Total pulse counts = 15111
15:41:13.342 -> Pulse Counts in the interval (1 sec) = 11 Total pulse counts = 15122
15:41:14.342 -> Pulse Counts in the interval (1 sec) = 10 Total pulse counts = 15132
15:41:15.357 -> Pulse Counts in the interval (1 sec) = 10 Total pulse counts = 15142

3rd miscount at 27842
16:02:24.438 -> Pulse Counts in the interval (1 sec) = 10 Total pulse counts = 27832
16:02:25.421 -> Pulse Counts in the interval (1 sec) = 10 Total pulse counts = 27842
16:02:26.426 -> Pulse Counts in the interval (1 sec) = 11 Total pulse counts = 27853`
16:02:27.428 -> Pulse Counts in the interval (1 sec) = 10 Total pulse counts = 27863
16:02:28.410 -> Pulse Counts in the interval (1 sec) = 10 Total pulse counts = 27873

No drawing shown so the question must be asked - are sig gen ground and Arduino ground connected together?

The signal ground from the function generator is connected to the Uno ground.
Point taken re a drawing though.

Looks OK to me. The interrupts and millis() are not synchronized so a +/- one count error is not unusual.

Notice that the 11 and 9 are on consecutive reading so the overall error over time is 1 count. If you go for 2 seconds the error should still only be one count 19 and 21 not 18 to 22.

EDIT - Notice in you first two sets of numbers there is only a 1 count total error over 11 seconds.

In each interval such an additional count occurs. It may be related to an Arduino clock frequency slightly below the assumed frequency (16MHz?).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.