Help for two "HC-SR04" ultrasonic sensor...

Hello
English is not my native language, then accept my apology. Thank you.

I have two "HC-SR04" Ultrasonic sensors.

I want the sensor 1 as the sender
And I use sensor 2 as receiver. (Please see the image)

I've put my code below ...
But, this code does not work for the given conditions

  • Do you see this as a limitation inherent in the "HC-SR04"sensor?
  • If yes, what sensors for the conditions you recommend?
  • Is my code needs to be improved?
  • If yes, what changes need to be done?
//sensor1
int PingPin5 = 13; //Transmitter
int EchoPin5 = 5; // Inactive
//


//Sensor2
int PingPin4 = 12; //Inactive
int EchoPin4 = 4; // Reciver
//

void setup(){
  
 //pinMode(PingPin4, OUTPUT);
 pinMode(PingPin5, OUTPUT);
 
 pinMode(EchoPin4, INPUT);
 //pinMode(EchoPin5, INPUT);
 
 Serial.begin(9600);
}

void loop(){
  unsigned long duration4, duration5, mm;
  
  duration4 = pulseIn(EchoPin4, HIGH);
  
  digitalWrite(PingPin5,LOW);
  delayMicroseconds(2);
  digitalWrite(PingPin5,HIGH);
  delayMicroseconds(5);
  digitalWrite(PingPin5, LOW);
  
  
  mm = MicroStoMm(duration4);
  Serial.print("pin 4: ");
  Serial.print(mm);
  Serial.println("mm");
  
  delay(1000);
  
}

unsigned long MicroStoMm( unsigned long MicroS){
return (MicroS/2.9411)/2;
  
}

thank you so much and Merry Christmas.

See the schematics in the attached file that i made
and use this code

//*
 HC-SR04 Ping distance sensor:
 VCC to arduino 5v 
 GND to arduino GND
 Echo to Arduino pin 7 
 Trig to Arduino pin 8

 Created by Mudassir Hussain
 */


#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED

int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
 Serial.print("Distance");
 Serial.println(distance);
 //Delay 50ms before next reading.
 delay(50);
}

Connect VCC and GND of both the sensors to same Arduino

Please Add Karma if this helped you.
Merry Christmas

mudassir9999:
Please Add Karma if this helped you.

This is the first time I've ever seen anyone explicitly asking for Karma points. Are they important?

Haha, I've a milestone of 100 Karma points :stuck_out_tongue:

mudassir9999:
Haha, I've a milestone of 100 Karma points :stuck_out_tongue:

But actually asking for them? Sort of defeats the purpose, IMO.
People will give you Karma points if they feel that you deserve them. You shouldn't have to ask for them.
You don't get any prizes, you know.

I apologies :frowning:

mudassir9999:
I apologies :frowning:

No need for apologies. My opinion isn't necessarily the same as others'.
But I do personally think it's just better to leave people to think of it themselves.
We don't want to mimic YouTube etc. :slight_smile:

mudassir9999:
See the schematics in the attached file that i made
and use this code

Thank you very much for your answer.

Everything looks good, but Output Distance (in serial monitor) is 176 or 168 OR 185 cm, in all conditions.

for example:
Sensors are face to face in 20 CM, but output distance = 174 OR 168 OR 185 cm.

Do you think this is related to the timing of transmitter and receiver?

thank you very much.

Yes, This is because of Timing.
The sonic waves emitted by the Transmitter are reflected by an object and received back in the Receiver, The system calculate the TIMING of each wave "How much does that wave took to come back to the receiver" and gives you the OUTPUT.But in your case the Sound wave is not reflecting to any object but directly coming into the receiver.
Keep the two modules side by side and see, I'm sure it will give you right values with good precision.

mudassir9999:
Keep the two modules side by side and see, I'm sure it will give you right values with good precision.

hi
Unfortunately, it doesn't work.

the modules & frequency are Equal and Wiring is correct, but I do not know why it doesn't work?

please look at attached file...

best regards

Hi,
What is the application, why do you need to use two sensor assembly and their Tx or Rx only?

Your picture shows the back of them, and they are facing in the same direction, just side by side, which can be done with one sensor assembly.

Tom..... :slight_smile:

the modules & frequency are Equal and Wiring is correct,

I just looked that attached picture, Can you tell me Where is that Green and Yellow wire are connected to.? In the picture it seems both of them are connected to ECHO pins of those two modules.

mudassir9999:
I just looked that attached picture, Can you tell me Where is that Green and Yellow wire are connected to.? In the picture it seems both of them are connected to ECHO pins of those two modules.

I think you might mean the "trig" pins. On my modules, at least, the "Trig" pin is next to the Vcc pin.
I agree, though, that it looks like the same pin on both modules is connected, rather than the "Trig" pin on one and the "Echo" pin on the other.

And Tom raises a very good point - if they're both pointed in the same direction, what's the benefit of using two modules? I imagined them pointing toward each other, so that one sent a ping and the other recieved it to indicate the distance between them. The way they're currently oriented, one sensor would suffice.