4x Ultrasonic Sensors problem

Hello guys, I'm working on my college final project and I have to make a automatic cleaning robot and I have a kinda big problem. :confused:

For controlling the motors i used L293D on top of arduino, it has to do something with it?

I have 4x ultrasonic sensors HC-SR04 connected at digital pins 2,3,4,5, ECHO and TRIG are connected together to use less pins. But my readings are innacurate. Every once in a while I get 0 value and don't know what to do. I used bigger delay but nothing changes. Even if i disconnect other sensors the readings remain like this. I get values like this:

12
12
12
0
12
12
12
0
12
11
12
0
11
11
0

For reading the values i used ping_cm from newping library. Thank you!

If it is the only thing going wrong: discard all 0 values.

zwieblum: If it is the only thing going wrong: discard all 0 values.

How could I do this? I'm not that good in Arduino. And thank you for the fast replay Could you give me an example?

Well, with an if-statement.

Will you be posting the program so we can take a look, or would you like us to guess? At this point I'm not sure how we would guess at anything where 6 pins from unknown ultrasonic sensors connected to digital inputs produce any values other than 0 and 1.

It’s a lot of code, it includes even code for bluetooth but i will post it.

#include <AFMotor.h>
#include <NewPing.h> 


AF_DCMotor motor3(3); 
AF_DCMotor motor4(4); 
AF_DCMotor motor1(1);
AF_DCMotor motor2(2); 

#define MAX_DISTANCE 10000 [b]I have used 10000 because it gives more consistent reading. If i put below 300 it goes crazy[/b]

NewPing sonar[4] = { 
  NewPing(3, 3, MAX_DISTANCE),
  NewPing(6, 6, MAX_DISTANCE), 
  NewPing(5, 5, MAX_DISTANCE), 
  NewPing(4, 4, MAX_DISTANCE) 
};


char comanda;



void setup() {
  Serial.begin(9600);
  delay(5000);

motor3.setSpeed(180);
  motor3.run(RELEASE);
motor4.setSpeed(180);
  motor4.run(RELEASE);
motor1.setSpeed(10);
motor1.run(RELEASE);
motor2.setSpeed(10); 
motor2.run(RELEASE);

}


void loop() {
delay(60);
if(Serial.available() > 0){
  comanda = Serial.read();
  Stop(); 
switch(comanda){ 
case 'F':
  forward();
  perie();
  break;

case 'B':
  back();
  perie();
  break;

case 'L':
  stanga();
  perie();
  break;

case 'R':
  dreapta();
  perie();
  break;

case 'A':
  aut();
    } 
  }
Serial.println(sonar[0].ping_cm());
delay(300);


}


void forward()
{
  motor3.setSpeed(180);
  motor4.setSpeed(170);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}

void back()
{
  motor3.setSpeed(180);
  motor4.setSpeed(170);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
}

void stanga()
{
  motor3.setSpeed(180);
  motor4.setSpeed(180);
  motor3.run(BACKWARD);
  motor4.run(FORWARD);
}

void dreapta()
{
  motor3.setSpeed(180);
  motor4.setSpeed(180);
  motor3.run(FORWARD);
  motor4.run(BACKWARD);
}

void Stop()
{
  motor3.setSpeed(0);
  motor3.run(RELEASE);
  motor4.setSpeed(0);
  motor4.run(RELEASE);
  motor1.setSpeed(0);
  motor1.run(RELEASE);
  motor2.setSpeed(0);
  motor2.run(RELEASE);
}

void perie()
{
  motor1.setSpeed(10);
  motor1.run(BACKWARD);
  motor2.setSpeed(10);
  motor2.run(FORWARD);
}

void aut()  {      [b]This is where I tried to make it run alone but it acts like crazy[/b]
  
  if(sonar[1].ping_cm() > 10){   
    forward();
  }
  
  if(sonar[1].ping_cm() < 9){
    back();
    delay(1500);                
    dreapta();
    delay(500);
  }
  
  if(sonar[3].ping_cm() > 20){
    back();
    delay(2000);
    stanga();
    delay(2000);
  }

  
}

The only serial output I see is sonar[0].ping_cm().

What are the digital pins 2, 3, 4, 5, ECHO, and TRIG (those two don't exist in the Arduino world, by the way, we need the pins numbers) connected to? You say the sensors, but the only time '2' appears is in reference to the motors and one of those includes '1'. which you can't be using pin 1, so that cannot be the pin number. So back to the question of what is connected to pins 2, 3, 4, 5? Where are ECHO and TRIG connected? What oes the function call "sonar[0].ping_cm()" do?

You should also post a wiring diagram and the ultrasonic sensor part numbers so it can be verified if you are wiring and using them correctly.

I have some mistakes in my writing, sorry. I’m from Romania and don’t know how to explain properly in english.

TRIG and ECHO of the ultrasonic sensor HC-SR04 are connected together.
All the 4 sensors are conncted to digital pins 3, 4, 5, 6. Every pin from 3 to 6 is a TRIG and ECHO.
I used sonar[0].ping_cm() from NewPing library - Arduino Playground - NewPing Library sonar ping end a ping, returns the distance in inches or 0 (zero) if no ping echo within set distance limit

I tried to do something like in this video: https://youtu.be/I9UEWkUHS2U

Thank you very much.

Are the four sensors acoustically isolated from each other so the mounting material cannot carry the sound to one of the other sensors?

Paul

Paul_KD7HB: Are the four sensors acoustically isolated from each other so the mounting material cannot carry the sound to one of the other sensors?

Paul

They are something like this https://i.stack.imgur.com/Q5dBX.jpg only thing I'm using only 4 sensors.

"something like this" is enough of a warning to not bother looking at the image. Post an image of YOUR setup as seemingly small details can make a world of difference.

For your code, reduce this to a minimal sketch that still shows the problem. In your case: a sketch that does nothing but read the sensors and display the values. Get that to work first. Then you can also see whether it's really the sensors that are the problem, or maybe some interaction with other parts of the code.

wvmarle: "something like this" is enough of a warning to not bother looking at the image. Post an image of YOUR setup as seemingly small details can make a world of difference.

For your code, reduce this to a minimal sketch that still shows the problem. In your case: a sketch that does nothing but read the sensors and display the values. Get that to work first. Then you can also see whether it's really the sensors that are the problem, or maybe some interaction with other parts of the code.

I think i found the problem. L293D is controlled by digital pins 2,3,...,12 and i have the ultrasonic on pins 3-6 and i think this is the problem. I will try when i get home from work, if having more problems i will come with photos. Thanks!

Using the same pin number twice in all you newping instances also doesn't look correct.

wvmarle: Using the same pin number twice in all you newping instances also doesn't look correct.

Tested it already. You can use trig and echo on same pin with correct measurment.

Do you realise that this means the sensors will be retriggering themselves all the time? No wonder you get unexpected readings!

It's not just because that trig and echo are separate pins.

You CAN get away with connecting all trig pins together, and reading all echoes at the same time. As long as the sensors don't hear each other's signal. Then you need five pins for four sensors.

wvmarle: Do you realise that this means the sensors will be retriggering themselves all the time? No wonder you get unexpected readings!

It's not just because that trig and echo are separate pins.

You CAN get away with connecting all trig pins together, and reading all echoes at the same time. As long as the sensors don't hear each other's signal. Then you need five pins for four sensors.

I don't have sensors to test with. I don't know the sensors the OP is using is the same as those used by the library author. Based on the library used, NewPing library - https://playground.arduino.cc/Code/NewPing/, the write up says the echo and trig pins can be the same pin. I don't know how it works, I didn't write the library and cannot test it; but that is what the library says.

OP says they have the HC-SR04 sensor.

The sensor triggers on a falling edge on the trig pin, regardless of how long trig has been high as long as it is at least 10 us. So that’s exactly what happens when the echo is finished: instant re-trigger of the sensor.

That is, unless the firmware of the sensor ignores the trig pin for a short time in this situation.

It also requires the Arduino’s I/O pin to be set as output to trigger, and as input to read the echo. The echo pin is normally held low, and goes high when a trig comes in. So unless that pin is set to input when the sensor is idle, you create a short the moment you try to trigger it by setting the Arduino pin high.

This thread is now redundant with the NewPing Library thread to which the library author is commenting.

Doesn't matter anymore, thank you guys, finally i sort it out, ive read againd the data sheet for L293D shield and i saw that it uses all digital pins from 3 to 12 so... Now ive managed to get a constant reading at all the time as you can see:

sonar0: 19
sonar1: 4
sonar2: 23
sonar3: 7
sonar0: 19
sonar1: 4
sonar2: 23
sonar3: 7
sonar0: 19
sonar1: 4
sonar2: 23
sonar3: 8
sonar0: 19
sonar1: 4
sonar2: 23
sonar3: 7

it barely gives errors. i`ve connected them on a0, a1, a2, and a3. This thread can be closed. Thank you!