const int trigPin = D4; // Define the trigger pin (D4, GPIO 4) const int echoPin = D5; // Define the echo pin (D5, GPIO 5) void setup() { Serial.begin(9600); // Initialize serial communication pinMode(trigPin, OUTPUT); // Set the trigger pin as an output pinMode(echoPin, INPUT); // Set the echo pin as an input } void loop() { long duration, distance_cm; // Send a short pulse to trigger the sensor digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Measure the duration of the pulse on the echo pin duration = pulseIn(echoPin, HIGH); // Calculate distance from the pulse duration (in cm) distance_cm = duration / 58.0; // Print the distance to the serial monitor Serial.print("Distance: "); Serial.print(distance_cm); Serial.println(" cm"); delay(100); // Wait for stability }