Problem with 2 ultrasonic HC-SR04 sensors

Hi! I have a problem trying to read 2 HCSR04 at once. I've used the simple code and the Ultrasonic and NewPing Libraries, but the problem is the same.
Both sensors are facing in the same direction, but 45 cm apart. The problem is that when I put an object to one of them, and slowly putting away, around 25 cm the other sensor would start showing a reading, but around 95-99 cm. Are their getting interfences from one another? Because if dont put any object on them, both of them will show no reading (0cm), because the max distance is 100cm. Could be the wiring?

The two sensors have their echo and trigger pins each.

Hi @imanolbetanzos ;
Can you post a drawing showing the relative positions of sensors and objects to facilitate understanding?
A drawing speaks more than 1000 words.

RV mineirin

Hi, here are two pics of the sensors.
We figured out that if we erase the sensor.ping_cm() function of the code of any of two sensors, the readings weren't affected, so, it isn't about of the wiring, but the code instead.

Make a test by placing a wide and long strip of cardboard between the 2 sensors in such a way that the reflection of the sound of one does not reach the other.

RV mineirin

We already did that, exaclty like you said. It isn't that either. Their angle of detection is not affected by that.

Unless the object in front of one of the sensor is a parabolic reflector, the echo will be reflected just like the original pulse is sent as a cone of sound, not a single point like a laser.
You need to change from triggering both sensors at the same time, to triggering them alternately with time for the other pulse and echo to dissipate.
Paul

Hi Paul, we don't trigger the sensors at the same time, at least that's what we think.
This is part of our code:
NewPing der(5, 4, MAX_DISTANCE);
NewPing izq(3, 2, MAX_DISTANCE);

int d=der.ping_cm();
int i=izq.ping_cm();

As you can see, we use different Echo and Trigger pins for each sensor

To facilitate understanding post your sketch and the arduino wiring diagram
When posting sketch, place between </> tags.

RV mineirin

1 Like

Here's the sketch:

#include <NewPing.h>
 
#define MAX_DISTANCE 100 // Max distance in cm.
 
NewPing der(5, 4, MAX_DISTANCE);
NewPing izq(3, 2, MAX_DISTANCE);
 
void setup() {
  Serial.begin(57600);
  
}
 
void loop() {
int  d=der.ping_cm();
int  i=izq.ping_cm();
  Serial.print("Izquierda: ");
  Serial.print(i);
  Serial.println("cm");
  
  Serial.print("Derecha: ");
  Serial.print(d);
  Serial.println("cm");
  delay(50);
    }

And here's the wiring:

Try this sketch.
I put a delay 300 mseg, between pingA ande pingB.

RV mineirin

#include <NewPing.h>

#define MAX_DISTANCE 100 // Max distance in cm.

NewPing der(5, 4, MAX_DISTANCE);
NewPing izq(3, 2, MAX_DISTANCE);

void setup() {
  Serial.begin(57600);

}

void loop() {
  int  d = der.ping_cm();
  delay(300);
  int  i = izq.ping_cm();
  Serial.print("Izquierda: ");
  Serial.print(i);
  Serial.println("cm");

  Serial.print("Derecha: ");
  Serial.print(d);
  Serial.println("cm");
  delay(50);
}
1 Like

@ruilviana Thank so much! Such a simple thing and it's been causing us problems all week. It worked with the delay! We changed it to 100 ms, but it still works. :smiley: :clap:

Paul @Paul_KD7HB saw the problem in the post #6.

RV mineirin

That's true, thank you both @ruilviana @Paul_KD7HB :smiley: