Distance US3 sensor

Hi GHI support,

I got a Distance US3 sensor module with FEZ spider mainboard (firmware 4.2.11.1) a few weeks ago from Mouser. After deployment, the returned distance seems to be incorrect as its value returns between 14-17 in range even if I move the sensor in and out. What wrong and could you please help me finding out the issue?

Here is a code

distance_US3.AcceptableErrorRate = 2;
int distanceCm = distance_US3.GetDistanceInCentimeters();
string message = string.Empty;
if (distanceCm == distance_US3.SENSOR_ERROR)
{
message = “Sensor errror”;
}
else
{
message = "Distance = " + distanceCm;
}

Display(message);

Thanks
Phisarn K.

How far out are you moving the sensor? Movement closer than a few centimeters, sometimes 10-15cm, can give incorrect readings.

I’ve moved around in any direction to measure distance but the sensor did provide incorrect result, Here are the test scenarios

case actual value returned value

sensor to so close object 5 cm 14 cm
sensor to cabinet 50 cm 17 cm
sensor to ceiling 150-200 cm 17 cm
sensor to floor 100 cm 17 cm.

the returned result varied between 14-17 cm

I’ve moved around in any direction to measure distance but the sensor did provide incorrect result, Here are the test scenarios

case | actual value | returned value

1.sensor to so close object | 5 cm | 14 cm
2.sensor to cabinet | 50 cm | 17 cm
3.sensor to ceiling | 150-200 cm | 17 cm
4.sensor to floor | 100 cm | 17 cm

the returned result varied between 14-17 cm

Do you have any other mainboards or modules you can test with?

I have only one main board but regarding other modules all I have such as PIR motion detection, color sense etc. all of them seems to be ok.

No other DistanceUS3 modules? Can you try to use a different cable and socket?

No other distance sensor. I’ve changed other cable and socket. The result is still the same.

what’s wrong with the sensor? Is it not the normal operation?

@ Phisarn I have a number of these modules so I’ll setup some tests with them tomorrow and post my results. I was thinking of using them for a snow pack experiment, but they only seem to have 1 cm resolution and I was needing 1 mm resolution, but I’m sure I’ll find other uses for them, but for now I’ll setup a test and see how they do.

How many measurements are you telling GetDistanceInCentimeters to take? Does it ever return SENSOR_ERROR (-1)? Could you post a minimal example that reproduces the error?

Here is the code I’m using for my test.



using System;
using System.IO;
using Gadgeteer.Modules.GHIElectronics;
using Microsoft.SPOT;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;

namespace DistanceTest2
{
    public partial class Program
    {
        private readonly Font _font = Resources.GetFont(Resources.FontResources.small);
        private string _distance;

        // This method is run when the mainboard is powered up or reset.   
        private void ProgramStarted()
        {
            Debug.Print("Program Started");

            capTouch.ButtonPressed += capTouch_ButtonPressed;
            button.ButtonPressed += button_ButtonPressed;
        }

        private void button_ButtonPressed(Button sender, Button.ButtonState state)
        {
            double distance = 0;

            Mainboard.SetDebugLED(true);

            if (_distance.Length > 0)
            {
                distance = Convert.ToDouble(_distance);
            }

            oledDisplay.SimpleGraphics.Clear();
            oledDisplay.SimpleGraphics.DisplayText(distance.ToString(), _font, GT.Color.White, 0, 0);

            int distance1 = distance_US3.GetDistanceInCentimeters(10);
            oledDisplay.SimpleGraphics.DisplayText("1. " + distance1, _font, GT.Color.Yellow, 0, 15);

            int distance2 = distance_US32.GetDistanceInCentimeters(10);
            oledDisplay.SimpleGraphics.DisplayText("2. " + distance2, _font, GT.Color.Orange, 0, 30);

            int distance3 = distance_US33.GetDistanceInCentimeters(10);
            oledDisplay.SimpleGraphics.DisplayText("3. " + distance3, _font, GT.Color.Red, 0, 45);

            if (sdCard.IsCardMounted)

            {
                try
                {
                    FileStream fs = sdCard.GetStorageDevice().OpenWrite("Data.txt");
                    var sw = new StreamWriter(fs);
                    sw.WriteLine(distance1 + ";" + distance1 + ";" + distance2 + ";" + distance3);
                    sw.Flush();
                    sw.Close();
                    fs.Close();
                }
                catch (Exception ex)
                {
                    Debug.Print("Message: " + ex.Message + "  Inner Exception: " + ex.InnerException);
                }
            }

            Mainboard.SetDebugLED(false);
        }

        private void capTouch_ButtonPressed(Gadgeteer.Modules.GMod.CapTouch sender, int button,
            Gadgeteer.Modules.GMod.CapTouch.ButtonState state)
        {
            double distance = 0;

            switch (button)
            {
                case 11:
                    _distance = string.Empty;
                    break;
                case 9:
                    _distance = _distance + ".";
                    break;
                case 10:
                    _distance = _distance + "0";
                    break;
                default:
                    _distance = _distance + (button + 1);
                    break;
            }

            if (_distance.Length > 0)
            {
                distance = Convert.ToDouble(_distance);
            }

            oledDisplay.SimpleGraphics.Clear();
            oledDisplay.SimpleGraphics.DisplayText(distance.ToString(), _font, GT.Color.White, 0, 0);
        }
    }
}


My initial tests are making me want to do more tests as the results weren’t as good as I hoped as anything under 9" was reporting as 14 cm and I thought the sensor was 2cm to 400cm in range, so more testing later.

Do you guy have any progress or any hint how to use the sensor?

Hello,
i have a problem like that.
Sensor Distance US 3 connected to Cerbot 1.3 return always distance between 4 to 50 cm.
When i connect the same sensor to Spider 1.0, sensor return distance up to 50 cm (200 cm for ex).

Code is the same with 2 projects :

    void ProgramStarted()
    {
        new Thread(CheckDistance).Start();
    }

    public void CheckDistance()
    {
        int j;
        distance_US3.AcceptableErrorRate = 2;
        while (true)
        {
            j = distance_US3.GetDistanceInCentimeters(5);
            Thread.Sleep(100);
            if (j == distance_US3.SENSOR_ERROR)
            {
                Debug.Print("Sensor errror");
            }
            else
            {
                Debug.Print("D=" + j);
            }
        }
    }

Why i can’t have distance up to 50 cm with sensor connected to Cerbot ?

Thank you.