#define TRIG_PIN 22
#define ECHO_PIN 23
void setup() {
Serial.begin(9600); // Start serial communication
pinMode(TRIG_PIN, OUTPUT); // Set Trig pin as an output
pinMode(ECHO_PIN, INPUT); // Set Echo pin as an input
}
void loop() {
long duration, distance;
// Clear the TRIG pin by setting it LOW
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
// Set the TRIG pin HIGH for 10 microseconds to initiate the pulse
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the Echo pin and calculate the duration of the pulse
duration = pulseIn(ECHO_PIN, HIGH);
// Calculate the distance in centimeters
distance = (duration / 2) / 29.1;
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(1000); // Wait for 0.5 seconds before next reading
}