#define TRIG_PIN 23 // GPIO pin for TRIG #define ECHO_PIN 22 // GPIO pin for ECHO void setup() { Serial.begin(115200); pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); } float measure_distance() { // Send a 10µs pulse to trigger the sensor digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); // Measure the duration of the echo pulse long duration = pulseIn(ECHO_PIN, HIGH); // Calculate the distance in centimeters float distance = duration * 0.0343 / 2; // Speed of sound is 343m/s, divided by 2 return distance; } void loop() { float distance = measure_distance(); Serial.print("Distance: "); Serial.print(distance); Serial.println(" cm"); delay(1000); // Wait for 1 second before taking the next reading }