Ultrasonic sensor giving odd readings

Here is my setup (container lid is normally closed):

The white enclosure holds two JSN-SR04T sensors, which are mounted on the lid.

Sensor height (relative to ground): 140cm
Maximum expected measurement distance (empty container):230cm
Expected measurement distance (picture): 120 to 150cm
Actual readings: 220 to 330cm

Here's my code (for sensor 1 - code for sensor 2 is identical):

       for (i=0; i<59; i++) {
            digitalWrite(TRIG1, LOW);
            delay(2);
            digitalWrite(TRIG1, HIGH);
            delay(10);
            digitalWrite(TRIG1, LOW);
            d1[i] = pulseIn(ECHO1, HIGH);
            d1[i] = d1[i]/58; // convert to centimeters
            } 
        d1len = sizeof(d1) / sizeof(d1[0]);
        qsort(d1, d1len, sizeof(d1[0]), sort_desc);
        fd1 = d1[31];

Note: they do not take measurements at the same time. First it's sensor 1, then sensor 2

Essentially, it takes 59 readings and uses the median measurement value as the distance it reports. The last 6 pings or so, this has been fluctuating around 220 to 275cm, which is far more than it should possibly measure.

When I'm testing things indoor, everything seems fine, so that brings me to the specific testing environment. My assumption is that part of the signal is bouncing off the metal container walls, producing a lot of noise and as the interval time between measurements isn't long enough, give wrong measurements.

My question is, does this assumption sound plausible? What would be the best way to deal with this issue (e.g. how long would all this signal interference last)? The sensor is a bit in a faraway spot and I can't yet update it remotely, so I first would like to see what people think before I spend a lot of time updating the code on it.

digitalWrite(TRIG1, LOW);
            delay(2);
            digitalWrite(TRIG1, HIGH);
            delay(10);
            digitalWrite(TRIG1, LOW);

could be a problem there - those should be microseconds, not milliseconds.

You should not be making more than about twenty readings per second.

For a single sensor, you must wait for all possible echos from a ping to die down, before pinging again.

Two sensors have to alternate, following the same principle.

Thanks for your (and AWOL's response). Good to know that these readings indeed are caused by the echo's - I suppose it's okay indoor (or in an open area), but the walls of the metal enclosure caused the echo's to keep being reflected around and returning to the sensor?

I will adjust the time between pings and try to find the sweet spot.

The surfaces composition and the orientation of surface can have a large effect on the echo "quality", too. If there is no direct path for the echo it can take a few bank shots to get back which will lead to false readings. If there is a hard flat surface normal to the pulse the return will be optimal. Firing a pulse into a jumble of trash, not so much.

groundFungus:
The surfaces composition and the orientation of surface can have a large effect on the echo "quality", too. If there is no direct path for the echo it can take a few bank shots to get back which will lead to false readings. If there is a hard flat surface normal to the pulse the return will be optimal. Firing a pulse into a jumble of trash, not so much.

Actually, there are a lot of professionally-designed waste devices, that make use of ultrasonic sensors (I've seen one using the SRF-01, and another using the MaxBotix MB7137). So ultrasonic sensors aren't by definition "unsuited" for taking measurements in an environment like this.

I did not mean that hey are unsuited, only that one should not expect mm accuracy. And to be aware of potential sources of error.

Echoes normally cause too short distance, as it's the first signal the sensor receives that it reacts to. If a distance is longer than expected this can not be a stray echo from a previous measurement.

Your housing is not pointed at the bottom, but a roughly 45 degree angle - I assume your sensors are pointing in that direction as well, so also not straight down. 1.6 meters divide by cos(45) is 2.2 meters. Looking at the photo it seems it's even pointing down less than that angle, so the distance from the sensor to the nearest surface is even more than that.

Trash may also be absorbing the ultrasound rather than reflecting it, meaning you get the echo from the opposite steel wall.

groundFungus:
I did not mean that hey are unsuited, only that one should not expect mm accuracy. And to be aware of potential sources of error.

I totally agree - I'm hoping for accuracy in the range of 5-10cm, because the waste is unevenly dispersed inside the container anyway. I'm trying to have it measure at a 10-20-30-40-etc.% scale when it comes to determining how full it is.

wvmarle:
Echoes normally cause too short distance, as it's the first signal the sensor receives that it reacts to. If a distance is longer than expected this can not be a stray echo from a previous measurement.

Your housing is not pointed at the bottom, but a roughly 45 degree angle - I assume your sensors are pointing in that direction as well, so also not straight down. 1.6 meters divide by cos(45) is 2.2 meters. Looking at the photo it seems it's even pointing down less than that angle, so the distance from the sensor to the nearest surface is even more than that.

Trash may also be absorbing the ultrasound rather than reflecting it, meaning you get the echo from the opposite steel wall.

Hmm... the angle should be about 37 degrees (so further down than 45 degrees), but looking at the picture you're right...I should look into this again and re-measure it, as it looks a bit further up than 45 degrees in fact. The sensors indeed are linear with the rest of the enclosure.

Yesterday afternoon some extra bags were thrown out, and now in fact I'm finally getting accurate measurements...so I'm guessing in the picture it may be a mixture of taking too many samples at a time + bags absorbing or reflecting it away.

This ultrasonic sensor outputs real-time envelop of received signals. You will see multiple peaks from multiple reflections. Maybe useful to use this as test. Sometimes if the main surface you're detecting (garbage) isn't very reflective, the first strong enough echo from other surfaces gets detected so your results are beyond the expected 2m range.

liuzengqiang:
MB1300 XL-MaxSonar-AE0 – MaxBotix

This ultrasonic sensor outputs real-time envelop of received signals. You will see multiple peaks from multiple reflections. Maybe useful to use this as test. Sometimes if the main surface you're detecting (garbage) isn't very reflective, the first strong enough echo from other surfaces gets detected so your results are beyond the expected 2m range.

I looked at these products as well, but sadly it isn't waterproof (in fact, when I tried using it, it didn't take much to get filth inside the transducer housing). There are some waterproof ones that MaxBotix sells, but they're a bit too costly unfortunately. I want to manufacture more of these sensors in the future, so the price per unit can a real concern.

I'll do some additional testing to see if I can bypass garbage bags not reflecting the signal well - perhaps instead of median value, I can give some preference to closer values.

I was saying that you can use one of the maxbotix sensors with analog envelop output to diagnose your reflection problems, not necessarily using them on your actual prototypes. Also, if your current sensor doesn't have much output power, it may lead to the "I can only hear echos from metal side wall" result since the reflected waves from the garbage would be really small with low sonic output from the sensor.

liuzengqiang:
I was saying that you can use one of the maxbotix sensors with analog envelop output to diagnose your reflection problems, not necessarily using them on your actual prototypes. Also, if your current sensor doesn't have much output power, it may lead to the "I can only hear echos from metal side wall" result since the reflected waves from the garbage would be really small with low sonic output from the sensor.

Good point...I should test this. I feed my sensors 5.0V by the way.

Actually, want to re-open this discussion - after adjusting my code to the following, I still have a lot of issues detecting anything in my container (max. readings should be 230cm - i can get up to 340cm with this)

   for (i=0; i<21; i++) {
        digitalWrite(TRIG1, LOW);
        delayMicroseconds(2);
        digitalWrite(TRIG1, HIGH);
        delayMicroseconds(15);
        digitalWrite(TRIG1, LOW);
        d1[i] = pulseIn(ECHO1, HIGH);
        d1[i] = d1[i]/58;
        delay(50);
        }    
    for (i=0; i<21; i++) {
        digitalWrite(TRIG2, LOW);
        delayMicroseconds(2);
        digitalWrite(TRIG2, HIGH);
        delayMicroseconds(15);
        digitalWrite(TRIG2, LOW);
        d2[i] = pulseIn(ECHO2, HIGH);
        d2[i] = d2[i]/58;
        delay(50);
        }
    d1len = sizeof(d1) / sizeof(d1[0]);
    d2len = sizeof(d2) / sizeof(d2[0]);
    qsort(d1, d1len, sizeof(d1[0]), sort_desc);
    qsort(d2, d2len, sizeof(d2[0]), sort_desc);
    fd1 = d1[10];
    fd2 = d2[10];

I just don't know what I'm doing wrong?? Surely when the container is filled with bags, the ultrasonic sensor should detect something?

I'd slow down the rate of pings to maybe 10Hz or lower - in a hard-sided container, you're going to get a LOT of echoes.

I'd also factor the code to remove duplicated code, and I'd post all the code.

d1len = sizeof(d1) / sizeof(d1[0])it worries me that you didn't use that for the for loops.

TolpuddleSartre:
I'd slow down the rate of pings to maybe 10Hz or lower - in a hard-sided container, you're going to get a LOT of echoes.

I'd also factor the code to remove duplicated code, and I'd post all the code.

Thanks, I will try reducing the cycle further down, see how that goes :slight_smile: Out of curiosity though, if there indeed are many ECHOs, how would this lead to an over-estimation of the distance? Wouldn't it make more sense if the ECHOs reach the transducer before the new ECHO is reflected back (i.e. under-estimation of the distance)? Or would this possibly be an issue with ECHOs coming back to the transducer before it has time to switch modes (since this sensor doesn't have a separate transmitter and receiver).

What kind of duplicate code are you referring to? I have two seperate JSN-SR04T units, so each take their own individual measurements. Is there any way I could shorten this code?

TolpuddleSartre:
it worries me that you didn't use that for the for loops.

I didn't expect it to make a difference, whether I would put it inside or outside the for-loop?

I changed the frequency to 5Hz only - now it atually went down from 300cm to 170cm on one sensor, and from 227cm to 180cm on the other sensor.

It still seems quite a bit of an over-estimation, as right now I would expect between 95 and 105cm . I will need to test some new things out tomorrow and see how exactly the parameters are affecting the results. The one positive thing I see now though is that the data from both sensors seems to match up more (10cm difference - compared to 73cm difference before)

Why do you even check that often? Once a minute will be enough. Bags don't come and go that frequently.