The problem is in calculating the distance between two arduino with HC-SR04.

Hello. I have two Arduino, and each of which is connected to the ultrasonic sensor HC-SR04. I try to calculate the distance between them, sending a signal from one Arduino and taking to another. I decided to make sure that if no signal was sent from the first Arduino, then on the second Arduino, when I try to receive the emitted signal (it is not being emitted at the moment), it will be output 0. But no, when measuring, it outputs not understandable values that are approximately equal to each other, similar either to some kind of interference or to noise. Both sensors are working. Can someone come across a similar problem or can suggest how to implement my idea. Thank you in advance.
P.S. I also tried to repeat this experiment, but I encountered the same problem.
How to measure distanse between two ultrasonic sensors

Setup:

Programming code for Transmitter (Arduino Mega 2560):

const int trigPin = 9;
const int echoPin = 10;
const int ledPin = 13;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT); 
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600); 
}

void loop() {
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  delay(2);
}

Programming code for Receiver (Arduino Uno):

const int trigPin = 9;
const int echoPin = 10;
const int ledPin = 13;

long duration;
int distance;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  while (digitalRead(echoPin)==HIGH){
    delay(2);
  }
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance= duration*0.034;
  Serial.println(distance);
  delay(2);
}

Result:

I turn off the transmitter and try to make sure that the receiver does not receive anything:

Programming code for Receiver:

const int trigPin = 9;
const int echoPin = 10;
const int ledPin = 13;

long duration;
int distance;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  Serial.println(duration);
  delay(2);
}

Result:

The result is the same. Similar values were obtained, although there is no signal from the transmitter.

Do post your code, photos of your setup, and actual results.

But before that, please do read the sticky (or if you did already, do it again and follow the instructions on how to post a proper question, as with the very limited info you posted there's not much we can do).

The trick is of course in triggering the receiving HC-SR04 so it actually listens for incoming pings.

wvmarle:
The trick is of course in triggering the receiving HC-SR04 so it actually listens for incoming pings.

Which can be done (and has been described on this forum and elsewhere) with a wired connection or wirelessly.

I know, can be done, but also has to be done, and for all we know OP doesn't even try to trigger the other sensor. Can't say any other sensible thing about this without OP coming back with much more info on their setup.

Since it appeared that the OP was looking for suggestions and not a critique of any particular setup, it seemed appropriate to give some hints (albeit obvious and easily "google-able" ones) about how to accomplish what you mentioned.

One could also say that the method in the OP's "PS" link (which does not require a wired connection or a radio/IR link to trigger the receiver, but which does require calibration at a known distance, and which the OP tried and failed to implement) looks like it ought to work but has some limitations - one of which is that it could eventually become inaccurate due to timing drift (edited to remove the strike-out because on 2nd (3rd?) thought, I think drift would eventually become a problem after the initial calibration to get the two uCs in sync).

wvmarle:
Do post your code, photos of your setup, and actual results.

But before that, please do read the sticky (or if you did already, do it again and follow the instructions on how to post a proper question, as with the very limited info you posted there's not much we can do).

The trick is of course in triggering the receiving HC-SR04 so it actually listens for incoming pings.

I added a photo of the configuration, code, and the result of the experiment. In Google I previously searched, found nothing. I tried to synchronize through a wired connection, and through a wireless one (as now). In all cases, I encountered the problem that the receiver receives any values even when the transmitter does not send anything.

Don't edit your post, but create a new post. Then we'll see it (I'm not going to read back to see if something maybe has changed).

Checked your post - has code indeed but no images attached.

Code as posted won't work for your purpose: there's nothing to indicate a second communication channel between sender and receiver as discussed above. The two send their ping and start listening independently instead of synchronised.

The sender could fire a laser or wired-trigger over to the receiver, a signal; account for latency of the receiving and other factors. After a programmed, short set time, fire the Arduino to send, with the receiver already in receive mode and before receipt time out.

Initially, just have the receiver on and hunt down those stray noise makers. I had a ultrasonic humidifier in the same room as a sonic project. I found putting a shallow dish around the sensor removes a lot of bounced around pulses.

If using the SR04's put a cap right across the power to ground pins, for better stability.

You might do the mod to tune the receiver from 38kHz to 40kHz, use your favorite search engine.

Lift the units off the floor. The SR04/5 have a 15 degree cone of transmission traveling at 700ish MPH, what happens to the ultrasonic wave hitting the floor is your guess.

I am sure the frequency attunation of a piece of tape over the receivers transmitter is not going to stop a 40Khz signal from getting out. Al foil might work better.

Pull the Xmitt sensor on the receiver, though you might encounter VSWR issues. You can find data on the i-net on which pin from the chip is the transmit pin, lift that leg to get rid of any VSWR issues.

Spurious reflections from the rather wide beam are no problem as it's only the first (presumably direct line) signal that matters. Any later signals are ignored.

It just means you have to allow for some time (10-100 ms) between pins for echoes to die out, or you may get triggered by an old one the moment you start listening again.

Thank you all for help. I will try)
Can you please tell me how to make the correct synchronization. From the receiver over a wired connection, send a signal to the transmitter and pick up the delay so that they start simultaneously?

Easiest is wired. Connect a signal wire (and a ground wire) between the two.

Receiver's trig signal can go to both the HC-SR04 on that side, and the Arduino on the other side. Receiver then starts the pulseIn() as usual.

Then when the trigger signal is seen by sender (use an interrupt for best reaction time, after all timing is critical here), it sends a trigger to the sending HC-SR04. Use a RISING interrupt here, to trigger on the rising edge, and do the trig sequence within the ISR.

If wired no need for 2nd uC. One can trigger both sensors and listen to the RX sensor echo pin the normal way.