Problem with ioe-sr05 ultrasonic sensor

Hi.

I have an ioe-sr05 ultrasonic sensor.
When I place an object in front of it, the sensor correctly measures its distance.
But when there is no object in front of the sensor, the distance is 160mm.
How can i solve this problem?

Thank you
code:

#define DistanceEn_Pin 2

int i = 0;
long unsigned distance = 0;
uint8_t distanceValue[4] = {0,0,0,0};
void setup()
{
Serial.begin(9600);
pinMode(DistanceEn_Pin,OUTPUT);
digitalWrite(DistanceEn_Pin,1);
}

void Distance()
{
while( Serial.read() >= 0 ); //Empty the serial buffer
digitalWrite(DistanceEn_Pin,0); //Ultrasound can open
while(Serial.available() <= 4) //Waiting for the ultrasonic data window
{
}
distanceValue[0] = Serial.read(); //Extract the data
if( distanceValue[0] == 0xff ) //Determine if data for ultrasonic module
{
for(i = 1;i <= 3;i ++)
{
distanceValue[i] = Serial.read();
}
}

digitalWrite(DistanceEn_Pin,1); //Ultrasound can make

distance = distanceValue[1] * 256 + distanceValue[2]; //Data processing, calculating distance
if((distanceValue[3] == distanceValue[1] + distanceValue[2] - 1)) //Data and check, can be removed
{
Serial.print("The distance is : ");
Serial.print(distance);
Serial.println(" mm");
}
}

void loop()
{
Distance();
}

Are you sure there is nothing in front, no bench surface. ensure it is free space/air in front.

Yes,I'm sure.there is nothing in front of it.

I tried one of these US sensors with example code (sorry I no longer have a link) and it worked first time.

Perhaps try example code to provide evidence of your hardware working OK.

This code does not work correctly for me.I still have that problem:


#define DistanceEn_Pin 2
 
int i = 0;
long unsigned distance = 0;
uint8_t distanceValue[4] = {0,0,0,0};
void setup() 
{
  Serial.begin(9600);
  pinMode(DistanceEn_Pin,OUTPUT);
  digitalWrite(DistanceEn_Pin,1);
}
 
void Distance()
{
  while( Serial.read() >= 0 );         //Empty the serial buffer
  digitalWrite(DistanceEn_Pin,0);      //Ultrasound can open
  while(Serial.available() <= 4)       //Waiting for the ultrasonic data window
  { 
  }
  distanceValue[0] = Serial.read();    //Extract the data
  if( distanceValue[0] == 0xff )       //Determine if data for ultrasonic module
  {
    for(i = 1;i <= 3;i ++)
    {
      distanceValue[i] = Serial.read();
    }
   }
 
  digitalWrite(DistanceEn_Pin,1);       //Ultrasound can make
 
  distance = distanceValue[1] * 256 + distanceValue[2];                   //Data processing, calculating distance
  if((distanceValue[3] == distanceValue[1] + distanceValue[2] - 1))       //Data and check, can be removed
  {
    Serial.print("The distance is : ");
    Serial.print(distance);
    Serial.println(" mm");
  }
}
 
void loop() 
{
  Distance();
}

Do you have another code?
Or do you have a way to solve my problem?
Thank you

I had no trouble with this as test code (not my code, just copied), this was for HC-SR04 US module not ...05. I do not (they seem very similar (Arduino ultrasonic sensor (HC-SR04 or HY-SRF05) | Jontas):

/* Example code for HC-SR04 ultrasonic distance sensor with Arduino. No library required. More info: https://www.makerguides.com */

// Define Trig and Echo pin:
#define trigPin 2
#define echoPin 3

// Define variables:
long duration;
int distance;

void setup() {
  // Define inputs and outputs:
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  //Begin Serial communication at a baudrate of 9600:
  Serial.begin(9600);
}

void loop() {
  // Clear the trigPin by setting it LOW:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);

  // Trigger the sensor by setting the trigPin high for 10 microseconds:
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
  duration = pulseIn(echoPin, HIGH);
  // Calculate the distance:
  distance = duration * 0.034 / 2;

  // Print the distance on the Serial Monitor (Ctrl+Shift+M):
  Serial.print("Distance = ");
  Serial.print(distance);
  Serial.println(" cm");

  delay(500);
}

Thank you but my sensor is ioe-sr05.
no HC-sr05.

Is this what you are seeing:
"5. AAAA automatically returns over-range data to reduce the wait time after over-range;".

Sure seems like it.

Apologies,

This is what is displayed on the serial monitor when there is nothing in front of the sensor:

The numbers are between 14 to 21.

No problem

And what is the maximum distance the sensor will read?

200 cm

And could you explain these part of code please?

uint8_t distanceValue[4] = {0,0,0,0};

and

while( Serial.read() >= 0 );         //Empty the serial buffer
  digitalWrite(DistanceEn_Pin,0);      //Ultrasound can open
  while(Serial.available() <= 4)       //Waiting for the ultrasonic data window
  { 
  }
  distanceValue[0] = Serial.read();    //Extract the data
  if( distanceValue[0] == 0xff )       //Determine if data for ultrasonic module
  {
    for(i = 1;i <= 3;i ++)
    {
      distanceValue[i] = Serial.read();
    }
   }

Thanks a lot

Then any returned value giving a result greater than 200cm means there is no reflection, and ignore the value.

That is where the serial data message is stored and parsed to give you the return value.

I know but when there is nothing in front of the sensor,it returns 14 to 21mm.
This is my problem.

That list does not show 14 to 21. It shows a bunch of three digit cm numbers.

What code should I add so that an LED turns on when there is an object in front of the sensor, and it turns off when there is no objects in front of the sensor?

Thank you

When the cm value is less then 201 and greater than some minimum, perhaps 5cm, you have an object, so turn the LED on, else turn it off.