Timing Precision

Hello Friends,

I was wondering what is the most precise way to put an output pin on HIGH (activates another circuit) for a certain time interval and then back to LOW. What is precision of such timer ? Does using FreeRTOS help in any way ? Please not that the only task I need from the Arduino is to just turn on and turn off an output pin, and that's all (but I need high precision!).

Thank you!

The precision depends on the board/timer you are actually using.

What precision are you looking for ?

You can reach this precision with any arduino board/timer :slight_smile:

Great ! do you know the max precision that can be achieved ?

Which board are you selecting ?

ard_newbie:
Which board are you selecting ?

any board

In other words can somebody list 2-3 boards and the relevant precision or the OP (any OP) has to ask one Q for each board?

any board really that will maximize the timing precision

joseph2847:
any board really that will maximize the timing precision

Maximize on its own is a meaningless word.

What is the biggest timing error that would be acceptable - measured in microseconds?

...R

100ns

With an Arduino Uno, you could get a granularity of 62.5 ns in setting the amount of time the output stays high. Using a Teensy 3.2, it would be 20.8 ns. The accuracy would depend on the accuracy of the underlying clock source.

Nobody can tell if either of these would work for your application since you've failed to provide a complete Requirement Specification or Problem Statement.

gfvalvo:
With an Arduino Uno, you could get a granularity of 62.5 ns in setting the amount of time the output stays high. Using a Teensy 3.2, it would be 20.8 ns. The accuracy would depend on the accuracy of the underlying clock source.

Nobody can tell if either of these would work for your application since you’ve failed to provide a complete Requirement Specification or Problem Statement.

Iam not the OP. I just found OP’s Q and just look for an answer.
So, from what you say, if use uno :

...
void Setup {
 pinMode(4, OUTPUT);

 while (1>0) {
 digital.Write(4, LOW);
 delayMicroseconds(50);
 digital.Write(4, HIGH);
 delayMicroseconds(50);
 }
}

So, if we assume a “calibrated” clock frequency EXACTLY 16 MHz, at pin 4 the user gets a squarewave with period 100 ± (2*62.5nsec) ?

@demkat1:
All I said in my post was that you could get the stated granularity. Doing so would require the use of hardware timers (see the appropriate chip datasheet). Your code would suffer additional delays due to loop overhead and possibly active interrupts.

With an ESP8286 one can get 6.25nSec resolution using the ESP.getCycleCount() macro. You would want to use a unsigned 32 bit variable that will, unless coded for, overrun in about 28 seconds. I've used a ESP32, freeRTOS, vTaskDelayUntil(), and an interrupt generated signal with the Cycle count timer. I used the freeRTOS macro xTaskCreatePinnedToCore (), to assign the Cycle count routines to a core. I have, also, coded the ULP core, using assembly, to cause the Cycle count timer to generate interrupts (through the ULP core), using a ESP32. By using the ULP core, the two main cores are free to run functions.

The timer Cycle count will give 160 counts per uSecond. An important number to know when using the Cycle count timer

The Arduino Due has the MCLK/2 clock which would give a resolution of 23.81nSec. I'd run freeRTOS or uMT on the Due to handle the code and, I'd not run 'anything' else on the Due for increased timer resolution.

Idahowalker:
With an ESP8286 one can get 6.25nSec resolution using the ESP.getCycleCount() macro.

The ARM chips also have cycle count capability. Trouble with using that type of counter is that you need to read it programmatically then act programmatically to set the output as required. That introduces unknown latency and hence degrades accuracy. Using hardware Counter / Timers (typically tasked to generate PWM), you can let the hardware handle those tasks. This removes the code-induced latency.

I wouldn't bother with an OS or macros that someone else wrote. For best results, read the appropriate chip datasheet and access the processor's registers directly.

Conclusively one can say that at OP;s question
post#1
I was wondering what is the most precise way to put an output pin on HIGH (activates another circuit) for a certain time interval and then back to LOW. What is precision of such timer ? Does using FreeRTOS help in any way ? Please not that the only task I need from the Arduino is to just turn on and turn off an output pin, and that's all (but I need high precision!).

the "define the board you are going to use" is NOT an answer.

Correct?

demkat1:
100ns

This is just causing confusion IMHO.

We need to hear from the OP what project the OP is trying to implement and what level of precision he needs. It is pointless speculating.

And it probably does not help, either, to put irrelevant ideas into the OP's mind causing him to focus on a level of precision that may be completely unnecessary for his project.

...R

Robin2:
We need to hear from the OP what project the OP is trying to implement and what level of precision he needs. It is pointless speculating.

Like so many requests on this forum, the problem is incompletely / poorly specified. I’d want to know at least:

  • Required Accuracy
  • Required Precision
  • Minimum / Maximum Required Pulse Interval
  • Logic Level (5V? 3.3V?)
  • How is the pulse event triggered?

Someone may not be an engineer by profession. But, to get anywhere in this hobby, you need to think like one.

The Difference Between Accuracy and Precision:

So,
did anyone asked for theese specifications from the beginning?
NO.

@ robin2
my answer "100ns" is on your question on post#7. If now, on post 15 you think "IYHO" ...whatever, then you should place your inquiry from the very beginning.

Sirs, I dont like arguing. Just trying to show that the answers to OP's Q were not as should, well IMHO.

thank you
over and out

demkat1:
So,
did anyone asked for theese specifications from the beginning?
NO.

Not our job. We're not the ones asking for (free) help.

The better you specify the problem, the better your chance of getting a usable answer.