I am completely new in this Arduino environment and I have a project that I'm gonna use waterproof ultrasonic sensor A02YYUW.
I am using Arduino Nano. When I upload the code below, I am getting the same values. Even the location of the sensoris constant, the values are same and I am getting the value of 0 cm mostly. I dont know how it is possible.
That code appears to be for a standard ultrasonic distance sensor.
The sensor you have has a special serial interface. Here is a description which includes a code sample: A02YYUW Waterproof Ultrasonic Sensor Wiki - DFRobot
I am positive. I have bought this sensor through the link below. I have checked the datasheet which is in the link and did the wiring according to this.
I can't see what colour wires go into the sensor, but these must match your pin definitions iny our code here:
The Green wire appears to be RX from the illustration in the documentation, but it is not clear if that is a standard. It appears to be different to your wiring. Anyway, just check it again.
A couple of random thoughts after looking around for more details on this sensor ...
It seems that the Rx input to the sensor is not a serial input but a discrete signal which appears to alter the amount of signal processing the module does before outputting the result.
From the previous link to the product: "When "RX" floats or input High level, the module outputs processed value, the data is more steady, response time: 100-300ms; when input Low level, the module outputs real-time value, response time: 100ms."
I wonder if the software serial port is losing track of the start of each message from the sensor?
When you get valid readings from the sensor, do they tie in with the distance to the object in front of the sensor?
you can now remove the additional debug code suggested in post #8
did you make any changes to the positioning of the sensor or the object in front of it. ? The jump from around 7cm to around 60cm would otherwise be difficult to explain.