# Not sure whether HC-SR04 is broken

**URL:** <https://forum.arduino.cc/t/not-sure-whether-hc-sr04-is-broken/1213848>\
**Category:** Sensors\
**Created:** [January 21, 2024, 5:49am UTC](https://forum.arduino.cc/t/not-sure-whether-hc-sr04-is-broken/1213848 "2024-01-21T05:49:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![low-zi-hong](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/low-zi-hong/32/1036164_2.png) [@low-zi-hong](https://forum.arduino.cc/u/low-zi-hong)\
**Post date:** [January 21, 2024, 5:49am UTC](https://forum.arduino.cc/t/not-sure-whether-hc-sr04-is-broken/1213848/1 "2024-01-21T05:49:54Z")

</div>

Recently, I bought a HC-SR04 ultrasonic sensor nad plug it into an Arduino UNO board.

```arduino
#define trigPin 9
#define echoPin 8
#define ledPin 13

long duration;
int distanceThreshold = 20; // Adjust this threshold according to your requirements

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

void loop() {
  // Trigger the ultrasonic sensor
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the echo duration
  duration = pulseIn(echoPin, HIGH);

  // Calculate the distance
  int distance = duration * 0.034 / 2;

  // Print distance to Serial Monitor (optional)
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // Check if the object is within the specified range
  if (distance < distanceThreshold) {
    // Light up the LED (representing the bulb)
    digitalWrite(ledPin, HIGH);
  } else {
    // Turn off the LED if no object is detected or it's outside the range
    digitalWrite(ledPin, LOW);
  }

  // Delay before the next reading
  delay(500);
}

```

Here are the code I upload, but it it keep outputting 0 and 12 and 11.

```arduino
13:46:55.487 -> Distance: 0 cm
13:46:55.990 -> Distance: 0 cm
13:46:56.474 -> Distance: 0 cm
13:46:56.978 -> Distance: 12 cm
13:46:57.486 -> Distance: 12 cm
13:46:57.954 -> Distance: 11 cm
13:46:58.501 -> Distance: 0 cm
13:46:59.005 -> Distance: 0 cm
13:46:59.501 -> Distance: 11 cm
13:46:59.978 -> Distance: 0 cm

```

Eventough it is facing the celling still it give output of 12 n 11 n 0.

 ![WhatsApp Image 2024-01-21 at 13.48.56_3dfee759](https://europe1.discourse-cdn.com/arduino/original/4X/1/2/e/12e5f35288d0237fd47d879961194e8d92b1dc5d.jpeg)

anyone could help me ☹ Thanks very much.

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [January 21, 2024, 6:01am UTC](https://forum.arduino.cc/t/not-sure-whether-hc-sr04-is-broken/1213848/2 "2024-01-21T06:01:08Z")

</div>

- Confirm your yellow and brown pin connections on the Arduino.

- The LED should have a 220 ohm series resistor connected to pin 13.

---

<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:** [January 21, 2024, 4:34pm UTC](https://forum.arduino.cc/t/not-sure-whether-hc-sr04-is-broken/1213848/3 "2024-01-21T16:34:17Z")

</div>

> [@low-zi-hong](#):
>
> `int distance = duration * 0.034 / 2;`

This may be a problem, as `duration` is also an integer. Not sure how the compiler is handling this. In just about all code I've seen `distance` is also a float. Try: `int distance = float(duration) * 0.034 / 2;`

Check whether your cables are not broken. Those cheap Chinese-made cables are highly unreliable, in my experience.

---

<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:** [July 19, 2024, 4:34pm UTC](https://forum.arduino.cc/t/not-sure-whether-hc-sr04-is-broken/1213848/4 "2024-07-19T16:34:59Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
