# Arduino Due precision with function micros

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.

• 4 µs is 4,000 ns. Be more specific with your required precision please. – W5VO Dec 17 '13 at 16:25
• Can you clarify why you mentioned the Arduino Due? Is your question regarding the Arduino Uno or the Due? – Joe Hass Dec 17 '13 at 17:28
• 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. – Ignacio Vazquez-Abrams Dec 17 '13 at 18:21
• And of course I meant micros(). That'll teach me to type/read too fast. Or not. – Ignacio Vazquez-Abrams Dec 17 '13 at 19:04
• You can read a regular timer directly once it is set up. Timer2 is often unused. – jippie Dec 17 '13 at 19:36

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.

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."