#define TRIGGER_PIN 14 // GPIO pin connected to the sensor's trigger
#define ECHO_PIN 12 // GPIO pin connected to the sensor's echo
void setup() {
Serial.begin(115200);
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
long duration, distance;
// Trigger the sensor to send a pulse
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
// Read the duration of the echo pulse
duration = pulseIn(ECHO_PIN, HIGH);
// Calculate the distance in centimeters (you can adjust the formula if you want inches)
distance = (duration / 2) / 29.1;
// Print the distance to the serial monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(1000); // You can adjust the delay for how often you want to take a reading
}