#define TRIG_PIN D5 // Define the Trig pin #define ECHO_PIN D6 // Define the Echo pin
void setup() {
Serial.begin(115200); // Initialize Serial Monitor at 115200 baud rate
pinMode(TRIG_PIN, OUTPUT); // Set Trig pin as OUTPUT
pinMode(ECHO_PIN, INPUT); // Set Echo pin as INPUT
}
// Trigger the sensor by setting the trigger pin HIGH for 10 microseconds
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the echo pin, and calculate the distance
long duration = pulseIn(ECHO_PIN, HIGH);
if (duration == 0) {
Serial.println("No echo detected");
} else {
float distance = (duration * 0.034) / 2; // Calculate the distance in cm
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
}
// Wait a bit before the next reading
delay(1000); // 1 second delay between readings
}
this is my code i am trying to make water level monitoring with arduino iot cloud and esp8266 but my sensor is not measuring values i have already used different ultrasonic sensor but still its same so any one can help me
Next time when you post the code use <code/> tag .
I dont see any issues with your code can you show me your connection .Any picture or .if you have any circuit diagram it would be fine
connection is correct because i need to connect echo and trig pin i am not getting data on dashboard it showing 100% in initial and my ultrasonic is also in working condition.