# Ultrasonic sensor reading only zeros

**URL:** <https://forum.arduino.cc/t/ultrasonic-sensor-reading-only-zeros/1377342>\
**Category:** General Guidance\
**Created:** [April 28, 2025, 2:43pm UTC](https://forum.arduino.cc/t/ultrasonic-sensor-reading-only-zeros/1377342 "2025-04-28T14:43:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![username360](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/username360/32/1126393_2.png) [@username360](https://forum.arduino.cc/u/username360)\
**Post date:** [April 28, 2025, 2:43pm UTC](https://forum.arduino.cc/t/ultrasonic-sensor-reading-only-zeros/1377342/1 "2025-04-28T14:43:20Z")

</div>

Using this code to get a buzzer to go off when an ultrasonic sensor reads under a certain distance. It was working for a while but then it just started reading zeros, tried switching out the sensor with a couple different ones and the issue persisted. Is there something wrong with the code?

```arduino
int trigPin = 9;
int echoPin = 10;
int buzzer = 7;

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

void loop() {
  long duration, distance;

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;

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

  if (distance <= 10) {
    digitalWrite(buzzer, HIGH);
  } else {
    digitalWrite(buzzer, LOW);
  }

  delay(10);
}

```

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [April 28, 2025, 2:49pm UTC](https://forum.arduino.cc/t/ultrasonic-sensor-reading-only-zeros/1377342/2 "2025-04-28T14:49:38Z")

</div>

Possibly, the buzzer is overloading the port pin, which can destroy the Arduino.

Please read the "How to get the best out of this forum" post and follow the instructions to provide the details.

---

<div class="post-metadata">

**Author:** ![docdoc](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/docdoc/32/220907_2.png) [@docdoc](https://forum.arduino.cc/u/docdoc)\
**Post date:** [April 28, 2025, 2:57pm UTC](https://forum.arduino.cc/t/ultrasonic-sensor-reading-only-zeros/1377342/3 "2025-04-28T14:57:05Z")

</div>

> [@username360](#):
>
> It was working for a while but then it just started reading zero

Is it an HC-SR04? If so, I had ever got problems with them, 3 out of 4 were faulty, giving almost exactly the behaviour you described: they started returning 0 distance after a while no obstacle has been detected (aka: timeout). The only real solution was to power the sensor off and on to get it back working, but I found a workaround, doing this when I read pulseDuration equals to zero:

```arduino
    if (pulseDuration == 0 ) {
      // SR04 unlock
      pinMode(EchoPin, OUTPUT);
      digitalWrite(EchoPin, LOW);
      delay(50);
      pinMode(EchoPin, INPUT);

```

But at the end I switched to the more reliable HY-SRF05: they cost a few cents more but I have never ever had problems with them!

---

<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:** [October 25, 2025, 2:58pm UTC](https://forum.arduino.cc/t/ultrasonic-sensor-reading-only-zeros/1377342/4 "2025-10-25T14:58:01Z")

</div>

Closed on October 25, 2025, 2:58pm UTC

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