2
\$\begingroup\$

I read on arduino.cc/en/reference/micros that I can know the number of microseconds since the Arduino began running the program.

On my 16 MHz Arduino Uno board, the function has a resolution of four microseconds and less with a 8 MHz board. I found the Arduino Due and it has a 84 MHz clock speed.

Could I have a better resolution than 4 μs? I would like to have a precision with nano seconds.

\$\endgroup\$
  • \$\begingroup\$ 4 µs is 4,000 ns. Be more specific with your required precision please. \$\endgroup\$ – W5VO Dec 17 '13 at 16:25
  • \$\begingroup\$ Can you clarify why you mentioned the Arduino Due? Is your question regarding the Arduino Uno or the Due? \$\endgroup\$ – Joe Hass Dec 17 '13 at 17:28
  • \$\begingroup\$ I would like to say "not with using millis()", but I don't know enough about the Arduino libraries for the Due to be able to give that answer. \$\endgroup\$ – Ignacio Vazquez-Abrams Dec 17 '13 at 18:21
  • \$\begingroup\$ And of course I meant micros(). That'll teach me to type/read too fast. Or not. \$\endgroup\$ – Ignacio Vazquez-Abrams Dec 17 '13 at 19:04
  • \$\begingroup\$ You can read a regular timer directly once it is set up. Timer2 is often unused. \$\endgroup\$ – jippie Dec 17 '13 at 19:36
4
\$\begingroup\$

The smallest time interval you can measure using the Arduino Due's built-in hardware is roughly 23.8 nanoseconds.

The smallest time interval you can measure using the Arduino Uno's built-in hardware is roughly 62.5 nanoseconds.

Arduino Due timers

The Arduino Due uses an Atmel SAM3X8E microprocessor. It has 3 identical Timer-Counter peripherals, each of which has 3 independent timer channels. On the Due, these are the best way to measure small amounts of time if you want resolution in nanoseconds. Each Timer-Counter can use different clock sources as input - these determine the increments of time the Timer-Counter can measure. The fastest clock source is MCK/2, the master (system) clock divided by 2.

On the Arduino Due, MCK is 84MHz.

This means that the smallest interval of time the Timer-Counter can measure is 2 / 84,000,000, or about 23.8 ns.

You can read more about the SAM3X8E Timer-Counters in the SAM3X8E reference manual, section 37, page 868.

Here's an Open Source library that provides a nice API to the Arduino Due's Timer-Counters. Here's a pretty clear explanation of how the SAM3X8E timers work, an alternative to reading the SAM3X8E reference manual.

You can also write ARM assembly code to measure durations in nanoseconds, but the Timer-Counters will probably have higher resolution.

Arduino Uno timers

The Arduino Uno uses the Atmega328 microprocessor. It has two Timer/Counters which can use different clock sources as input - the fastest source is FCLK_I/O (system clock, 16MHz). This means the smallest interval it can measure is 1 / 16,000,000 seconds or 62.5 nanoseconds.

You can read more about the timers in the AVR datasheet, Section 16, page 141. Or check out this summary of how to use the AVR native timers.

\$\endgroup\$
1
\$\begingroup\$

Arduino Uno/Nano micros() function (4us resolution), and my replacement function with 0.5us resolution:

I can't answer the question about the precision of the core Arduino micros() function when used on a Due. However, I wanted to present my work on the Arduinos using the Atmega328 microcontroller (such as the Uno, Nano, Ethernet, etc), as it may be helpful to others too.

Here it is: "I wrote a "libary" to get 0.5us precision on a "micros()" replacement function, so that I can get repeatable results reading a PWM or PPM signal, to within 1us. I searched all around the internet and could not find something comparable (or that was easy to use, and maintained the Arduino's ability to write PWM signals via the Servo Libary), so I think this is my first real contribution to the world of Arduino and Radio Control."

The code is downloadable here: http://electricrcaircraftguy.blogspot.com/2014/02/Timer2Counter-more-precise-Arduino-micros-function.html

\$\endgroup\$
0
\$\begingroup\$

I can't say anything about arduino libraries, but every ARM Cortex-M processor has a SysTick timer that increments on every system clock cycle. I work with the Cortex-M3, which has a 24-bit counter for SysTick so you can easily measure durations of several milliseconds with a precision of one clock period, or about 12 ns for an 84 MHz system clock. The normal use of SysTick is to set up periodic interrupts for multitasking.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.