Ultrasonic Sensor HC-SR04 wrong distances

I'm a beginner and tried to do something with distance sensor but it keeps return wired values.

160cm
162cm
163cm
164cm
164cm
166cm
165cm
165cm
167cm
166cm
168cm
167cm
169cm
172cm

These are what I get. How can I know this sensor is broke or not?

I've done two different source code. One I used from tutorials and one from NewPing library just in case I did something wrong. But the result are same.

#include <NewPing.h>

const int TRIG = 8;
const int ECHO = 9;

NewPing sonar(TRIG, ECHO, 200);

void setup() {
  Serial.begin(9600);
}

void loop() {

  Serial.print(sonar.ping_cm());
  Serial.println("cm");
}
void setup() {
  Serial.begin(9600);

  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
}

void loop() {
  // init
  digitalWrite(TRIG, LOW);
  digitalWrite(ECHO, LOW);
  delayMicroseconds(2);

  // trig for 10 us
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);

  long duration = pulseIn(ECHO, HIGH);
  float distance = duration / 29 / 2.0;

  Serial.print(distance);
  Serial.println("cm");

  delay(20);
}

You have the trigger and echo wires in the wrong side of the breadboard. As they are, they are connected to nothing. Move them to the same side of the center of the breadboard as the sensor.

wrong holes.jpg

And Karma for a well done first post. Most new members do not bother to read the guidelines.

wrong holes.jpg

BRUCE_BLACK:
I'm a beginner and tried to do something with distance sensor but it keeps return wired values.

160cm
162cm
163cm
[...]

You also forgot to mention what values you expect, as this are perfectly valid readings for a distance sensor that's rated to do a distance of 2-400cm.

Indeed it seems to be a simple wiring error this time.

And to pre-empt your next wiring question: note how the blue and red lines are interrupted. That means in your breadboard the power rails are also interrupted (whether that is the case differs per breadboard, yours does have this interruption).

groundFungus:
You have the trigger and echo wires in the wrong side of the breadboard. As they are, they are connected to nothing. Move them to the same side of the center of the breadboard as the sensor.

wrong holes.jpg

And Karma for a well done first post. Most new members do not bother to read the guidelines.

What am I doing.. Embarrassed. Thank you so much.

But still not working. I really can not figure why.

Now it returns zero with tutorial code. It returns not zero (167cm...) with NewPing library code.

167cm is not I expected. I want about 2cm ~ 30cm.

Any idea would be appreciated.

Sounds like a timeout; no pulse received.

Check that you didn't swap trig/echo.

wvmarle:
Sounds like a timeout; no pulse received.

Check that you didn't swap trig/echo.

Thank you. I tried to swap trig/echo but no work.

You should connect them as per your code: trig to pin 8, echo to pin 9.

If it still doesn't work, time to get your multimeter out: check that you have 5V power on the HC-SR04's pins (measure at the board). Also check whether the connections between the trig and echo pins (on the board) and pin 8 resp pin 9 of the Arduino (at the Arduino) is actually there. Breadboards are notorious for poor connections.

Reread reply #2. As wvmarle said, your breadboard has power and ground interrupted in the middle. You have connected power and ground to the sensor to the wrong side in your latest photo.

And to pre-empt your next wiring question: note how the blue and red lines are interrupted. That means in your breadboard the power rails are also interrupted (whether that is the case differs per breadboard, yours does have this interruption).

Move the power wires from the Uno to the opposite end of the breadboard or connect the rails, in the middle, as shown below.

groundFungus:
Move the power wires from the Uno to the opposite end of the breadboard or connect the rails, in the middle, as shown below.

Mmm... I should have looked better at the images :slight_smile: Missed that part. OP apparently as well, even after I pointed it out :slight_smile: Irritating "feature" of those breadboards...

That "feature" about drove me nuts when first encountered.

I think that you can only see those lines after you have made that mistake at least once. Up until that point, they're just decoration

I have gotten to just putting in the jumpers right when I get a new breadboard, broken lines or not (note second photo).

Thank you guys. It works now. After connect 5v/gnd directly to chipset 5v/gnd row.

Learned a lot. Thanks again.