Malfunctioning Ultrasonic distance sensor

I am using a HC-SR04. Whenever I try reading something from the sensor I always get the same value regardless of the actual distance, sometimes random outputs are also received.
This is the code:

int trigPin = 3;    // Trigger
int echoPin = 5 ;   // Echo
long duration;
float cm;
float inches;
 
void setup() {
  //Serial Port begin
  Serial.begin (9600);
  //Define inputs and outputs
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
 
void loop() {
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
 
  // Convert the time into a distance
  cm = (duration/2) / 29.1;     // Divide by 29.1 or multiply by 0.0343
  inches = (duration/2) / 74;   // Divide by 74 or multiply by 0.0135
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(1000);
}

Example Output:

0.00in, 0.62cm
0.00in, 0.62cm
0.00in, 0.62cm
0.00in, 0.38cm
0.00in, 0.24cm
0.00in, 0.10cm
0.00in, 0.00cm
0.00in, 0.00cm
0.00in, 0.07cm

Another snippet of the output:

0.00in, 0.10cm
0.00in, 0.00cm
0.00in, 0.62cm
0.00in, 0.10cm
0.00in, 0.62cm
0.00in, 0.58cm
0.00in, 0.00cm
0.00in, 0.62cm
0.00in, 0.14cm

The circuit:

If you need any other info do tell me.

This usually indicates a wiring issue. Verify trig and echo are on the correct pin.

Another "same value" might be caused by the location of the SR04. Is it resting on a surface? Pointing at the surface? The "cone" of the transmitter is 15 degrees (small, but not a pinpoint).

Good news is, your code works!

What was the actual distance when you got those results?

—-

Try using “2.0” or “74.0” here

2 Likes

Try this mod (compiles but untested):

int trigPin = 3;    // Trigger
int echoPin = 5 ;   // Echo
long duration;
float cm;
float inches;
 
void setup() {
  //Serial Port begin
  Serial.begin (9600);
  //Define inputs and outputs
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
 
void loop() {
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  duration = pulseIn(echoPin, HIGH);
 
  // Convert the time into a distance
  cm = duration * 0.0172;
  inches = duration * 0.00677;
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(1000);
}
1 Like

Even after adjusting no success.
PS : After reconnecting I get way more random values and not very repititive

Actual distance is abt 20 cm
Example output now:

0.25in, 63.57cm
0.03in, 6.87cm
0.00in, 0.00cm
0.04in, 10.31cm
0.25in, 63.57cm
0.02in, 5.15cm
0.00in, 0.00cm
0.03in, 6.87cm
0.03in, 8.59cm
0.25in, 63.57cm
0.25in, 63.57cm
0.01in, 3.44cm
0.16in, 39.52cm

I just realised the previous output had used 0.29 for division for cm.
Here's updated output if divided by 29.1

0.00in, 0.00cm
0.00in, 0.00cm
0.13in, 0.33cm
0.00in, 0.00cm
0.12in, 0.31cm
0.10in, 0.26cm
0.11in, 0.29cm
0.00in, 0.00cm
0.00in, 0.00cm
0.11in, 0.27cm
0.01in, 0.03cm
0.00in, 0.00cm

Tried the code but still similarly bad results.
Also, many times i get 0 as output untill I adjust the sensors

Tried the code but still similarly bad results.
Also, many times i get 0 as output untill I adjust the sensors
Is it possible that I have faulty wires but the circuit is correct :sweat_smile: ?

And how are you adjusting the sensor? There is nothing on the sensor to adjust.

Sorry for the unclear words, I just meant that I physically moved the sensor and(or) wires slightly

Yes that is quite possible.
Try moving the sensor to a different spot on the breadboard.
Connect 5V and ground directly to the sensor, don't use the power rails on the edge of the breadboard

1 Like

Thanks for the suggestion, I wiil do it. I did move it to a diff spot on the breadboard. I also feel that the sensor does not have a tight fit onto the breadboard after i connect the wires

It's a problem with some of the cheap solderless breadboards.
Several people on the forum have mentioned breadboards with bad connections.

1 Like

Remove the SR04 and run the sketch again. If you get similar values, the SR04 is either not powered, not working, not returning data or not being read.

2 Likes

I removed the sensor and I mostly got 0 as the output. But,sometimes(rarely) if I hold down the wires I get extremely weird outputs.
Example:

0.00in, 0.00cm
0.00in, 0.00cm
2182.48in, 5544.85cm
0.00in, 0.00cm
0.00in, 0.00cm
0.00in, 0.00cm
0.00in, 0.00cm
0.00in, 0.00cm
0.00in, 0.00cm
4632.96in, 11770.60cm

Note: I have selected the outputs which show both the anomolies but, there were wayy more 0's and only 2 outliers

It gets old giving someone the answer, and it falls on deaf ears, even when the answer is literally, under their thumb.

2 Likes

I realised the issue, I changed the wires but it didnt work, until:

This did solve the problem.
Thanks a lot. It is working now but occasionally a reading 1000 cm creeps in, but this occurs quite rarely. So , i think I can manage, since my application focuses on objects which are close. The 1000 cm error does not stay it goes away.
Thanks again to everyone who supported me!!!

Glad you got it working
Have a nice day!

1 Like

Consider using a running median (e.g. 5 measurements)
It "automatically" removes outliers.

1 Like