simultaneous use of two HC-SR04 sensors

Hello everyone,
I'm a mechanical engineering student and I have to realize a project, with Arduino, for the university.
I'm trying to use two HC-SR04 sensors at the same time, but only a sensor (sensor 1) gives me the correct value.

#define ECHO1_PIN 12
#define ECHO2_PIN 9
#define TRIG1_PIN 13
#define TRIG2_PIN 10

void setup() {

  Serial.begin(9600);
  pinMode(ECHO1_PIN,INPUT);
  pinMode(TRIG1_PIN,OUTPUT);
  pinMode(ECHO2_PIN,INPUT);
  pinMode(TRIG2_PIN,OUTPUT);
  digitalWrite(TRIG1_PIN,LOW);
  digitalWrite(TRIG2_PIN,LOW);


}

void loop() {
  
  digitalWrite(TRIG1_PIN,HIGH);
  delayMicroseconds(20);
  digitalWrite(TRIG1_PIN,LOW);
    
  float time_micro1 = pulseIn(ECHO1_PIN,HIGH);
  float distance1 = 0.034 * time_micro1 / 2;

  digitalWrite(TRIG2_PIN,HIGH);
  delayMicroseconds(20);
  digitalWrite(TRIG2_PIN,LOW);
  
  float time_micro2 = pulseIn(ECHO2_PIN,HIGH);
  float distance2 = 0.034 * time_micro2 / 2;

//after 38ms it is out of the sensor range
if( time_micro1 > 38000 ){
Serial.println("sensor 1 is out of range " + String(distance2) );
}

if( time_micro2 > 38000 ){
Serial.println(String(distance1) + " sensor 2 is out of range");
}

else{ 
  Serial.println("distance 1 : " + String(distance1) + " distance 2 : " + String(distance2) );
  delay(2000);
 }
}

Unfortunately I don't have much knowledge of the C ++ programming language and the Arduino IDE, for this I kindly ask you for help.
Thank you all

Hi there, Which Arduino board do you use? Does this Arduino board work in a 3.3V or 5V system?

I've met the same problem with you, it turned out HC-SR04 doesn't support 3.3V, especially when you use two. In my case, after doing a lot of searching, I give up the HC-SR04, because I found another ultrasonic distance sensor which supports 3.3V. Best of all, it only needs 3 pins(echo and trigger is the same pin!), that save the IO resource. Perhaps, it will work with you too. :smiley:

You say only sensor1 gives "the correct value" but what values are you expecting and what values do both sensors give you?

What is the physical arrangement of the sensors? Do you realise that two sensors pointing in the same direction are always going to interfere with one another?

Steve

You can add a timeout to the pulseIn() function as third parameter, so you don't have to wait a full second in case no echo is received.

Chances are that the second sensor hears an echo of the ping of the first, and thus gives a false trigger. Wait a bit between readings (20-50 ms or so should be enough).

ainsfeiii:
Hi there, Which Arduino board do you use? Does this Arduino board work in a 3.3V or 5V system?

I've met the same problem with you, it turned out HC-SR04 doesn't support 3.3V, especially when you use two. In my case, after doing a lot of searching, I give up the HC-SR04, because I found another ultrasonic distance sensor which supports 3.3V. Best of all, it only needs 3 pins(echo and trigger is the same pin!), that save the IO resource. Perhaps, it will work with you too. :smiley:

https://www.seeedstudio.com/blog/2019/11/04/hc-sr04-features-arduino-raspberrypi-guide/

Hi ainsfeiii, my Arduino board works in 5V like sensors. I have connected the 5V port with the breadboard with a jumper to which I have connected the sensors' power jumpers in parallel, always with the breadboard.
I have done some research and I have found informations about the TTL level converter, but I do not understand if it can help me or not.
Thank you for your help

slipstick:
You say only sensor1 gives "the correct value" but what values are you expecting and what values do both sensors give you?

What is the physical arrangement of the sensors? Do you realise that two sensors pointing in the same direction are always going to interfere with one another?

Steve

Hi steve, considering orthogonal directions of the sensors , respect the prefixed direction 210 cm long the results of the sensors are :

sensor 1 : 298 cm (while sensor 2 is directed in another orthogonal direction)
sensor 2 : 155 cm (while sensor 1 is directed in another orthogonal direction)

Could it be a voltage problem?
Thank you for your help

wvmarle:
You can add a timeout to the pulseIn() function as third parameter, so you don't have to wait a full second in case no echo is received.

Chances are that the second sensor hears an echo of the ping of the first, and thus gives a false trigger. Wait a bit between readings (20-50 ms or so should be enough).

Hi wvmarle, I have tried to put in the code a " delayMicroseconds(30); " between the two reads, but nothing is changed.
Thank you for your help

Don't try to use ultrasound pingers at the same time, it will be unreliable at best. You have to let the ping subside before using another sensor (or the same one) again. You never see bats hunting together for the
same reason.

1 Like

lorenzo19_98:
Hi wvmarle, I have tried to put in the code a " delayMicroseconds(30); " between the two reads, but nothing is changed.
Thank you for your help

I suggested you to wait between two pings 1,000 times longer than you apparently do. I'm not surprised that 30 µs doesn't make a difference.

MarkT:
You never see bats hunting together for the same reason.

Maybe bats in my part of the world are smarter than yours? I have seen bats hunt in close proximity, with my bat detector picking up the calls from many bats. Definitely actively hunting based on the movement and the sounds themselves.

No idea how bats do it but they can also fly around caves with many animals at the same time, forming dense clouds as they leave their roosts, yet not crashing into one another much (it does happen of course) or against the cave walls or other obstacles. Somehow they have the ability to recognise their own voice or so, filtering out all the other noise.