ultrasonic sensor always showing 0 value

ultra sonic sensor always showing 0.

can anyone help me to correct this

#define trigPin 13
#define echoPin 12

void setup() 
{
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
   pinMode(11, OUTPUT);
  
}
void loop() 
{
 long duration, distance;
 digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  Serial.print("distance is ");
   Serial.print(distance);
    Serial.println(" cm");
  delay(1000);

 

}

Post a drawing or photo of how you have wired your sensor up. Is it an SR04 type sensor?

davidrh:
Post a drawing or photo of how you have wired your sensor up. Is it an SR04 type sensor?

sro4

trigger pin to 13
echo pin to 12

vcc to 5v (3.3v also showing 0)

raj412:
sro4

trigger pin to 13
echo pin to 12

vcc to 5v (3.3v also showing 0)

Welcome to the forum.
What about the gnd terminal of the sensor, and you need to post a circuit diagram, not a description.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Can you tell us your electronics, programming, Arduino, hardware experience?
Thanks.. Tom... :slight_smile:

i followed the same connections as shown in below attached figure

HC-SR04.jpg