Looking for new ideas on what to explore to debug a distance sensor

Hi there

Looking for ideas on what else I could test to debug a new water tank monitoring system I was trying to install yesterday in a friend's garden.

Here is the context:

The system is to be installed between a shed in the garden and an underground tank collecting rain water.
The pipe joining both is is about 7 meters / yards

There is an existing blue pipe sleeve joining the shed and the tank through which runs the pipe going from the tank to the shed's pump.

My idea was to use this sleeve to run a cable from the shed to the inside of the water tank where I could fix the sensor pointing towards the water surface

From an MCU perspective:

I'm using a UART based DYP-A02YY waterproof ultrasonic ranging sensor connected to a M5Stack M5StickCPlus using a ~8m long cat 6 ethernet cable with 4 twisted pairs from which I removed the RJ45 plugs to just get the wires.

On the sensor side, as it will be within the tank, I'm using a 5 way waterproof connector to join the sensor to the ethernet cable.

I constructed the system at home and tested it and all was working fine, wether the M5 Stick was powered from its builtin battery or through USB C, I was showing the distance without any faults.

So in theory all seemed ready to roll... but in practice as always it did not work out that way.


Once we managed to get the ethernet cable going through the pipe sleeve from the tank to the shed, I connected the arduino on one end and the sensor on the other end (just like at home) and the distance reading was just 0... :frowning:

:thinking: So I started testing a few things.

  • initial idea was that the voltage drop was somehow too high for some reason. I measured the voltage at the sensor, I was getting a clean 5V.

  • I tested continuity for the cable, the 4 wires from one end where correctly connected to the other end.

  • I was wondering if the Rx/Tx UART signal was getting distorted somehow along the way. The good thing about using this UART based sensor is that the protocol is simple : there is a 0xFF start marker, then 2 bytes representing the distance in mm as an uint16_t little endian format and then a checksum over the first 3 bytes. So if something goes wrong, you would not receive the start marker or the CKSUM would be wrong... I checked and I was getting correct messages matching the spec.

  • I was wondering if there could be some issue in transporting enough power (the sensor needs 8mA so nothing really serious) and although I needed only 4 wires (Rx, Tx, 5V, GND) going from the M5SticC to the sensor because I had 8 wires (4 pairs) in the cable, I rewired everything attaching a pair together to get "more copper" between the sensor and the M5 StickC. no luck...

So none of this worked.

I unplugged the sensor from the end of the long cable and connected it directly to the M5Stick C with a short cable and the distance sensor was working fine. so the sensor was not broken...


I'm running a bit out of ideas on what to test next. The system was working fine at home with 10m of Ethernet cables laid on the floor so distance did not seem to be an issue for the UART signal.

that was my code to read the sensor (called repeatedly until I get true - I added some prints to ensure I was not loosing bytes)

bool readOnce(uint16_t &distance) {
  const uint8_t startMarker = 0xFF;
  static bool waitForMarker = true;
  static uint8_t data[3];
  static uint8_t dataIndex = 0;

  if (Serial2.available() != 0) {
    uint8_t receivedByte = Serial2.read();

    if (waitForMarker) {
      if (receivedByte == startMarker) {
        dataIndex = 0;
        waitForMarker = false;
      }
    } else {
      data[dataIndex++] = receivedByte;
      if (dataIndex >= sizeof data) {
        waitForMarker = true;
        distance = (((uint16_t)data[0]) << 8) + data[1];
        uint8_t checksum = (((uint16_t)data[0]) + ((uint16_t)data[1]) - 1u) & 0x00FFu;
        return (data[2] == checksum);
      }
    }
  }
  return false;
}

So my question is: if that was your project - what would you be testing next ?

May be something to dig into as a 0mm distance is a message like this "0xFF 0x00 0x00 0xFF" so only 0x00 or 0xFF bytes. The protocol is respected and checksum works and I was not getting any bug in message reception, so it was not like I my code was picking up random noise, I was getting really a stream of 0xFF 0x00 0x00 0xFF

would a steady
0xFF 0x00 0x00 0xFF 0xFF 0x00 0x00 0xFF 0xFF 0x00 0x00 0xFF 0xFF 0x00 0x00 0xFF...
happen by magic? it does not feel random (2 0x00 followed by 2 0xFF)

I'd take any ideas — hey, even conspiration theory ones :slight_smile: — on why this is not working.

(if I can't get this to work, I have another version of the distance sensor using RS485 so might be more immune to noise on the line and the "long" distance but would require RS485 adaptation which means extra stuff to pack in a waterproof box inside the tank)

have you an oscilloscope to look at the serial TTL signal received?
could consider using RS232 serial?

not used one of DYP-A02YY sensors - I have used a JSN-SR04T waterproof sensor to measure river levels but the mirocontroller was within a few feet of the sensor therefore no communication problem

thx

I've a scope but would be a hassle to bring over there. I could use my small Logic Analyzer to see what's going on on the Serial line. the fact that I receive well formed messages seems to hint at a working line.

I could try to have an arduino on both ends and see if the Serial communication goes through - that could be an idea

how would that help?

(if it's about voltage and noise I'd portably be OK using the RS485 version of the sensor)

RS323 logic voltages -12 to +12 are more immune to noise than TTL 0-5V
RS485 would also work - the modules also tend to be smaller than RS232

OK - I would need a module on both ends in that case

On a second though, if I move to RS485 then I would only need the module in the shed (Serial to RS485) so would not change my setup in the tank.

I thing I'll try first with an arduino on both end doing some serial echo and see if that works or not.

I thought that the idea was to use RS232 on the link from the sensor to the Arduino to take advantage of the fact that it is more immune to noise. If so, then you need more electronics and voltages in the tank

so use something like a MAX202 RS232 to TTL on both ends

arduino -- RS232 converter ---- cable ---- RS232 converter -- UART sensor


I thought RS485 was also as immune to noise. I am wrong in that understanding ?

RS485 is also immune and even over longer distances. But for 7 meters RS232 should do. The only possible issue is that it's half-duplex; alternative can be RS422.

I am thinking that 0xFF is a start marker and I accept it as it is outside the boundary of ASCII codes of the digits: 0 - 9, A - F. After that I would expect ASCII codes (4-byte) for the four digits of the int-type data (0x0000 - 0xFFFF). After that ASCII codes (2-byte) for the 2-digit CHKSUM computed over 2-byte data (excluding the start marker).

There may be some sensors that deliver raw binray over UART Port. In that case, I would expect the start marker (the synchronizing pattern) be at least 3-byte binary to rule out the possibility of seeing the start marker as data.

Asynchronous TTL (0V - 5V) signal can travel upto 10 m (~= 32 feet) without amplification for Bd <= 4800. The OP's operating distance is 8 m.

If OP's sensor is delivering data at Bd > 4800, RS232 logic could be explored.

I don’t control what the sensor does
It’s 9600 bauds and a very trivial binary protocol as I explained

sounds a good idea!
I find the XY-017 UART Serial Port to RS485 Converter easy to use - no need to control TX/RX switching the device does it for you

As Bd > 4800 (post #10), you may try using TTL/RS232 logic.

Thanks for the ideas

I’ll try (when it stops raining…)

  • connecting two arduinos and testing if a simple echo code works

  • testing the RS585 version

I don’t have the rs232 adapters with me so can try that at the moment - might be next option

The sensor is only sending stuff by default so should work

Bury a cheap plastic fibre for each direction.
Interfaces are cheap.

Could be an option if everything else fails - (I still need 5V and GND inside the tank so will have wires)

Loopback test. Sorry if it is in your thorough description as having been performed and passed with flying colours…

a7

thx - it was not (I did not have two arduinos and did not think of connecting Tx to Rx simply...)

that's on my next test now

curious, maybe try running through same length of cable in the lab..

good luck.. ~q