# Timing 2 pulses in nano seconds?

I am in the middle of a project for school and need to measure the time difference in between 2 pulses that are being fed in via interrupt pins. The 2 pulses are slightly out of phase and coming of a Hall effect or optical sensor that is taking reading from 2 rotating shafts. The shafts are spinning at 16Hz, but part of the requirements for this project is a calibration uncertainty of less than 1% at a 95% confidence interval. Long story short I need to be able to measure a time difference of 1.8micro (or 1/540000) seconds. I wrote the code using the micros() function to try and get something to work and according to the o-scope I was using to verify the phase shift of the signals it was doing ok at time differences of about 100micro seconds. I was using an Arduino Uno, but we have Teensy 4.0 that has a 600Mb clock that will probably be used in the final project. I'm still very new to this stuff (I'm mechanical Engineering major) and supposedly the Uno is allot more user friendly than the Teensy so I was going to try and get it working with the Uno first. I wasn't sure if there was a library I could download that would have a function similar to micros() but would measure nano seconds?

The shortest interval measurable on a Uno would be 62.5ns.

What makes you think this is an installation and troubleshooting issue?

(sp " a lot" - "allot" is a verb, meaning to proportion or divide up )

The last time I used a Teensy, the only thing that made it any more difficult was the need to install additional software to handle pushing code to the device, but that's one and done.

That clock speed should make your task significantly easier - I'd leave the Uno behind.

There isn't to my knowledge - you'd use the signals to gate a hardware timer for this sort of timescale, avoiding software altogether. Interrupt latencies can be large compared to a microsecond (other interrupts are usually flying around for one thing).

I'd suggest XORing the signals with each other using a logic gate and feeding to a timer input pin and then monitoring the timer for changes (this can be polled in software easily). IIRC there's a timer input pin on the Uno for timer1.

[ Though I'm not sure if it can be used as a gate - you'd need the right timer mode so reading up on timers or timer libraries is an idea ]

FYI... On an UNO, micros() is always a multiple of 4 which is even worse

Such a function is impossible because there is latency in calling functions. But you can use a hardware feature built into most MCU's called "input capture", which latches the exact time of some input transition into a register, where it can be read at slower speed. I'm not familiar with the Teensy processor, but as it is fairly advanced, I suspect it would have the two input capture registers that you would need to compare the phase.

Thanks for the input! I downloaded the CaptureTimer library and modified my script. It compiles but I wont have access to the function generators and oscilloscope until after thanksgiving break so I wont be able to try it out till then. If I'm understanding the code correctly the command CaptureTimer::getTickCapture(time_1) will write the amount of "ticks" to the variable time_1 that have past since the command CaptureTimer::initCapTime(signal_1_pin); was used. My next question is what is a "tick"?

I'm not familiar with the library, but in this context, a "tick" probably refers to one count of the capture timer.

I think you may have misunderstood the operation of the function, though. You should refer to the library documentation for that. The time that has passed since a capture can not be measured accurately in software alone, because of the overhead involved in making the function call and polling the timer. To use capture to time a very short interval you need to use two capture timers (usually the capture pins are tied together because the interval is being measured, but you won't because you're measuring relative phase).

Again, I don't know the library, but it's possible that they reset the timer and re-use it to measure the interval, but I'm not sure. The AVR 328 only has one capture timer input as I believe I mentioned before. So any library you use has to support the multiple timers in other libraries, because of your special requirement of very short intervals.
For what you need, you must have two capture timers and compare the values that you capture in each one.

If you don't wish to wait there are some Arduino "simulators" on-line. I've never used one but from the feedback I've see in a number of posts the work pretty well.

That library does not use the capture input. It just uses interrupts on pin change. It won't meet your needs.

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