Measure distance by radio waves

I would like to build up a distance measuring system by using radio waves, instead of the commonly used way - Ultrasonic.

Firstly, assuming there's no interference between two objects(object A and object B.), and the distance in between is dozens of kilometers.

So what I use are two 900Mhz radio modules, and connect each of them to a Arduino Uno to be Object A and Object B.

Here is the logic of design:

At object A, radio module are controlled to transmit a beacon(a specific character) every 3seconds,
At object B, when radio module receive this specific character, it will return another character back(assuming as returning beacon).

By using the Arduino at object A to calculate time interval between transmitting beacon and receiving returning beacon, it is possible to invert the distance between A and B, if doing (speed of light)* (time interval)/2.

The code are attaching below,

Object A

unsigned long U;
char C;
char K;
unsigned long P;

void setup() {
Serial.begin(9600);
}

void loop() {
delay(3000);        //transmitting beacon every 3 seconds
U = micros();       // T1  
Serial.print('L');    //The specific character(transmitting-beacon) 

 if(Serial.available()>0);     // if receive returning-beacon
 {
  C = Serial.read();
  switch(C)
  {
  case 'J':
  P = micros()-U; ()     // should be the time interval? T2 - T1
   Serial.print(P);        //print the time interval
   break;
  }
 }
}

Object B

char N;
char L;
char K;
void setup() {
Serial.begin(9600);
}
void loop() {
  
if(Serial.available()>0);       // if receive the transmitting-beacon from object A
{
  N = Serial.read();
  switch(N)
  {
    case 'L':
    Serial.print('J');             // returning-beacon 
    break;
  }
}
}

Since electronic wave travel at time speed, 300000 km / sec = 300 meter / microsecond
The expected time-interval should be around 200 microsecond if the distance in between is 30km; however, the value I actually got was way more different from "200", which were 12 , 16 or 20.

I think the problem is definitely about the programming issue... which I couldn't figure out yet.

So I am here looking for any suggestion.
Thanks in advance !

micros() has a granularity of 4 µsecs.

Serial is a very slow function.

Radio waves are very fast.

...R

Robin2:
Thanks for your reply,
I did some little correction, which is making the returning signal with 1 seconds delay in between receiving and returning back,
but it doesn't make any difference, very strange......

I am now totally confused by Arduino.

Your code is wrong, you are most likely reading a previous answer to the ping from your receiver and what you are measuring is the time it takes to execute the following code

if(Serial.available()>0);     // if receive returning-beacon
 {
  C = Serial.read();
  switch(C)
  {
  case 'J':
  P = micros()-U; ()     // should be the time interval? T2 - T1

In an ideal world you would wait after sending your beacon for the return of that ping. But you have an if, not a while not received, so because your receiver answers very slowly (serial at 9600 bauds) after sending your ping there is no character to read, you go back to the loop, wait 3 secs and then send a new ping and go read the answer from the previous ping. Because you reset your time counter, you really measure the time it. Takes to execute your code.

Alternatively you could send time stamps rather than letters and have the time stamp returned to do measurements. But Robin's points will probably come bite you and add challenges that are orders of magnitude bigger than what you try to measure

ken810616:
I am now totally confused by Arduino.

An Arduino is far far too slow to measure distances using the propagation time of radio waves - even if you use the least time consuming code - for example detecting a flash from an LED.

The time taken for an interrupt to get to the start of an ISR would be sufficient to make a nonsense of the measurements - before you take account of the time to run code that you might write.

...R

9600 baud communications take over 104 microseconds PER BIT. You are not going to receive a one-character answer in less than about a millisecond (1000 microseconds).

1 microsecond resolution would get you 300 meter resolution but the microseconds() function only has 4-microsecond resolution so the best you can do with that is 1.2 km resolution.

You MIGHT be able to get more accurate timing using the Input Capture feature of Timer1. You would need to connect it to the TX and RX pins and time between the leading edge of the outgoing start bit and the leading edge of the returning start bit. At 16 MHz you could get 62.5 nS (18.7 meter) resolution.

johnwasser:
9600 baud communications take over 104 microseconds PER BIT. You are not going to receive a one-character answer in less than about a millisecond (1000 microseconds).

1 microsecond resolution would get you 300 meter resolution but the microseconds() function only has 4-microsecond resolution so the best you can do with that is 1.2 km resolution.

You MIGHT be able to get more accurate timing using the Input Capture feature of Timer1. You would need to connect it to the TX and RX pins and time between the leading edge of the outgoing start bit and the leading edge of the returning start bit. At 16 MHz you could get 62.5 nS (18.7 meter) resolution.

Thanks John,
Your comment is very very helpful for me, at least letting me to know what I am currently doing is not that accurate and correct.
And I am highly interested in the INPUT CAPTURE you said, which may be working and having higher accuracy on the system. But I don't have any context and idea on it, do you know if there are some source or coding example that I can refer to?
Thank you !

Try this

I would be very interested to see the pair of programs (one at each end of the link) that can give 62.5nS resolution (even in theory).

...R

If you are using OOK'd simple modules you have no chance whatsoever of making this work
as the modulating bandwidth is a few kHz.

If you use a transceiver chip driving packets with 2Mbit/s modulation you might have some
vague possilibity of getting useful round-trip times, since a bit time is then only 160m - but
at that bandwidth you'd need more power.

By the way your notion of using 900MHz over dozen's of km falls foul of the curvature of the
earth - UHF signals need line-of-sight for reasonable propagation, you'd need a tower.

MarkT:
If you are using OOK'd simple modules you have no chance whatsoever of making this work
as the modulating bandwidth is a few kHz.

If you use a transceiver chip driving packets with 2Mbit/s modulation you might have some
vague possilibity of getting useful round-trip times, since a bit time is then only 160m - but
at that bandwidth you'd need more power.

By the way your notion of using 900MHz over dozen's of km falls foul of the curvature of the
earth - UHF signals need line-of-sight for reasonable propagation, you'd need a tower.

Hello,
thanks for the comment,
I am not sure about the modulating bandwidth, I guess it may be 20Mhz? . This is exact the radio module I use, so called Xtend 900(XTend 900 1W RPSMA - 40 Mile Range - WRL-09411 - SparkFun Electronics).

Concerning the line of sight issue, yes it is totally correct! However, this experiment will be conducted with a radio-module installing on a sounding rocket, the other on the ground. Consequently, this is quite different from normal ground base experiment.
But I am sure the signal will definitely lost once the rocket flying out from the line of sight.

This technique doesnt work because you dont know the propagation time of the data you are transmitting thru the radios.
You are assuming its zero.
In real life the time it takes for the receiver to demodulate a data stream depends on a mass of variables which are very hard to measure accurately, but are all much greater than the actual propagation time of the radio signal.
Radar is the only way to accurately measure distances by radio, and even then it gets problematic for very short distances.

The normal way of achieving distance measuring is to put a GPS receiver on the device you want to measure the distance of , another GPS at the measuring point and have the device being measured transmit back its GPS coordinates.
You can then calculate the distance between the 2 gps receivers.

Yes, but a reasonably dumb radio will have a constant delay. Just subtract that delay to estimate the distance.

MorganS:
Yes, but a reasonably dumb radio will have a constant delay. Just subtract that delay to estimate the distance.

Yes!! I would say I am pretty naive in the field of radio waves transmission, and as you said, subtracting the delay, which I assume it is quite constant, is exactly the way that I expect how it may work.

The simplest system that I can envisage would have the base unit switch on an LED, The distant unit would detect that with a photodetector and an interrupt and cause its own LED to be switched on. The base unit would, in turn, detect that with an interrupt and measure the time from the moment the first LED was turned on.

That simple system has a lot of computation as well as the uncertainty about how long it takes two interrupts to respond.

If you actually want to send a message the amount of computation will be dramatically higher and will probably run foul of timing uncertainty due to other background interrupts.

...R

This made me think of metal detectors, sending out pulses of RF and detecting changes in the echoes. The key point here is that no timing is performed, only comparison between the return signal and the original signal, used to generate a tone, representing the degree of (dis)similarity between the two.

If we now scale up to consider three objects in a more or less like-sided triangle configuration, if two of those send an RF signal at exactly the same time, could the third perhaps employ similar processing as that which occurs in a metal detector, to calculate phase shift, indirectly obtaining some idea of distances, rather than relying on travel time?

I'm out of my depths here, just some random thoughts! Be gentle! :slight_smile:

Metal detectors don't send out pulses of RF and detect echoes, they use near-field.

They usually detect change of mutual inductance of two coils, or the detuning of an LC circuit
by change in the inductance of the L.

Perhaps you are getting confused with radar? Metal detectors have a sensitivity that drops as
the cube of distance, like any magnetic phenomena, and are very sensitive to the type, size, shape
and orientation of the object, not just the distance.

MorganS:
Yes, but a reasonably dumb radio will have a constant delay. Just subtract that delay to estimate the distance.

This is basically the uncertainty principle - for a 1kHz modulated RF carrier, the accuracy with which you
can determine the start of a pulse is limited by the low bandwidth of the modulator and demodulator - the
pulse envelope is a few kHz, so pulse rise and fall times are 100's of microseconds, mixed in with noise
and fading - this is true whatever the carrier wave, even a laser....

There are ways to extract better time information, but they are complex, and not dumb. GPS has to do
this, and its non-trivial.

Much easier to modulate at 1MHz or so and get orders of magnitude better time information. You still
have noise and fading to worry about even so, and you use more spectrum.