Problem running three sonar SR04 with arduino uno

Hello,
I am trying to run three sonar sensors by an arduino. Basically for detecting obstacle (front, left and right). My code and circuit diagram is attached.

Problem is, first two sonar works. But the output of 3rd one is always 0 distance. I have changed place of sonar sensor(SR04). All three of them works independently. The third sonar connected always outputs high when i upload my code.

Hopefully i have properly stated my problem. I want to know how to make these three sonar work together. Thank you.

#define ledPin 8 //these are the led pins
#define ledPin2 9 
#define ledPin3 13

class Sonar{

 public:
 int triggerPin;
 int echoPin;
 long distance, duration;
void sonar(int trig, int echo){  //custom initializer
triggerPin = trig;
echoPin = echo;
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
}

bool isObstaclePresent(Sonar s){  //checking an obstacle is within less than or equal five centimeters

digitalWrite(s.triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(s.triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(s.triggerPin, LOW);
duration = pulseIn(s.echoPin, HIGH);
duration /= 29; //for converting miliseconds to centimeters
duration /= 2; //for getting the actual distance
distance = duration;
if(distance <= 5){ //checking if the distance is less or equal five
   return true;
 }
else{
   return false;
 }
}
};



Sonar sonar1;
Sonar sonar2;
Sonar sonar3;
void setup() {
 // put your setup code here, to run once:

sonar1.sonar(2, 4); // sonar(trig, eho);
sonar2.sonar(6, 7); 
sonar3.sonar(11, 12);
Serial.begin(9600);
}

void loop() {
 // put your main code here, to run repeatedly:
if((sonar1.isObstaclePresent(sonar1) == true) ){ //checking the obstacle for sonar1
 pinMode(ledPin,OUTPUT);
 digitalWrite(ledPin,HIGH);
 }
 else   digitalWrite(ledPin,LOW);

 
  if((sonar2.isObstaclePresent(sonar2) == true)){
    pinMode(ledPin2,OUTPUT);
    digitalWrite(ledPin2,HIGH);
 }
else  digitalWrite(ledPin2,LOW);


if((sonar3.isObstaclePresent(sonar3) == true) ){
 pinMode(ledPin3,OUTPUT);
 digitalWrite(ledPin3,HIGH);
 }
 else   digitalWrite(ledPin3,LOW);

 
 Serial.print("this is for sonar1: ");
 Serial.println(sonar1.distance);
 Serial.print("this is for sonar2: ");
 Serial.println(sonar2.distance);
 Serial.print("this is for sonar3: ");
 Serial.println(sonar3.distance);

}

sonar3.ino (2.05 KB)

Are you allowing any time at all to let the ultrasonic bursts generated by each trigger to sufficiently decay before firing the the next SR04? I found it takes about 20-40 milliseconds before the burst is undetectable. You may be running into the situation where the burst from the first (or second) SR04 is being received by the 3rd SR04 just after you fire the trigger for #3.
You must allow enough time between each trigger so that only the burst triggered is the one received, not one of the other ones.

Tried 20ms delay after all sonar check, still same result. The problem you mentioned should be noticeable in all sonars right? Not just the last one. I also tried different positions, but to no improvement.

(sonar1.isObstaclePresent(sonar1)What are you passing the object a reference to itself?

Are you using a breadboard like the one shown in the Fritz? Many of those longer breadboards have the power rails split in the middle. Some will indicate that by a break in the red line running the length of the rail, others may need to be measured for continuity. Usually only the red side is split, and requires a short jumper to connect the two halves together.

What are you passing the object a reference to itself?

I am aware of that. But this should not be a problem right? I can always make the function static so that i can call it by the class.

Are you using a breadboard like the one shown in the Fritz?

Yes i am using same breadboard as fritz.

Hi,

Yes i am using same breadboard as fritz.

What we need to know is, are the positive and negative rails running down the sides of the protoboard, continuous?
Some have a break halfway so you only have a supply on half the length of the board.

Check the supply voltages at each of your SR04 units.

Tom... :slight_smile:

miruza:
I am aware of that. But this should not be a problem right? I can always make the function static so that i can call it by the class.

It shouldn't be a problem, but like other stuff in the class, it looks a little bizarre.
I don't understand your comment about making the function static.