const int trigPin = 5; // GPIO pin connected to Trig pin of HC-SR04
const int echoPin = 18; // GPIO pin connected to Echo pin of HC-SR04
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Send a 10 microsecond pulse to trigger the sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the duration of the echo pulse
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
long distance = duration * 0.034 / 2;
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(1000); // Wait for a second before the next reading
}