const int trigPin = 12; // Trigger Pin of Ultrasonic Sensor const int echoPin = 11;// Echo Pin of Ultrasonic Sensor // Define variables: long duration; int distance; void setup() { //Serial Port begin Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { // Clear the trigPin by setting it LOW: digitalWrite(trigPin, LOW); delayMicroseconds(5); // Trigger the sensor by setting the trigPin high for 10 microseconds: digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds: duration = pulseIn(echoPin, HIGH); // Calculate the distance: distance= duration*0.034/2; // Print the distance on the Serial Monitor (Ctrl+Shift+M): Serial.print("Distance = "); Serial.print(distance); Serial.println(" cm"); delay(50); }