Distance between two ultrasonic sensors (hc-sr04)

Hi there,

I am trying to calculate the distance between two ultrasonic sensors but making one of them emit and the other receive it but I am not sure what went wrong. Here is my code:
I am using the pins below:
(Ultrasonic Receiver)
2: Ground
3: Echo
4: Trig
5: Vcc
(Ultrasonic Emitter)
A2: Vcc
A3: Trig
A4: Echo
A5: Ground

//Code Start
int led = 13;

void setup() {
  pinMode(led, OUTPUT);   
  pinMode (A2,OUTPUT);//attach pin 2 to vcc
  pinMode (A5,OUTPUT);//attach pin 5 to GND
  pinMode (2,OUTPUT);//attach pin 2 to vcc
  pinMode (5,OUTPUT);//attach pin 5 to GND
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
digitalWrite(A2, HIGH);
digitalWrite(5, HIGH);
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(A3, OUTPUT);// attach pin 3 to Trig
  digitalWrite(A3, LOW);
  digitalWrite(led, LOW);
  delayMicroseconds(2);
  digitalWrite(A3, HIGH);
  digitalWrite(led, HIGH);
  delayMicroseconds(5);
  digitalWrite(A3, LOW);
  digitalWrite(led, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode (3, INPUT);//attach pin 4 to Echo
  duration = pulseIn(3, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
 
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 
  delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
//Code End

I kept getting 0 for the duration and I am not sure what I did wrong.
Thanks in advance.

Code looks fine, I'm not sure how close match receiver/transmitter pair of sensors on the same board, may be that one board is tuned slightly differently than other?

I tried on the both on the same board and different board it is still the same. I am not sure what I did wrong.

Have a link to your sensor? I try to read a doc linked in the code to http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf but it looks like wrong one, 3 pins configuration. Yours has 4?

Magician:
Have a link to your sensor? I try to read a doc linked in the code to http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf but it looks like wrong one, 3 pins configuration. Yours has 4?

Sure thing. It is a four pin instead of three.

The timing diagram of HC-SR04 is shown. To start measurement, Trig of SR04 must receive a pulse of high (5V) for at least 10us, this will initiate the sensor will transmit out 8 cycle of ultrasonic burst at 40kHz and wait for the reflected ultrasonic burst. When the sensor detected ultrasonic from receiver, it will set the Echo pin to high (5V) and delay for a period (width) which proportion to distance. To obtain the distance, measure the width (Ton) of Echo pin.

You are using the code for 3-pin sensor, find right library for your 4-pin sensor, sure there are hundreds posted online. You may try to fix what you have, than hint:

digitalWrite(A3, LOW);    // <<<<<<<---- A3 is a trig
  digitalWrite(led, LOW);
  delayMicroseconds(2);
  digitalWrite(A3, HIGH);
  digitalWrite(led, HIGH);
  delayMicroseconds(5);
  digitalWrite(A3, LOW);
  digitalWrite(led, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode (3, INPUT);//attach pin 4 to Echo
  duration = pulseIn(3, HIGH);    //<<<<<<<<---- should be 4 - receiver.

There may be more errors, check again. Other things, I'd advise not to use digital pins as power source line, under the load voltage 'd drop for about 1V, leaving only 4V for sensor

Just a follow up question: Could you manage to measure the distance between two sensor? Did it work in the end? Was it accurate enough?

I'm working on a similar project and have been able to get descent results thus far.

Perhaps I'm not following the logic of this.

These sensors work by knowing when a pulse was emitted and reading how long it took TO COME BACK.

This discussion is about sending at one location and receiving at a different location. There is no COME BACK. How would Sensor A know exactly when Sensor B pinged it and be able to calculate anything?

Why wouldn't one sensor at one location be able to determine the distance to the second location? That's what the sensor is for....

Like I say, probably not following the logic.

Like I say, probably not following the logic.

Obviously, one side has to be configured as dumb repeater, whenever it's get pinged, it immediately generates a response ping. All math is the same, if response time < 3 usec it's probably don't need to be corrected on the receiver side, as error < 1 mm., but may be accounted for longer delay time. Method able to work on much bigger distance and doesn't require a flat reflective surface

Yes, so obvious it hasn't been mentioned in the 10 or so posts of this 10 month old thread until now and the problem hasn't been solved.

The major problem while measuring the distance between two ultrasonic sensor is that, the Receiver sensor will not know when the transmitter sensor has sent the Ultrasonic wave. Also a HC-SR04 module cannot work as receiver only, you have to use the trigger pin to enable echo pin.

So If we are trying to be successful here we should somehow trigger both the Ultrasonic sensor at the same time. I tried out a method which was partially successfully, I do not claim to have solved the problem but I am close.
We can use one trigger pulse as a dummy one to get sync with the receiver's trigger as shwon in below image

I have documented the entire concept here
Measuring Distance between two HC-SR04

Aswinth:
The major problem while measuring the distance between two ultrasonic sensor is that, the Receiver sensor will not know when the transmitter sensor has sent the Ultrasonic wave. Also a HC-SR04 module cannot work as receiver only, you have to use the trigger pin to enable echo pin.

So If we are trying to be successful here we should somehow trigger both the Ultrasonic sensor at the same time. I tried out a method which was partially successfully, I do not claim to have solved the problem but I am close.
We can use one trigger pulse as a dummy one to get sync with the receiver's trigger as shwon in below image

I have documented the entire concept here
Measuring Distance between two HC-SR04

yeah it suppoed to work .good algortim but we dont know until we try

Aswinth:
The major problem while measuring the distance between two ultrasonic sensor is that, the Receiver sensor will not know when the transmitter sensor has sent the Ultrasonic wave. Also a HC-SR04 module cannot work as receiver only, you have to use the trigger pin to enable echo pin.

Although posting on a three-year-old thread seems....wrong...

...in fact an HC-SR04 can work as a receiver only. Carefully read the schematic and the David Pilling post linked here and you will see that it is possible to listen in on the receiver without triggering the unit. It will take some hacking.