# Ultrasonic Sensor HC-SR04 wrong distances

**URL:** <https://forum.arduino.cc/t/ultrasonic-sensor-hc-sr04-wrong-distances/627902>\
**Category:** Sensors\
**Created:** [December 26, 2019, 11:12am UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hc-sr04-wrong-distances/627902 "2019-12-26T11:12:36Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![BRUCE\_BLACK](https://avatars.discourse-cdn.com/v4/letter/b/3d9bf3/32.png) [@BRUCE\_BLACK](https://forum.arduino.cc/u/BRUCE_BLACK)\
**Post date:** [December 26, 2019, 11:12am UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hc-sr04-wrong-distances/627902/1 "2019-12-26T11:12:36Z")

</div>

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.

```cpp
#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");
}

```

```cpp
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);
}

```

 ![111.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/c/c/8/cc879e7f19769adfbc5da35b2138284a2582d454.jpeg)

 ![111.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/c/c/8/cc879e7f19769adfbc5da35b2138284a2582d454.jpeg)

 ![222.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/2/2/c/22c359646c061197a6d009d7d34f4ddcb0d80276.jpeg)

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [December 26, 2019, 11:37am UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hc-sr04-wrong-distances/627902/2 "2019-12-26T11:37:24Z")

</div>

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](https://europe1.discourse-cdn.com/arduino/original/4X/0/9/1/0913e11e86a44ea52465af3a37b000df39bbb793.jpeg)

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

![wrong holes.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/0/9/1/0913e11e86a44ea52465af3a37b000df39bbb793.jpeg)

---

<div class="post-metadata">

**Author:** ![wvmarle](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wvmarle/32/1013044_2.png) [@wvmarle](https://forum.arduino.cc/u/wvmarle)\
**Post date:** [December 26, 2019, 12:13pm UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hc-sr04-wrong-distances/627902/3 "2019-12-26T12:13:03Z")

</div>

> 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).

---

<div class="post-metadata">

**Author:** ![BRUCE\_BLACK](https://avatars.discourse-cdn.com/v4/letter/b/3d9bf3/32.png) [@BRUCE\_BLACK](https://forum.arduino.cc/u/BRUCE_BLACK)\
**Post date:** [December 26, 2019, 1:53pm UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hc-sr04-wrong-distances/627902/4 "2019-12-26T13:53:57Z")

</div>

> 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](https://europe1.discourse-cdn.com/arduino/original/4X/0/9/1/0913e11e86a44ea52465af3a37b000df39bbb793.jpeg)
> 
> 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.

 ![222.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/2/2/c/22c359646c061197a6d009d7d34f4ddcb0d80276.jpeg)

---

<div class="post-metadata">

**Author:** ![wvmarle](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wvmarle/32/1013044_2.png) [@wvmarle](https://forum.arduino.cc/u/wvmarle)\
**Post date:** [December 26, 2019, 1:59pm UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hc-sr04-wrong-distances/627902/5 "2019-12-26T13:59:02Z")

</div>

Sounds like a timeout; no pulse received.

Check that you didn't swap trig/echo.

---

<div class="post-metadata">

**Author:** ![BRUCE\_BLACK](https://avatars.discourse-cdn.com/v4/letter/b/3d9bf3/32.png) [@BRUCE\_BLACK](https://forum.arduino.cc/u/BRUCE_BLACK)\
**Post date:** [December 26, 2019, 2:32pm UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hc-sr04-wrong-distances/627902/6 "2019-12-26T14:32:14Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![wvmarle](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wvmarle/32/1013044_2.png) [@wvmarle](https://forum.arduino.cc/u/wvmarle)\
**Post date:** [December 26, 2019, 2:46pm UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hc-sr04-wrong-distances/627902/7 "2019-12-26T14:46:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wildbill](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wildbill/32/1126438_2.png) [@wildbill](https://forum.arduino.cc/u/wildbill)\
**Post date:** [December 26, 2019, 2:48pm UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hc-sr04-wrong-distances/627902/8 "2019-12-26T14:48:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [December 26, 2019, 2:52pm UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hc-sr04-wrong-distances/627902/9 "2019-12-26T14:52:35Z")

</div>

> 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).

 ![broken power rail.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/b/f/5/bf53887fd7406bfdb1b52faead191a63b226d870.jpeg)

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

 ![breadboard.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/0/7/8/078c1bc55ead4a306041b7cbd06553a0033d1442.jpeg)

 ![broken power rail.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/b/f/5/bf53887fd7406bfdb1b52faead191a63b226d870.jpeg)

 ![breadboard.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/0/7/8/078c1bc55ead4a306041b7cbd06553a0033d1442.jpeg)

---

<div class="post-metadata">

**Author:** ![wvmarle](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wvmarle/32/1013044_2.png) [@wvmarle](https://forum.arduino.cc/u/wvmarle)\
**Post date:** [December 26, 2019, 2:58pm UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hc-sr04-wrong-distances/627902/10 "2019-12-26T14:58:52Z")

</div>

> 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 🙂 Missed that part. OP apparently as well, even after I pointed it out 🙂 Irritating "feature" of those breadboards...

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [December 26, 2019, 3:10pm UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hc-sr04-wrong-distances/627902/11 "2019-12-26T15:10:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wildbill](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wildbill/32/1126438_2.png) [@wildbill](https://forum.arduino.cc/u/wildbill)\
**Post date:** [December 26, 2019, 3:15pm UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hc-sr04-wrong-distances/627902/12 "2019-12-26T15:15:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [December 26, 2019, 3:31pm UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hc-sr04-wrong-distances/627902/13 "2019-12-26T15:31:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BRUCE\_BLACK](https://avatars.discourse-cdn.com/v4/letter/b/3d9bf3/32.png) [@BRUCE\_BLACK](https://forum.arduino.cc/u/BRUCE_BLACK)\
**Post date:** [December 26, 2019, 4:16pm UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hc-sr04-wrong-distances/627902/14 "2019-12-26T16:16:32Z")

</div>

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

Learned a lot. Thanks again.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 10:27am UTC](https://forum.arduino.cc/t/ultrasonic-sensor-hc-sr04-wrong-distances/627902/15 "2021-05-06T10:27:54Z")

</div>


