Hi all,
I need a big help. I am new to Arduino and trying with a Ultrasonic sensor for my school project.
I used below code and did the wiring correctly, but it give Distance 0 all the time, then I bought a new sensor after tried so many with my existing sensor, but same issue is there with the new sensor too. And when tested from a shop even my old sensor works there.
Can't identify what is wrong with the below code or is there anything to do with IDE version? I have no clue how to fix.
I tried to find in the other posts too...But still struggling.
const int trigPin = 9;
const int echoPin = 10;
long duration;
long distance;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH, 1);
distance = duration*0.034/2;
Serial.print("Distance : ");
Serial.print(distance);
Serial.println(" cm ");
delay(500);
}
Much appreciate your help on this.