I have a problem with 2 of my 4 ultrasonic sensor's

hey everyone i am new to arduino project i have an obstacle avoiding car with 4 ultrasonic sensor's and 2 motors which are controlled by a l298n bridge i am using arduino mega with them but i have one problem it is that 2 of my ultrasonic sensor dont work i checked the power all of them have power i changed the pins still doesn't work i even changed my mega , the sensors are connected to four 4A batteries and my motors and arduino are connected to two 3.7v batteries and this is the code am using

int Front_Right_Echo = 2;
int Front_Right_Triger = 3;
int Front_Left_Echo = 4;
int Front_Left_Triger = 5;
int Front_Midel_Echo = 6;
int Front_Midel_Triger = 7;
int Back_Echo = 8;
int Back_Triger =9;


int in1 = 10;
int in2 = 11;
int in3 = 12;
int in4 = 13;


int minDistanse = 20;


int Left_Distance = 0, Right_Distance = 0, Middle_Distance = 0, Back_Distance = 0;

void _mForward() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}

void _mBack() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
 
}

void _mleft() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  
}

void _mright() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  
}


void _mStop() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  
}

//Ultrasonic distance measurement:
int Left_Distance_test() {
  digitalWrite(Front_Left_Triger, LOW);
  delayMicroseconds(2);
  digitalWrite(Front_Left_Triger, HIGH);
  delayMicroseconds(10);
  digitalWrite(Front_Left_Triger, LOW);

  float duration = pulseIn(Front_Left_Echo, HIGH);
  float distance = duration * 0.034 / 2;  // Speed of sound = 343 meters/second (0.034 cm/microsecond)

  return (int)distance;
}

int Middle_Distance_test() {
  digitalWrite(Front_Midel_Triger, LOW);
  delayMicroseconds(2);
  digitalWrite(Front_Midel_Triger, HIGH);
  delayMicroseconds(10);
  digitalWrite(Front_Midel_Triger, LOW);

  float duration = pulseIn(Front_Midel_Echo, HIGH);
  float distance = duration * 0.034 / 2;  // Speed of sound = 343 meters/second (0.034 cm/microsecond)


  return (int)distance;
}

int Right_Distance_test() {
  digitalWrite(Front_Right_Triger, LOW);
  delayMicroseconds(2);
  digitalWrite(Front_Right_Triger, HIGH);
  delayMicroseconds(10);
  digitalWrite(Front_Right_Triger, LOW);

  float duration = pulseIn(Front_Right_Echo, HIGH);
  float distance = duration * 0.034 / 2;  // Speed of sound = 343 meters/second (0.034 cm/microsecond)


  return (int)distance;
}

int Back_Distance_test() {
  digitalWrite(Back_Triger, LOW);
  delayMicroseconds(2);
  digitalWrite(Back_Triger, HIGH);
  delayMicroseconds(10);
  digitalWrite(Back_Triger, LOW);

  float duration = pulseIn(Back_Echo, HIGH);
  float distance = duration * 0.034 / 2;  // Speed of sound = 343 meters/second (0.034 cm/microsecond)
  return (int)distance;
}



void setup() 
{
  Serial.begin(9600);
  pinMode(Front_Right_Echo, INPUT);
  pinMode(Front_Right_Triger, OUTPUT);
  pinMode(Front_Left_Echo, INPUT);
  pinMode(Front_Left_Triger, OUTPUT);
  pinMode(Front_Midel_Echo, INPUT);
  pinMode(Front_Midel_Triger, OUTPUT);
  pinMode(Back_Echo, INPUT);
  pinMode(Back_Triger, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  _mStop();
}

void loop() {
  // Read sensor values
  Left_Distance = Left_Distance_test();
  Middle_Distance = Middle_Distance_test();
  Right_Distance = Right_Distance_test();
  
  Back_Distance = Back_Distance_test();
  
  if (Right_Distance > minDistanse && Left_Distance > minDistanse && Middle_Distance > minDistanse)
  {
    _mForward();
  }
  else if (Right_Distance < minDistanse && Left_Distance < minDistanse && Middle_Distance < minDistanse) {

    _mStop();
    {
      if (Back_Distance < minDistanse)
      {
        _mStop();
      }
      else if(Back_Distance > minDistanse){
        _mBack();
      }
    }
  } else if (Right_Distance > minDistanse && Left_Distance < minDistanse) {
    _mleft();
  } else if (Right_Distance < minDistanse && Left_Distance > minDistanse) {
    _mright();
  }
}

Can you explain how you determine they are not working? What was your clue?

yes my car works like this
if the sensors dont detect anything it will move forward if the front sensors detect an obstacle it will turn then check then move, i put an obstacle in front but the car didn't turn but when i put an obstacle in the left or right it dose turn
also excuse my poor language english isn't my first language

Have you replaced one of the faulty sensors with a new one and does it still not work?

the sensors are not faulty all 4 work fine i know that because i replaced the 2 that dont work with the ones that work and they worked fine, no matter what i do only 2 sensors work at once

Have you tried serial.Print() the value of the echo return microseconds to verify valid values?

Confirm all grounds connected properly.

how can i do that,
if u may help

all of them are connected to the same ground so if it's faulty none of them should work

Without building what you have, it looks like your code does exactly what it says it should do.

condition 1:

  if (Right_Distance > minDistanse && Left_Distance > minDistanse && Middle_Distance > minDistanse){
    _mForward(); // so this works according to you
  }

condition 2:

  else if (Right_Distance < minDistanse && Left_Distance < minDistanse && Middle_Distance < minDistanse) {
    _mStop(); // never mentioned this, does it stop?
    {
      if (Back_Distance < minDistanse)
      {
        _mStop(); // or stop in reverse?
      }
      else if(Back_Distance > minDistanse){
        _mBack(); // and reverse for a bit?
      }
    }
  } 

conditions 3 and 4 only involve the left and right sensors, so when you put an obstacle in front of the car (middle sensor?) how do you know the left and right sensors aren't missing the target obstacle altogether?

  else if (Right_Distance > minDistanse && Left_Distance < minDistanse) {
    _mleft();
  } 
  
  else if (Right_Distance < minDistanse && Left_Distance > minDistanse) {
    _mright();
  }

condition 1: works fine
condition 2: does not work because the front sensor cant detect any thing
also it does not reverse because only the left and right can detect the obstacles in front of them
but it does turn if there is something in the left or right
for how can i know i cant if u can help me with the code please

Apparently you are not willing to track down the source of your bug. Good luck.

You are wrong about the front sensor, because you said...

If all sensors work fine, you must examine your program.

Write a small sketch to test ONE ultrasonic sensor. Using this, test each sensor.
Write another small sketch to test two, three and four sensors.

Add your working sketch to the broken sketch.

2 Likes

Exactly. I would test each sensor independently in the back position

and add in Serial.print() to do as Paul suggested,
using a trimmed down version of the code such as this

int Front_Right_Echo = 2;
int Front_Right_Triger = 3;
int Front_Left_Echo = 4;
int Front_Left_Triger = 5;
int Front_Midel_Echo = 6;
int Front_Midel_Triger = 7;
int Back_Echo = 8;
int Back_Triger = 9;


int in1 = 10;
int in2 = 11;
int in3 = 12;
int in4 = 13;


int minDistanse = 20;


int Left_Distance = 0, Right_Distance = 0, Middle_Distance = 0, Back_Distance = 0;

void _mForward() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}

void _mBack() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);

}

void _mleft() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);

}

void _mright() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);

}


void _mStop() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);

}

//Ultrasonic distance measurement:
int Left_Distance_test() {
  digitalWrite(Front_Left_Triger, LOW);
  delayMicroseconds(2);
  digitalWrite(Front_Left_Triger, HIGH);
  delayMicroseconds(10);
  digitalWrite(Front_Left_Triger, LOW);

  float duration = pulseIn(Front_Left_Echo, HIGH);
  float distance = duration * 0.034 / 2;  // Speed of sound = 343 meters/second (0.034 cm/microsecond)

  return (int)distance;
}

int Middle_Distance_test() {
  digitalWrite(Front_Midel_Triger, LOW);
  delayMicroseconds(2);
  digitalWrite(Front_Midel_Triger, HIGH);
  delayMicroseconds(10);
  digitalWrite(Front_Midel_Triger, LOW);

  float duration = pulseIn(Front_Midel_Echo, HIGH);
  float distance = duration * 0.034 / 2;  // Speed of sound = 343 meters/second (0.034 cm/microsecond)


  return (int)distance;
}

int Right_Distance_test() {
  digitalWrite(Front_Right_Triger, LOW);
  delayMicroseconds(2);
  digitalWrite(Front_Right_Triger, HIGH);
  delayMicroseconds(10);
  digitalWrite(Front_Right_Triger, LOW);

  float duration = pulseIn(Front_Right_Echo, HIGH);
  float distance = duration * 0.034 / 2;  // Speed of sound = 343 meters/second (0.034 cm/microsecond)


  return (int)distance;
}

int Back_Distance_test() {
  digitalWrite(Back_Triger, LOW);
  delayMicroseconds(2);
  digitalWrite(Back_Triger, HIGH);
  delayMicroseconds(10);
  digitalWrite(Back_Triger, LOW);

  float duration = pulseIn(Back_Echo, HIGH);
  float distance = duration * 0.034 / 2;  // Speed of sound = 343 meters/second (0.034 cm/microsecond)
  return (int)distance;
}



void setup()
{
  Serial.begin(9600);
  pinMode(Front_Right_Echo, INPUT);
  pinMode(Front_Right_Triger, OUTPUT);
  pinMode(Front_Left_Echo, INPUT);
  pinMode(Front_Left_Triger, OUTPUT);
  pinMode(Front_Midel_Echo, INPUT);
  pinMode(Front_Midel_Triger, OUTPUT);
  pinMode(Back_Echo, INPUT);
  pinMode(Back_Triger, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  _mStop();
}

void loop() {
  // only left-front or right-front will work at at time
  Left_Distance = Left_Distance_test();
  Middle_Distance = Middle_Distance_test();
  Right_Distance = Right_Distance_test();
  Back_Distance = Back_Distance_test();

  if (Back_Distance < minDistanse)
  {
    _mStop();
  }
  else if (Back_Distance > minDistanse) {
    _mBack();
  }
}

Then, once the sensors are known working, add in each other condition and go from there.

1 Like

... and do not post two topics with the same subject...

@laithhabeeb

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

1 Like

look i am sorry if i didnt make this clear but what i meant is that i already did that(checked whether the sensors are working or not ) for the sensors i tested them one by one and they worked fine individually and they all can detect, but if i put them all together only 2 work the left and the right one ,
then i tried to change them with the back and front sensors, the previous sensors that were in the left and right are now in the front and back those 2 stopped working, and the others that were not working are now in there place they worked. this was another confirmation that all sensors are good and not faulty

so i am now confused is the problem with the code or with the sensors and how i connected them to the arduino

thank u good sir but if u may help me with one more thing how do i check the value's with this Serial.print()
i know this maybe a dumb question to ask but where do i put this in the code and how do i check the value's with it
am new to all of this so bare with me

Sure, something like this. Make sure your serial monitor in the IDE is set to 9600 baud to match setup()

void loop() {
  // only left-front or right-front will work at at time
  Left_Distance = Left_Distance_test();
  Middle_Distance = Middle_Distance_test();
  Right_Distance = Right_Distance_test();
  Back_Distance = Back_Distance_test();

  if (Back_Distance < minDistanse)
  {
    Serial.print("Back Distance: ");
    Serial.println(Back_Distance);
    _mStop();
  }
  else if (Back_Distance > minDistanse) {
    Serial.print("Back Distance: ");
    Serial.println(Back_Distance);
    _mBack();
  }
}


1 Like

I suggest that you insert some time between pings. When using multiple rangefinders I usually put in 30us to 50us between pings to let previous echos die out.

1 Like