Measuring time between pulses in microseconds

I am trying to measure the time between pulses and RPM from 2 shaft encoders using an Arduino Uno. Both encoders provide 8 pulses per revolution and the shaft rotates between 150-300RPM

Sensor B can lag behind Sensor A by duration T which can range anywhere from 4 microseconds to 550 micro seconds.

I would like to average results (RPM and T) over 40 pulses or 5 shaft revolutions and also determine the RPM from this by counting either pulses over a time period.

What would a sketch that could do that look like?

Thank you

You need a processor with two input capture registers.

What harware would you recommend?

IDK. The Uno only has one, though.

What would a script that could do that look like?

There are no "scripts" in Arduino, only sketches. It would look like a lot of low level processor register configuration, along with interrupt service routines and mainline code to interface with those.

The drawing suggests that you want to measure the time difference between two falling edges, on different pins.

I think that could be done with direct port read (one instruction, 62.5 ns) plus some code to detect the individual edges, which would start and stop Timer1 clocked at 16 MHz.

May be you could set pins interruptions and count the microseconds.

How?

Look for reference micros() . Also look for attachInterrupt()

Or XOR the inputs in hardware and drive the capture register input with that result? Although, this would generate a phase difference signal twice on every cycle.
It should be possible too to connect the inputs to the analog comparator circuit via AIN0 and AIN1 and feed that result to the capture register - eliminating the need for an external XOR.

.02

1 Like

I've been there before. See there, the part about only 4us resolution... also that invokes code execution delays.

The ask was for minimum 4 us measurement interval. It simply can not be done with polling and micros() for those two reasons.

If you involve interrupts, it will get even slower, unless the interrupt is a response to an input capture. Look at the overhead in an interrupt call/return.

This is one of these threads where the application was not outlined. The first time I read the first post, I immediately thought this might be some kind of PLL, was going to ask if a hardware PLL circuit might be a complete solution.

If nothing else, an XOR would give you a phase comparison that you could LP filter, and maybe don't need a software solution.

But I never asked about the application, because I get so tired of asking things like that...

the actual function call micros() can take 4 to 5 uS to get the current uS. So if an accuracy of 4 uS is required use an ESP32 cycle counter which can give micros with a 12.5nS resolution or picos or nanos.

1 Like

Then lets ask:

@Riseley - can you tell us a bit more about the application please

(my guess - are you finding the power transmitted by a shaft by detecting the shear stress?)

Hi John, Yes, I am trying to measure twiest -torque-power on a shaft

I'm an ESP, BeagleBone, RPi user.

If you want to count pulses with a 12.5nS resolution the ESP32 or the BeagleBone Blacks PRU is the way to go.

Remaining on things to be done with the Arduino IDE, that leaves the ESP32.

The ESP32 has a very good pulse counter that when set does not require any cpu intervention to operate once its programed.

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/pcnt.html

Could use both interrupt pins on an Uno to do this.
Here's an example that uses a 10 kHz clock to create a 100 μs delayed signal on int2.
image

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