Go Down

Topic: Ultrasonic sensor giving odd readings (Read 448 times) previous topic - next topic

Vitesze

Jun 13, 2018, 07:11 pm Last Edit: Jun 26, 2018, 12:02 am by Vitesze
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):
Code: [Select]
       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.





AWOL

#1
Jun 13, 2018, 07:30 pm Last Edit: Jun 13, 2018, 07:40 pm by AWOL
Code: [Select]
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.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

jremington

#2
Jun 13, 2018, 08:27 pm Last Edit: Jun 13, 2018, 08:27 pm by jremington
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.

Vitesze

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.

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.

Vitesze

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.

groundFungus

#6
Jun 13, 2018, 11:24 pm Last Edit: Jun 13, 2018, 11:25 pm by 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.

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.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Vitesze

#8
Jun 14, 2018, 07:40 pm Last Edit: Jun 14, 2018, 07:47 pm by Vitesze
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.

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.

liuzengqiang

https://www.maxbotix.com/Ultrasonic_Sensors/MB1300.htm

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.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

Vitesze

#10
Jun 14, 2018, 08:22 pm Last Edit: Jun 14, 2018, 08:23 pm by Vitesze
https://www.maxbotix.com/Ultrasonic_Sensors/MB1300.htm

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.

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.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

Vitesze

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.

Vitesze

#13
Jun 25, 2018, 11:04 pm Last Edit: Jun 25, 2018, 11:05 pm by Vitesze
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)

Code: [Select]

   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?

TolpuddleSartre

#14
Jun 25, 2018, 11:08 pm Last Edit: Jun 25, 2018, 11:10 pm by 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.

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

Go Up