Reading Values from Two Ultrasonic Sensors One After the Other

Hello,

I am trying to read the values of two ultrasonic sensors one after the other using the pulseIn() function. I have read that it is not possible to read the values of ultrasonic sensors simultaneously, but I am not try to do it simultaneously (although I would like it to read the sensors values at least 500 milliseconds after the other). I have tried using the pulseIn() command but it does not seem to be reading the values of the second sensor. I was hoping for help with this issue.

I have attached my code below.

Thank you in advance for the help!

#define trigPinOne 12
#define echoPinOne 11
#define trigPinTwo 13 
#define echoPinTwo 10  


int distance_in_one = 0; 
int distance_in_two = 14; 

int duration_one = 0;
int distance_sum_one;
int distance_one=0;

int duration_two = 0; 
int distance_sum_two; 
int distance_two=0;

int i; 

void setup(){

  Serial.begin(9600);
  Serial.println("Testing has begun \n");
  pinMode(trigPinOne, OUTPUT);
  pinMode(echoPinOne,INPUT);
  pinMode(trigPinTwo,OUTPUT); 
  pinMode(echoPinTwo,INPUT);
}

void loop(){
   distance_sum_one=0;
   distance_sum_two=0;
   
   for(i=0;i<5;i++){
    
   digitalWrite(trigPinOne, LOW);
   delayMicroseconds(2); 
   digitalWrite(trigPinOne, HIGH);
   delayMicroseconds(10);
   digitalWrite(trigPinOne,LOW);

   digitalWrite(trigPinTwo,LOW);
   delayMicroseconds(2); 
   digitalWrite(trigPinTwo,HIGH); 
   delayMicroseconds(10); 
   digitalWrite(trigPinTwo,LOW);
    
    
    duration_one=pulseIn(echoPinOne);
    duration_two=pulseIn(echoPinTwo);
    distance_sum_one+=duration_one*0.0135/2;
    distance_sum_two+=duration_two*0.01235/2;
  }

   distance_one=distance_sum_one/5;
   distance_two=distance_sum_two/5;
  
  
    Serial.print("DistanceOne: ");
    Serial.println(distance_one);
    Serial.print("DistanceTwo: "); 
    Serial.println(distance_two);
 

  delay(1000);
}

but I am not try to do it simultaneously

Well that code is trying to do it simultaneously.
You need to pulse one sensor and then measure the pulse from the sensor.

The have a short delay to let the sounds settle down, and then do it for all the second sensor.

Thanks for the reply Grumpy Mike. The sensors will be placed 180 degrees apart, so I presume interference between the two sensors shouldn't be that much of an issue correct?

I modified by code and it works well know!

Thank for the feedback. Any other suggestions on how to modify by code to make it more time efficient would be greatly appreciated.

#define trigPinOne 12
#define echoPinOne 11
#define trigPinTwo 13 
#define echoPinTwo 10  


int distance_in_one = 0; 
int distance_in_two = 14; 

int duration_one = 0;
int distance_sum_one;
int distance_one=0;

int duration_two = 0; 
int distance_sum_two; 
int distance_two=0;

int i; 

void setup(){

  Serial.begin(9600);
  Serial.println("Testing has begun \n");
  pinMode(trigPinOne, OUTPUT);
  pinMode(echoPinOne,INPUT);
  pinMode(trigPinTwo,OUTPUT); 
  pinMode(echoPinTwo,INPUT);
}

void loop(){
   distance_sum_one=0;
   distance_sum_two=0;
   
   for(i=0;i<5;i++){
    
   digitalWrite(trigPinOne, LOW);
   delayMicroseconds(2); 
   digitalWrite(trigPinOne, HIGH);
   delayMicroseconds(10);
   digitalWrite(trigPinOne,LOW);
   duration_one=pulseIn(echoPinOne,HIGH);
   
   delayMicroseconds(2); 
   
   digitalWrite(trigPinTwo,LOW);
   delayMicroseconds(2); 
   digitalWrite(trigPinTwo,HIGH); 
   delayMicroseconds(10); 
   digitalWrite(trigPinTwo,LOW);
   duration_two=pulseIn(echoPinTwo,HIGH);
   
   distance_sum_one+=duration_one*0.0135/2;
   distance_sum_two+=duration_two*0.01235/2;
  }

   distance_one=distance_sum_one/5;
   distance_two=distance_sum_two/5;
  
  
    Serial.print("DistanceOne: ");
    Serial.println(distance_one);
    Serial.print("DistanceTwo: "); 
    Serial.println(distance_two);
 

  delay(1000);
}

Pat1287:
I modified by code and it works well know!

Thank for the feedback. Any other suggestions on how to modify by code to make it more time efficient would be greatly appreciated.

#define trigPinOne 12

#define echoPinOne 11
#define trigPinTwo 13
#define echoPinTwo 10

int distance_in_one = 0;
int distance_in_two = 14;

int duration_one = 0;
int distance_sum_one;
int distance_one=0;

int duration_two = 0;
int distance_sum_two;
int distance_two=0;

int i;

void setup(){

Serial.begin(9600);
  Serial.println("Testing has begun \n");
  pinMode(trigPinOne, OUTPUT);
  pinMode(echoPinOne,INPUT);
  pinMode(trigPinTwo,OUTPUT);
  pinMode(echoPinTwo,INPUT);
}

void loop(){
  distance_sum_one=0;
  distance_sum_two=0;
 
  for(i=0;i<5;i++){
   
  digitalWrite(trigPinOne, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPinOne, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinOne,LOW);
  duration_one=pulseIn(echoPinOne,HIGH);
 
  delayMicroseconds(2);
 
  digitalWrite(trigPinTwo,LOW);
  delayMicroseconds(2);
  digitalWrite(trigPinTwo,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinTwo,LOW);
  duration_two=pulseIn(echoPinTwo,HIGH);
 
  distance_sum_one+=duration_one0.0135/2;
  distance_sum_two+=duration_two
0.01235/2;
  }

distance_one=distance_sum_one/5;
  distance_two=distance_sum_two/5;
 
 
    Serial.print("DistanceOne: ");
    Serial.println(distance_one);
    Serial.print("DistanceTwo: ");
    Serial.println(distance_two);

delay(1000);
}

Sure! Get rid of the "int" unless you really need a number bigger than what can be stored in a "byte". Ints take extra code to handle a two byte field.

Also, you are using arithmetic with decimal points being computed with integers.
distance_sum_one+=duration_one0.0135/2;
distance_sum_two+=duration_two
0.01235/2;

Also, the compiler will probably compute 0.0135/2 unless you use parenthesis to specify you want the whole product divided by 2. What do you really want computed. Better use float for distance and duration.

Probably others.

Paul

I would take this bit

digitalWrite(trigPinOne, LOW);
   delayMicroseconds(2); 
   digitalWrite(trigPinOne, HIGH);
   delayMicroseconds(10);
   digitalWrite(trigPinOne,LOW);
   duration_one=pulseIn(echoPinOne,HIGH);

And replace it with a function where you pass in the two PIN numbers you want to use and return the duration value. That way you simply call the same function for two different sensors.

Those pins are also better stored in an array.

The moment you start using numbers in your variable names, you're probably really looking for arrays. So that combined with part of the advice from #4 and #5 you can get this:

byte nSensors = 2;
byte trigPin[nSensors] = {12, 13};
byte echoPin[nSensors] = {11, 10};
float distance[nSensors];

void loop() {
  for (byte i = 1; i < nSensors; i++) {
    distance[i] = readSensor[i];
  }
}

float distance (byte sensor) {
  float d;
  // Read sensor on trigPin[sensor] and echoPin[sensor] and store it in d.
  return d;
}

Note also how easy it is now to add more sensors, if you need to later.