Nanosecond time measurement with Arduino due

Hello,
I'm new to Arduino due. For my project I need to measure time in nanosecond fineness. (Like measuring the travel time of radio waves in air).
If I go into a little detail, I have boards such as Raspberry Pi 3 B+, Arduino DUE, Esp32.
In the tests I made at home, DUE gave an accuracy of +- 1 us and a stable time measurement. However, in field tests, the arduino due measures the travel time of radio waves to 0 when 600 meters away from the transmitting station. I measure time with simple micros command.
For this reason, I started looking for more sensitive ways to measure time. And I need to know how to measure in nanosecond precision. The accuracy I'm looking for is 0.5 us or more, like 200 nanoseconds.
I'm waiting for your help.
Thanks.

Does the Due have input capture, like the AVRs?

If so, use that instead.

You're all over the place; it starts with nanosecond precision, then you talk about using micros() to measure time and finally it ends up being 200-500ns. Which is it...?
Anyway, I would say that 200-500ns resolution should be perfectly feasible with something in the range of a Due, ESP32 or Pi3. Input capture would work nicely (@anon73444976) and is a standard feature of timers most microcontrollers, so I expect it to be there at least on the Due (can't vouch for the ESP32 with its inherent 'Chineseness' of implementation). Alternatively a pin change interrupt combined with a timer could be used.
Either option of course does depend on a reliable trigger signal being present. You've got that covered?

Thanks for the quick response.
Currently I am using ebyte e32 for radio waves. I integrated it with arduino due. And it works fine. The algorithm is as follows
The start is given.
microsecond is sent to the station.
The station reflects the incoming time as it is.
As soon as the transmitter receives the reflected time data, it generates microseconds again.
The difference between the two times is taken and divided by two and the delay is subtracted.

This is my algorithm. It is best for me to measure time as precisely and accurately as possible.

Ok, just checking, it could have been any other kind of data that's being sent to the station, correct? You're not doing anything with the fact that it happens to be a microseconds timestamp?

When you say 'sent to the station', who/what does the sending and how? Is this internal WiFi of the ESP32, LoRa, etc.?

     Serial3.println(micros());
     delayMicroseconds(6000000);


 if (Serial3.available()>1) {
  
   // Serial.println((String)"uretilen:"+micros()+" \n gelen zaman:"+Serial3.readString()); 
      producedtime=micros();
      sendingtime=Serial3.readString().toDouble();
     
        
   if(i>1 && i<10){
              
        float difference=((producedtime-sendingtime)/2)-3003242.50;
        screenprint(difference ,i);
          
         }
          i++;
   
    }
    else{
        display.clearDisplay();
        display.setTextSize(2); 
        display.setCursor(5,35);             // Start at top-left corner
        display.println("No Signal");
        display.display();
        
           }

reflective station esp32
The station where the time calculations are made is the arduino due

I'm very new to this stuff. That's why my code is very simple. I am sending data via serial communication. Use-interpret tx3 and rx3 pins.

I have simple arduino due . I don't know what. "input capture, like the AVRs?" i'm so new :slight_smile:

Input capture refers to a capability of using the hardware clocks and timers to directly measure pulse widths to much higher resolution (small multiples of the processor clock) than is available through micros().
Usually, such facilities are very processor-specific, and are not usually supported by portable libraries
They are not a beginner topic

Not an ideal choice at all.

The E32 is a UART front ended module so there is a microcontroller between the LoRa receiver and indication of output. There may be all sorts of internal delays, which could be variable, from when a packet actually arrives and data starts appearing.

The standard SPI based LoRa modules have a IO pin which can be set to activate directly when the LoRa IC has finished receiving a packet so at least the indication of arrival can be considered consistent.

Or you could just swap LoRa devices and use the ones that will do the entire distance measurement process for you, circa 50m up to 85km.

I have tried this before, on Arduino DUEs, with SPI based LoRa modules.

One DUE would send a packet and wait for a reply from the other.

The variation in total round trip time, measured with micros() was around 10us, so that represents a distance variation of circa 1.5km, for units that dont move.

This variation is a problem..

So what variation in round trip time were you seeing ?

1 Like

I started the tests with esp32 at first. I did not make any changes to the code structure and links. I observed that when the distance between the transmitter and the receiver in Esp32 exceeds 300 meters, the measured time interval increases by 1 µs. But esp32 wasn't doing it stably. In other words, when the distance increased to 600 meters, I read values like 1 us or 4 us instead of 2 us. For this reason, I decided to replace the developer board with an arduino due with an 84 MHz crystal oscillator. But I saw that the time difference measured at 300 meters and 600 meters with due remained 0 us. For this reason, I decided to share this situation with you here.

Do you have any suggestions as far as I know E220 series supports SPI.

Are you talking about the İnterrupts?

Thank you guys for your help and comments.
But is there a timer that will help me measure time in nanoseconds? How can I create?

On its own that little snippet of code does not tell us much about what is going on.

Can you Post the full code for both ends of the link, the transmitter and receiver ?

1 Like

Measuring nS is not something you do in software. Get a hardware timer front end or your jitter is going to render things meaningless as you already finding out.

1 Like

In fact, the other part of the code multiplies the difference variable calculated here by 300 and prints it on the screen. Other than that, there is nothing.

Hi there @sagapps,

You can use library tc_lib for measuring digital signals with DUE's ATSAM3X8E microcontroller hardware TC modules. In this post you have more information about the performance of the library:

The post reports measurements of signals with tc_lib with frequencies greater than 20Mhz (period of 50 nano secs.)

I hope it helps.

Thank you for your help. I need to figure out how to adapt it to my code. I use serial communication in my application, so I use duenin TX3 and RX3 pins. How can I adapt it. any idea?