HI everyone, i'm using ultrasonic sensor for measuring the distance and i' getting very different output
See this is my code:
const int trigPin = 2;
const int echoPin = 4;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
delay(2000);
Serial.println(distance);
}
And i have connected ultrasonic sensor with arduino in proper manner still my output is like this
Distance: 3186
Distance: 3175
Distance: 3150
Distance: 3151
Distance: 3162
Distance: 3155
Distance: 3172
Distance: 3163
Distance: 3152
Distance: 3156
Distance: 3141
Distance: 3150
Distance: 3169
Distance: 3177
there is no obstacle in front of ultrasonic sensor
what should i do to resolve this ???