#define trig 10
#define echo 9
long duration;
double distance;
void setup() {
// triger is used to take input for sensor and output for aurdrino as it triggers the sensor to work
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trig, LOW); //we will do this to eliminate noise in sensor
delayMicroseconds(2);
digitalWrite(trig, HIGH); //Our sensor will be active
delayMicroseconds(1000); // for 10 microseconds
digitalWrite(trig, LOW); // now sensor will turn off
duration = pulseIn(echo, HIGH); //This will store the time period for which pulse is high in microseconds
distance = duration*0.034/2;
Serial.print("Distance: ");
Serial.println(distance);
}