Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EDISON-SCIENCE-CORNER/eac548292edd6d28860842aef32858d2 to your computer and use it in GitHub Desktop.
Save EDISON-SCIENCE-CORNER/eac548292edd6d28860842aef32858d2 to your computer and use it in GitHub Desktop.
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // trigPin as Output
pinMode(echoPin, INPUT); // echoPin as Input
Serial.begin(9600); // serial communication
}
void loop() {
digitalWrite(trigPin, LOW);// Clears the trigPin
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);// Sets the trigPin on HIGH state for 10 micro seconds
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);// Reads the echoPin,returns the sound wave travel time in microseconds
distance= duration*0.034/2;// Calculating the distance
Serial.print("Distance: ");//Serial Monitor
Serial.println(distance);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment