ultrasonic sensor max distance behavior min max unstable? hc-sr04

hello, not able to find this issue on google hoping for some help.

not sure why this happens?

if the sensor is pointed to infinity, the read out looks like this

Distance: 4177.36 CM
Distance: 4.23 CM
Distance: 4229.01 CM
Distance: 4.23 CM
Distance: 4172.63 CM
Distance: 4.15 CM

it seems to read the max then the min,
if an object is put in front, the reading is stable.

is this normal or some issue?

I would like to have a stable max reading.. if its over 4XXX CM it stays high not min, max.

thanks

is this normal

That depends on which sensor you have, and how you read it - details that you failed to include.

sorry as linked - hc-sr04

PaulS:
That depends on which sensor you have, and how you read it - details that you failed to include.

That Fritzing diagram is crappy. Most long breadboards, like the one shown in the diagram, have breaks on the power and ground rails, to and bottom, in the middle, so you can construct two independent circuits on one breadboard. There is no reason to use such a large breadboard in the diagram, or in reality, for such a simple connection.

Make sure your sensor is actually getting power, and is grounded.

thanks, that's a bit of topic, but is not the issue. the sensor and code works as I explained.
the issue is its behaviour when nothing is in front of it.

thanks

PaulS:
That Fritzing diagram is crappy. Most long breadboards, like the one shown in the diagram, have breaks on the power and ground rails, to and bottom, in the middle, so you can construct two independent circuits on one breadboard. There is no reason to use such a large breadboard in the diagram, or in reality, for such a simple connection.

Make sure your sensor is actually getting power, and is grounded.

the sensor and code works as I explained.
the issue is its behaviour when nothing is in front of it.

Distance: 4177.36 CM
Distance: 4.23 CM
Distance: 4229.01 CM
Distance: 4.23 CM
Distance: 4172.63 CM
Distance: 4.15 CM

yes,

the issue is the result with nothing in front of the sensor

Distance: 4177.36 CM
Distance: 4.23 CM
Distance: 4229.01 CM
Distance: 4.23 CM
Distance: 4172.63 CM
Distance: 4.15 CM

pulseIn() should return 0 when no echo pulse is produced. If there is nothing in front of the sensor you should get a distance of 0.

Distance: 4177.36 CM
Distance: 4.23 CM

You are obviously NOT using the sketch you pointed to. In that sketch (shown below) 'distance' is an 'int' and you are displaying a 'float'. What sketch are you running?!? I have to suspect a programming error.

/*
[pre]* Ultrasonic Sensor HC-SR04 and Arduino Tutorial
*
* by Dejan Nedelkovski,
* www.HowToMechatronics.com
*
*/

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;

// defines variables
long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

copy and pasting the sketch linked, same thing
I am not sure why it goes from max to min on readings. thanks

// defines pins numbers
const int trigPin = 13;
const int echoPin = 11;

Distance: 4047
Distance: 3
Distance: 4023
Distance: 3
Distance: 4023
Distance: 3
Distance: 4046
Distance: 3
Distance: 4039
Distance: 3

johnwasser:
pulseIn() should return 0 when no echo pulse is produced. If there is nothing in front of the sensor you should get a distance of 0.

Distance: 4177.36 CM
Distance: 4.23 CM

You are obviously NOT using the sketch you pointed to. In that sketch (shown below) 'distance' is an 'int' and you are displaying a 'float'. What sketch are you running?!? I have to suspect a programming error.

/*

[pre]* Ultrasonic Sensor HC-SR04 and Arduino Tutorial
*

*/

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;

// defines variables
long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

I don't know how people manage how to spin-out such a simple thing.

const byte trigPin = 9;
const byte echoPin = 10;

void setup() 
{
  pinMode(trigPin, OUTPUT);
  digitalMode (trigPin, LOW);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
}

void loop() 
{
  digitalWrite(trigPin, HIGH); //trigger the transmit
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);

  Serial.print ("Distance: ");
  Serial.print ((pulseIn(echoPin, HIGH) * 0.034) / 2.0);
  Serial.println ("cm");
  delay (50); // limit the ping rate to no more than 20Hz
}

I set up one of my HC-SR04 sonar units and tried some tests. I could not get your results with the original code.

Unlike earlier tests, the sonar would not produce a zero distance. When the distance went over about 2.1 meters (12,000 microseconds) the pulse length would jump to about 190,000 microseconds (33 meters!). I think they added a timeout in the HC-SR04 firmware.

I put a timeout value of 15,000 on pulseIn() to get zero, rather than 33 meters, whenever there was no echo. This also increased the cycle time since it didn't have to wait 190 milliseconds to find out there was no result.

Adding a 60 millisecond delay after displaying the output produced more stable results, indicating that echos might be interfering with subsequent measurements.

I think your issue might be a fault in your particular HC-SR04. Since they are less than $2 each I would suggest buying a few more from another vendor to see if the problem goes away. Try the 60 millisecond delay first.

I am not sure why it goes from max to min on readings. thanks

// defines pins numbers
const int trigPin = 13;
const int echoPin = 11;

Distance: 4047
Distance: 3
Distance: 4023
Distance: 3
Distance: 4023
Distance: 3
Distance: 4046
Distance: 3
Distance: 4039
Distance: 3

Don't just post a meaningless BS snippet and repeated results - show us the code that produced it.
Show us your schematic.

g43q654wutrjh:
I am not sure why it goes from max to min on readings. thanks

I think you will need an oscilloscope or a logic analyzer to figure out why your particular HC-SR04 is producing the results you are seeing with the setup you have. Mine is not producing such results with the same sketch. I suspect there is something odd in the firmware of your HC-SR04.
Good luck.

THE CODE IS IN THE LINK???? CAN YOU READ? IF YOU DONT HAVE HELP DONT REPLY. SIMPLE

TolpuddleSartre:
Don't just post a meaningless BS snippet and repeated results - show us the code that produced it.
Show us your schematic.

thank you for making me aware it could be a hardware issue, I have a few sensors, they all act the same.
the code works fine if there is an object in front, but when its open it will read min\max it seems a default or something

Distance: 4047
Distance: 3
Distance: 4023
Distance: 3
Distance: 4023
Distance: 3

I will play with the timing to see if they helps or try some other sample code

thanks

johnwasser:
I set up one of my HC-SR04 sonar units and tried some tests. I could not get your results with the original code.

Unlike earlier tests, the sonar would not produce a zero distance. When the distance went over about 2.1 meters (12,000 microseconds) the pulse length would jump to about 190,000 microseconds (33 meters!). I think they added a timeout in the HC-SR04 firmware.

I put a timeout value of 15,000 on pulseIn() to get zero, rather than 33 meters, whenever there was no echo. This also increased the cycle time since it didn't have to wait 190 milliseconds to find out there was no result.

Adding a 60 millisecond delay after displaying the output produced more stable results, indicating that echos might be interfering with subsequent measurements.

I think your issue might be a fault in your particular HC-SR04. Since they are less than $2 each I would suggest buying a few more from another vendor to see if the problem goes away. Try the 60 millisecond delay first.

This also increased the cycle time since it didn't have to wait 190 milliseconds to find out there was no result.

Since there wasn't a need to wait, uselessly, for as long, didn't that result in a decreased cycle time?

digitalMode ?

'digitalMode' was not declared in this scope

TolpuddleSartre:
I don't know how people manage how to spin-out such a simple thing.

const byte trigPin = 9;

const byte echoPin = 10;

void setup()
{
  pinMode(trigPin, OUTPUT);
  digitalMode (trigPin, LOW);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
}

void loop()
{
  digitalWrite(trigPin, HIGH); //trigger the transmit
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

Serial.print ("Distance: ");
  Serial.print ((pulseIn(echoPin, HIGH) * 0.034) / 2.0);
  Serial.println ("cm");
  delay (50); // limit the ping rate to no more than 20Hz
}

digitalMode ?

'digitalMode' was not declared in this scope

That was, obviously, supposed to be digitalWrite().

OH obviously... silly me to mix up MODE and WRITE. thanks for clarifying

and its stilling doing this.

Distance: 3957.92cm
Distance: 4.64cm
Distance: 3962.33cm
Distance: 4.76cm
Distance: 3959.35cm
Distance: 4.76cm
Distance: 3942.23cm
Distance: 4.35cm

PaulS:
That was, obviously, supposed to be digitalWrite().