Electronic – Arduino Due precision with function micros

arduinoprecisiontime

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.

Best Answer

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.