How can I use HCSR04 to measure distance between two object?

Hi, I am working on a project to measure distance between two objects (A and B) using Arduino.

Both these two objects are moving with time among other objects, but there is always a Line of sight available (assumed for now). After some research I concluded to use ultrasonic sensors.
My idea is to use an ultrasonic transmitter on object A and a receiver on object B.

arduino ultrasonic sensor

Now my understanding of ultrasonic sensors comes from the above animation from this Arduino ultrasonic sensor tutorial blog

From this what is understand is, the HCSR04 module has Tx and Rx units and they work independently. Meaning I can connect the two set of generic HCSR04 ultrasonic sensors and Arduino on object A and object B.

Then on object A I can just give a 10ms pulse to trigger pin of sensor and listen to incoming pulse on object B using the Echo pin of the ultrasonic sensor

I thought this would work until I realized I don't know when exactly the pulse was trigger from Object A to do my calculation as mentioned in the blog.

I tried using RTC on both to sync time, but something tells me it won't be reliable.
So it appears I am stuck, is there any way to do this better? I am even willing to scarp the progress and use a better sensor if something like that is available.

I don't know if I correctly got what you're asking, but I can say you don't need the exact time but just the time elapsed between the pulse and the (first) received echo. To do that, use a call to "pulseIn()" returning the delay in microseconds. Given the sound speed in air (at sea level) you can then calculate a total distance, divided by 2 to get the distance (the sound made two times the distance).

Why do you believe it won't be reliable?
I think it is quite doable.

With the transmitter is on one object and the receiver on the other then measuring the time taken between the transmission and reception requires that the receiving system knows when the transmission was done. Can two RTCs maintain microsecond accuracy to achieve this ?

There is also the problem of how the receiver will know the time that the transmission was done or even that it had happened

Just adding more details: I don't know why you said you want to put "an ultrasonic transmitter on object A and a receiver on object B". If your goal is to let both objects "know" the distance between them, you just need to use a full sensor on both (I suggest SRF05 instead of SR04 but the concept is the same) so each one can get the distance independently.

PS The only drawback I can see is the fact they shouldn't send the trigger burst at the same time (the burst from A could be received by B thinking it was his echo) so some adjustement is needed, like trying to make collision unlikely by using larger pulse intervals (e.g. a pulse every 1 or 2 seconds or more, depending on the required precision along the time) or an algorythm to discard "collision" readings and change the timing accordingly.

There is an important factor mentioned in the original post

I have added emphasis to the important part.

Because the other objects are present you cannot just use an ultrasonic send/receive device on one or both both target objects because you will get reflections from the other objects and will not be able to pick out the target

You're absolutely right.
But as far as we can understand (due to missing any other information or reference to the environment) we can just assume like the objects are "fluctuating" somewhere and the requirement of a reciprocal distance measurement can be easily solved using a "full" sensor each.

Once we'll be able to understand more requirements for this project, we 'll be able to say more.

I agree that we are short on detail, but I don't see how this can be achieved when multiple objects are in the field of view

The HCSR numbers bounce around a lot.
OP's reach exceeds HCSR's grasp.

You could try an alternate method of synchronizing the two objects, like a beam of light or a radio signal. Then you send the sync signal at the same time that the transmitter sends the ultrasonic pulse.
On the receiver, the time difference between the sync signal and the ultrasonic signal being received will give you distance from transmitter.

Yes, the only information provided is:

but there is always a Line of sight available (assumed for now).

Can't do much more without knowing what is the complete scope.

I've done this. It seemed to be reliable. There was a constant small latency that had to be deducted. No big deal.

Please share your sketches

Lost in the mists of time. But it worked like this:

I had two Arduinos and called one the "master" and the other a "slave." The master has a NRF24L01 radio and a HC-SR04. The slave has a NRF24L01 radio and a modified HC-SR04.

I did not remove the receive transducers from the master HC-SR04. The master's receive transducer isn't used but there's no point in removing it.

I removed the transmit transducer from the slave HC-SR04. The slave's transmit transducer isn't used, but it might cause spurious signals, so it probably should be removed.

I used the NewPing library.

The system operates like this:

The "master" radio transmits a packet, triggers its HC-SR04 using sonar.ping(), waits "x" seconds, repeats. The master does not use the value that sonar.ping() returns.

The "slave" waits for a radio packet from the master. When the slave receives a packet, it triggers its HC-SR04, reports the distance using sonar.cm (which will be half the distance between the master and slave), and then waits for another packet.

A small distance correction was applied to account for latency.

Ran for a few hours with no obvious glitches.

Sorry, I thought that this was a current project

The problem is the RTC has to be kept in sync with an accuracy in milliseconds. I am not sure if that can be done

Also with time RTC sync will drift off?

This might work, I have to factor in the additional components and complexity though

This is brilliant. I like the way how it can be done without any additional hardware. For anyone reading this in the future, here is an image on how the sync is achieved

In the above diagram, the Tx represents Transmitter sensor and Rx represents Receiver sensor. As shown the Transmitter sensor will be made to transmit US waves at a periodic known delay, this is all it has to do.

In the Receiver sensor we have to somehow make the trigger pin go high exactly during when the transmitter pin goes high. So initially we randomly make the Receivers Trigger to go high which will and stay high till the echo pin goes low. This echo pin will go low only when it receives a US wave from the transmitter. So as soon as it goes low we can assume that the Transmitter sensor just got triggered. Now, with this assumption as soon as the echo goes low we can wait for the known delay and then trigger the receivers trigger. This would partially sync the trigger of both the Transmitter and receiver and hence you can read the immediate echo pulse duration using pulseIn() and calculate the distance.
I found the solution on this blog How To Measure Distance Between Two Ultrasonic Sensors using Arduino

Thanks all

1 Like

The synchronising could be done using gps.

Give each object a gps receiver, and use the 1PPS (one pulse per second) output to do the synchronising.

In the following oscilloscope traces, the connections are:

  • Channel 1 - gps #1 PPS output
  • Channel 2 - 10µs HC-SR04 #1 trigger pulse
  • Channel 3 - gps #2 PPS output
  • Channel 4 - 10µs HC-SR04 #2 trigger pulse

and on a faster time base:

I would have tested out the proposal, but I couldn't find 2 x HC-SR04s.
I've got some on order.

The two gps PPS outputs were within a few nanoseconds of each other.
The pulses to trigger the HC-SR04s were always less than 10µs apart. Sound only travels 3mm in this time.

A. The Rx echo pin goes low when sound from Tx hits Rx, not when "Tx got triggered." There will be an unknown delay between when Tx is triggered and when Rx echo goes low, equal to the time it takes for sound to go from Tx to Rx.

B. So then if you do this...

...then the Tx and Rx are not in sync. The reason can be seen in the following:

where:
X = time for sound to go from Tx to Rx
Y1 = Rx's first measured pulse length, which is random
Y2 = Rx's second measured pulse length (triggered the known delay after receiving the first pulse), equal to approximately the "known delay," not equal to X
.
.
.
Maybe I'm missing something, but I don't see a way for Rx to ever know or even grossly estimate the value of X using this scheme.

Also, this does not implement the @Delta_G method that you quoted at the top of your post.