HC-SR04 always return ZERO with Arduino UNO

I use Arduino UNO, HC-SR04 module and use the code from Arduino official Website to measure distance. But on serial monitor I always get "Distance = 0". I also review previous posts on this forum, but none of them give a clear idea. So I post again this problem.

/*
 * HC-SR04 example sketch
 *
 * https://create.arduino.cc/projecthub/Isaac100/getting-started-with-the-hc-sr04-ultrasonic-sensor-036380
 *
 * by Isaac100
 */

const int trigPin = 3;
const int echoPin = 2;

float duration, distance;

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

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration*.0343)/2;
  Serial.print("Distance: ");
  Serial.println(distance);
  Serial.println(duration);
  delay(100);
}

Please anyone help me with proper way out, instead of referering to some invalid link.
Thanks in advance.

Where and when did that happen? If there are relevant posts already on this topic, it's a waste of time for people to start all over from scratch.

Also please, more details. Did you write the sketch? If not, did you modify it? If so, how? What board do you have? What wiring do you have?

A result of zero usually means that no pulse was seen on the echo pin. Are you sure it is wired correctly?

Code works fine with my Uno and HCSR-04. Are you sure that the rangefinder is wired right (pin 3 to trigger, Pin 2 to echo) and the wires are good (check them with a DMM)?

Yeah, I also surprised that why in my case it is not working. I am very sad because of this.

Yeah, I also surprised that why in my case it is not working. Because it is so easy code and the project is just begining level /

Hi,
You are trying make objects approach the SR04, not just running the code with nothing to detect?

Can you please post an image of your froject, so we can see your component layout?

Tom... :smiley: :+1: :coffee: :australia:

@johnwasser @groundFungus I am surprised to see the knowledge of both of you about electronics. Unbelievable. You both were right. One of the connectors is defective. Thanks a lot, to both of you. Now my project works fine. Thanks again.

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