I'm trying to measure the duration of 15 pulses which can be in the range of 350uS and 60uS. I then need to average this out every 15 pulses.
I tried pulseIn however the pulse duration varies eg for a 50/50 square wave at 5kHz I get a range of 87uS to 92uS.
Is this a limitation of the pulseIn function?
I then tried using interrupts this gives a more erratic output.
I also tried the FreqCounter.h library, this gives a very stable output but this uses a window/gate and to get the resolution I need the window must be 100mS.
In the actual application the pulses will come from a Hall Effect sensor fitted to a vehicle engine, however at the moment I am using a bench signal generator which has a constant frequency output.
The signal will generally be a 50/50 square wave (1.4kHx to 8kHz), it is the fluctuation that the engine produces from its firing cycle that I am trying to filter out. The engine fires every 15 pulses, once every 1.8mS to 11ms.
If you're planning to read the signal from a magnetic sensor then the pulse train will not be anything like 50% duty cycle. To use pulseIn() to measure the pulse interval you would need to ensure that you got the polarity right (since that it is measuring the length of the gap, not the pulse) and also note that this would probably miss every other pulse since it needs to see the start of the pulse as well as the end.
To measure the length of a pulse you simply need to detect the start and end of the pulse and measure the time between them. If the duration is short so that the granularity of your time measurements becomes a problem then you can measure the duration of multiple pulses to get better resolution. Since the duration of the pulse from a Hall effect sensor can be quite short, this is a case where I'd consider using interrupts to make sure the pulse times were captured with minimal latency and to ensure that no brief signals got missed. You will need to arrange to get a logic level signal from the sensor, which usually means you'll need a conditioning circuit between the Arduino and the sensor.
If you want an average pulse length for every fifteen pulses then you could put the logic to do that in the interrupt handler - just count the number of received pulses; when it reaches 15 subtract the start time from the current time and divide by fifteen to get the average pulse length, then save the current time for the next measurement. The variable containing the resulting pulse length would need to be declared as volatile so that it can be read by the main context code, and if the variable is bigger than a single byte then you will need to have your main context code suspend interrupts while it copies the volatile data to a local variable, to ensure the volatile data doesn't change while it is reading it.
Thanks for you input. I am just trying to get the pulse measurement correct at the moment, I am using a square wave signal generator 50% duration at 4kHz which I can measure on a calibrated oscilloscope. The Hall effect sensor I will be using has signal conditioning built-in to give a TTL output.
I tried using interrupts earlier however I got even more spurious pulse durations using this method. I didn't have the 15 pulse count in there, I just started and stopped a timer and read the value. I'll give your method a try.
I wonder if Serial.println(raw_ip_pulse_dur) is slowing things down.
I would try reading (say) 100 pulse widths into an array and when that is done I would print the values from the array - just to see if it makes a difference.