// Define the trigger and echo pins
int trigPin = 9;
int echoPin = 10;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Define pin modes
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Clear the trigger pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigger pin high for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echo pin and calculate the duration
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance in cm
int distance = duration * 0.03428 / 2;
// Print the distance to the Serial Plotter
Serial.println(distance);
// Wait before next loop
delay(100);
}