2 Ultrasonic Sensors working together - possible wiring problem

Hi guys,

I’m also experimenting on the subject of 2 sensors with 1 servo.
I have the 4 pin sensors (VCC, TRIG, Echo, GND).

The first one worked great, by seeing if the distance is < Xcm and then turning the motor if something is close.

When I connected a second sensor, I thickened the code to add all it’s parameters, and said that:

if ((cm < 10) && (cm2 < 10)) {
    myservo.write(90);
    Serial.println(cm); 
    Serial.println(" from #1 sensor"); //display the distance from sensor
    Serial.println(cm2);
    Serial.println(" from #2 sensor");
    delay(250);
  }
  else {
    myservo.write(0);  
    Serial.println("Too Far");
    delay(250);
  }

What gets printed in the serial monitor is something like this:

Too Far
Too Far
8
from #1 sensor
0
from #2 sensor
7
from #1 sensor
0
from #2 sensor
4
from #1 sensor
0
from #2 sensor
Too Far
Too Far

The above situation happens when I place objects in front of BOTH sensors. So #1 gets distance, #2 doesn’t.

So basically it’s telling me that something is wrong with the input from the #2 sensor, because the cm2 variable is 0, so it doesn’t pick up any distance at all.

I don’t want to paste the whole code here because I think it might be the wiring, not the code.
Please take a look at the pic and tell me if something is wrong.

Take note of the Black and Yellow wires. Are they correct?

I hope you can understand the drawing. Just in case - the 2 sensor (I don’t have the image), are in opposite order of the input pins, so the 4 right-most rows represent each one, where the sticker is.

Thanks!

Hi,
I’m attaching the full code, since I tested the other sensor separately, and it seems to be working fine.

So it’s either a power problem, which is unlikely, or it’s a code issue. Here is the full code:
I would really appreciate any help on this.

Thank you! :slight_smile:

#include <Servo.h> 

Servo myservo; 

int pos = 0; 
int i = 0;
const int trigPin = 13;
const int echoPin = 12;
const int trigPin2 = 6;
const int echoPin2 = 5;

void setup(){
  
  myservo.attach(7);  
  Serial.begin(9600); 
 
  for(pos = 0; pos <= 180; pos += 1) {                               
    myservo.write(pos);
    delay(15);
  } 
  for(pos = 180; pos>=0; pos-=1) {                                
    myservo.write(pos);
    delay(15);
  } 
}  
 
void loop(){ 
  
  long duration, duration2, cm, cm2;
  
  pinMode(trigPin, OUTPUT); 
  pinMode(trigPin2, OUTPUT);
  digitalWrite(trigPin, LOW); 
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  digitalWrite(trigPin2, LOW);
  
  pinMode(echoPin, INPUT); 
  pinMode(echoPin2, INPUT);
  duration = pulseIn(echoPin, HIGH); 
  duration2 = pulseIn(echoPin2, HIGH);
  
  cm = microsecondsToCentimeters(duration); 
  cm2 = microsecondsToCentimeters(duration2);
  
  if ((cm < 10) && (cm2 < 10)) {
    myservo.write(90);
    Serial.println(cm); 
    Serial.println(" from #1 sensor"); 
    Serial.println(cm2);
    Serial.println(" from #2 sensor");
    delay(250);
  }
  else {
    myservo.write(0);  
    Serial.println("Too Far");
    delay(250);
  }

}

long microsecondsToCentimeters(long microseconds) {
  
  return microseconds / 29 / 2;
}

i have same exact problem :

According to that pic, you have 2x GNDs and no power to the lower sensor. It has GND from the rail, and a GND fed from the upper one.

Side note: you also need to connect the servo -ve / battery -ve to Arduino GND.

Hi JimboZA,

Thanks. I made a few mistakes in the diagram, sorry for that, can you take a look now?
I lack the image for the sensor, so I wrote 2 sensor with their exact pin order:

The top sensor has its pins going from GND2 as the leftmost (black wire) to VCC2 which is in blue.
The bottom sensor has its pins going from VCC1 as the leftmost (yellow wire that connects to the other sensor’s VCC2) to GND1 (black wire)

There was also a wire connecting the battery to the GND, which I forgot to put in the drawing, now it’s there.

Any ideas?
This is the way it was connected, I just updated the diagram to the correct situation it’s in.

Thanks!

I have to mention again, that with the current setup, both sensor work if tested separately, but not together.

It must be a code issue. It is attached in the above posts.

Thanks.

You're still not showing power to the lower sensor.

Assuming it has power, I wonder if the issue can be to do with one's ultrasonic beam being read by the other and confusing it? Or are the pointing apart?

The pointing is apart, the sensors need to be able to display separate distances.

But - what do you mean there is no power to the lower sensor? the VCC line is connected between them (yellow) - from 5V on arduino it goes to the VCC2, then to VCC1...

Thanks.

You are triggering the two sensors at more or less the same time.
How do you know which echo belongs to which sensor?

Ok I didn't see the tiny yellow line but it's connect to the upper one's black and back to the lower one. So lower one has no power, still got 2x grounds afaics.

Sorry I don't understand JimboZA, what do you mean it's connected to the upper one's black? the GND2 and GND1 are connected both to the bottom row of the breadboard, and this way they have GND to the arduino.
What's the problem?
Please notice the order of the pins of the sensors, it's confusing but they are reverse from each other in terms of pin locations on thebreadboard.

AWOL:

  duration = pulseIn(echoPin, HIGH); 
  duration2 = pulseIn(echoPin2, HIGH);
  
  cm = microsecondsToCentimeters(duration); 
  cm2 = microsecondsToCentimeters(duration2);

you can see that each sensor has a pulse feedback into a cm or cm2, then these get divided into a distance by the microsecondsToCentimeters function. Each should have a separate distance depending on the object from the sensor....

The problem is that sensor 2 is showing always "0", so I can't figure out why...

Thanks.

The problem is that sensor 2 is showing always "0", so I can't figure out why..

Because the first "pulseIn" blocks waiting for the echo for from the first sensor, by which time, the echo for the second has come...and gone?

Ok, right sorry, I was misreading the pic

AWOL, thanks for replying.

I modified for a small delay between them but still it shows "0" while the first sensor shows the correct distance.
What else could be done?

  pinMode(echoPin, INPUT); //set to recieve data from sensor
  pinMode(echoPin2, INPUT);
  duration = pulseIn(echoPin, HIGH); //gets length of time of bounce
  delay(100);
  duration2 = pulseIn(echoPin2, HIGH);

Thanks

The sensor doesn't hold on to the echo until you're ready to read it.

AWOL, i'm not sure I understand exactly what you mean.. do you have a suggestion about the change needed in the code?

added note - the code gets the pulse directly into the duration parameters so it shouldn't be some sort of a time issue... the problem must be somewhere else...

Thanks.

If anyone has any ideas please tell me, I keep debugging the code but nothing seems to work out..

Thanks again!

Post the code you've got and a schematic.

Thanks:

#include <Servo.h> 

Servo myservo;

int pos = 0; 
int i = 0;
const int trigPin = 13;
const int echoPin = 12;
const int trigPin2 = 6;
const int echoPin2 = 5;

void setup(){
  
  myservo.attach(7);  // attaches the servo on pin 7 to the servo object 
  Serial.begin(9600); //set up communication with PORT
  
  for(pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees                               
    myservo.write(pos);
    delay(15);
  } 
  for(pos = 180; pos>=0; pos-=1) {                                
    myservo.write(pos);
    delay(15);
  } 
}  
 
void loop(){ 
  
  long duration, duration2, cm, cm2;
  
  pinMode(trigPin, OUTPUT); //set initial LOW, then 2microsec HIGH to start sensor
  pinMode(trigPin2, OUTPUT);
  digitalWrite(trigPin, LOW); //then back to LOW again to start measuring
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); //enough to reset the sensor
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10); //enough to reset the sensor
  digitalWrite(trigPin, LOW);
  digitalWrite(trigPin2, LOW);
  
  pinMode(echoPin, INPUT); //set to receive data from sensor
  pinMode(echoPin2, INPUT);
  duration = pulseIn(echoPin, HIGH); //gets length of time of bounce
  
  duration2 = pulseIn(echoPin2, HIGH); //PROBLEMATIC VARIABLE!!!!!!!!!!!!
  
  cm = microsecondsToCentimeters(duration); //convert time into distance  
  cm2 = microsecondsToCentimeters(duration2);
  
  Serial.print(duration); //debug
  Serial.println(); //debug
  Serial.print(duration2); //debug
  Serial.println(); //debug
  
  if ((cm < 10) && (cm2 < 10)) {
    myservo.write(90);
    Serial.println(cm); 
    Serial.println(" from #1 sensor"); //display the distance from sensor
    Serial.println(cm2);
    Serial.println(" from #2 sensor");
    delay(50);
  }
  else {
    myservo.write(0);  
    Serial.println("Too Far");
    delay(50);
  }

}

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;
}

This code has some debug lines to figure out why cm2 is 0. I think that because pulseIn goes directly into duration and duration2, there shouldn’t be a problem.
The problem is to understand why duration2 is 0.

Attached is also a schematic of the wire setup as it is.
It is important to understand that with a code made for 1 sensor, both sensor work when tested separately.
Notice please in the image the different pins of the sensors, they are in mirrored positions. you might have to zoom in a bit.

Thanks a lot! :slight_smile:

Try sending a ping from one sensor and time the echo, wait maybe 20ms, then send a ping from the other sensor, and time its echo.