Why Duration is 0?

I want it to measure distance but i can't because duration is 0 Please Help me(Board: Arduino MKR 1010 Wifi with MKR Carrier Explore IOT Kit)(Use on IOT Cloud)

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

long duration;
int 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 * 0.034 / 2;
  Serial.print("Duration: ");
  Serial.println(duration);
  Serial.print("Distance: ");
  Serial.println(distance);
  delay(100);
}


it is 5V device on 3.3V MCU;
maybe wrong connection;
maybe try this

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

long duration;
int distance;
void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
}
void loop() {
 
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin, LOW);
 
  duration = pulseIn(echoPin, HIGH);
  distance = (duration * 17)/1000;
  Serial.print("Duration: ");
  Serial.println(duration);
  Serial.print("Distance: ");
  Serial.println(distance);
  delay(100);
}

It pin on 5V pin already

Please READ the documentation on the pulseLn() function call. It always returns ZERO when the time-out occurs! Since you did not specify a time-out value, one second is used by default.

if distance larger as 163m

No, if NO echo return is detected.

are you mean it was no trigger signal at all? if it was there it should return because it is indoor.

Did you read the pulseLn() documentation to see why zero is returned? It is returned because the pulse has not ended by the time the time-out period has occurred. That is caused by the device not detecting an echo return for ANY reason!

this is correct, but which reason it might be?
1 distance was too large for this particular setting of timeout, default 1000000µs is travel of 343m;
2 it was no trigger signal at all, for any reason;
3 trigger signal is too weak and/or absorbed on obstacles.
do i forget something?

i try this code but duration still 0 ;-;

do you have Uno, Nano, Micro, Leonardo, Mega?

i have esp8266 and esp32 lol but i can borrow my friend(uno)

try on yours and on Uno.

okie thanks

Yes, you did. The object MUST be able to reflect the sound pulse back to the sensor. Using a pencil or a blanket, or your hand or any other soft material could be the reason. Or the reflection is NOT directly back to the sensor.

this is normal end of pulseIn(), no timeout occurs

variant 3.

well, nothing forgot.

btw it is so simple to check if it timeout issue

duration = pulseIn(echoPin, HIGH, 5000000);

@inotplayingrn would you please test it?

okie

actually you have a wiring problem, your sensor is not powered

you need to add short wires to continue the blue and red rails across