Fail from HC-SRo4 ultrasonic sensor

In my Vacuum Cleaner project I use 6 HC-SR04 ultrasonic sensors, motor shield and Arduino Uno. The sensors usually measure correct distances but sometimes they mesure completely fail. It looks like this in Serial Monitor:

Distance 1: 79
Distance 1: 77
Distance 1: 1336
Distance 1: 78
Distance 1: 78
Distance 1: 3403
Distance 1: 79

It makes that robot behaves weird. Where fail comes from?

In order to reduce impact of this I measure distance from every of 6 sensors three times and calculate average distance of it, but it solves the problem just a little.

Here is how I calculate distances:

for (j = 0; j < 3; j = j + 1) {
  for (i = 0; i < 6; i = i + 1) {
    delayMicroseconds(5);
    digitalWrite(ultrasonic[i], HIGH);  //  triggering of a high pulse on sensor i
    inMode(ultrasonic[i], OUTPUT);     //  pin of sensor i becomes an output
    digitalWrite(ultrasonic[i], LOW);   //  clearing of output i
    delayMicroseconds(10);
    digitalWrite(ultrasonic[i], LOW);   //  turning off a high pulse on sensor i
    pinMode(ultrasonic[i], INPUT);      //  pin of sensor i becomes an input
    imp_back[i][j] = pulseIn(ultrasonic[i], HIGH);  // measuring of a come back time of high pulse to sensor i
    distance[i][j] = imp_back[i][j]*0.0344/2;  //  calculation of a distance to an obstacle in front of sensor i in cm
    delayMicroseconds(10);
  }
}

for (i = 0; i < 6; i = i + 1) {
  avr_distance[i] = (distance[i][0] + distance[i][1] + distance[i][2])/3;
  }

Do you have any ideas where fail comes from and how to eliminate it?

Try NewPing's median method. Median is better than average for outlier rejection.

I think you are pinging too rapidly.
Try slowing down, by increasing the time between pings.

    imp_back[i][j] = pulseIn(ultrasonic[i], HIGH);  // measuring of a come back time of high pulse to sensor i
    distance[i][j] = imp_back[i][j]*0.0344/2;  //  calculation of a distance to an obstacle in front of sensor i in cm

It's silly to keep a 2D array of times, when all you care about is the distance.

You don't even care about the individual distances - just the average distance. So that array is unnecessary, to.

Getting the distance from a PING sensor should be done in a function, NOT as part of loop().

Pass the function the pin number and the number of times to ping, and have it return hte average.

Thank you for all answears. I checked all suggestions.

NewPing's median method. It eliminates almost all error readings. Disadvantage with this is that it take a lot of time to do measurements. For instance I have 6 ultrasonic sensors in my robot and if I measure 5 times for each sensor, takes it 0,7 second. It is to long in my project but good to know that it exists this kind of function.

Slowing down pinging. I tried to slow down and take measurements even every 100 microseconds each, but it doesn't give noticeable results..

I used a function from NewPing library to measure distances. I received less errors.

Most of errors comes when robot measures distance to a mirror when drives on the floor. On a table goes it quite well, but it will be vacuum cleaner so it will work mostly on the floor with all kinds of obstacles. I made a movie earlier about how the robot plays with the ball with 6 ultrasonic sensors: Obstacle Avoiding Robot. Object Following Robot. Arduino Project 2018. Part 2. - YouTube.

Thank you guys for all feedbacks and ideas!

One of the things, I have done, to quiet down the SR04's is to put a .47uF capacitor across the V+ and Gnd pins on the front of the sensors, between the R/T transducers.

I am using 2 sensor pairs. Rt and Rb as one sensor pair and Lt and Lb as another sensor pair. Rt is fired and if it gets a reflection, a non zero value, Rb is fired. Rt and Rb are compared and, for my application, if Rt=Rb then object detected. The same goes for Lt/Lb pair.

I have mounted Rt and Rb with Rb on the bottom and Rt on top of Rb, I found that the top / bottom mounting to gave less noise or false readings.

Hi,
How are you powering your robot?
How are you powering the SR-04 units?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
NOT a FRITZY image!!!!

Thanks.. Tom.. :slight_smile: