# UART controlled ultrasonic sensor

**URL:** <https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092>\
**Category:** Programming\
**Created:** [January 7, 2024, 1:00pm UTC](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092 "2024-01-07T13:00:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![k0ral78](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@k0ral78](https://forum.arduino.cc/u/k0ral78)\
**Post date:** [January 7, 2024, 1:00pm UTC](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092/1 "2024-01-07T13:00:08Z")

</div>

Hello to everybody,  
I am a bit confused about how to read the output of an ultrasonic sensor with UART controlled.  
The sensor is this [AliX ultrasonic sensor](https://it.aliexpress.com/item/1005004273107315.html)

Does anyone can help me with a code example?

I tried this [First test code](https://wiki.dfrobot.com/Underwater_Ultrasonic_Obstacle_Avoidance_Sensor_6m_SKU_SEN0599#target_0) that is explicitly for UART controlled and I also tried some code in the comments in the product page but I did not get a proper output value: no value in the First test code.

Did anyone already use an UART controlled sensor? I have to better understand the difference between UART controlled and UART automatic.

Thank you in advance for your suggestions.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [January 7, 2024, 1:36pm UTC](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092/2 "2024-01-07T13:36:45Z")

</div>

may be the protocol is not the same as the one from DFRobot

try to see if anything get printed automagically (may be what they mean by "UART automatic". In the "UART controlled" you have to send a command to get a reading)

```arduino
#include <SoftwareSerial.h>
SoftwareSerial mySerial(7, 8);
void setup() {
  Serial.begin(115200);
  mySerial.begin(9600); // try various baud rates
}

void loop() {
  if (mySerial.available()) {
    int r = mySerial.read();
    if (isalnum(r)) {
      Serial.println((char) r);
    } else {
      Serial.print("0x");
      Serial.println((byte) r, HEX);
    }
  }
}

```

as we don't know which baud rate is used, you might have to try different common baud rates in this line

```arduino
  mySerial.begin(9600); // try various baud rates

```

---

<div class="post-metadata">

**Author:** ![k0ral78](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@k0ral78](https://forum.arduino.cc/u/k0ral78)\
**Post date:** [January 7, 2024, 2:25pm UTC](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092/3 "2024-01-07T14:25:55Z")

</div>

Thank you for your answer. I tried following baud rates with NO output: 9600, 19200, 38400, 57600, 115200.

I also tried to add `Serial.println("check");` at the beginning of the loop to be sure that the board was up and running with an USB adapter. "check" was printed correctly.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [January 7, 2024, 2:28pm UTC](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092/4 "2024-01-07T14:28:21Z")

</div>

how did you wire pins 7 and 8 ? did you connect also 5V and GND ?  
does your sensor have some marking that could let you find the specifications?

---

<div class="post-metadata">

**Author:** ![k0ral78](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@k0ral78](https://forum.arduino.cc/u/k0ral78)\
**Post date:** [January 7, 2024, 2:29pm UTC](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092/5 "2024-01-07T14:29:09Z")

</div>

In the comment of the page product, one user that bought the product said that he got the correct output using following (part) of the code:

```arduino
  //Define input and outputs 
  pinMode(pinTrig, OUTPUT);
  pinMode(pinEcho, INPUT);
  digitalWrite(pinTrig, HIGH);

uint16_t getSonarDistance(int Trig, int Echo) {
  // 15 ms @ 340 m/s = 5m round trip
  const unsigned long MEASURE_TIMEOUT = 15000UL;

  // Trigger the sensor
  digitalWrite(Trig, LOW);
  delay(5);

  // Read the signal from the sensor
  long duration = pulseIn(Echo, HIGH, MEASURE_TIMEOUT);

  // Set it back to HIGH until next Trig
  digitalWrite(Trig, HIGH);

  //convert into a distance
  distance = int(duration / 57.5);

  return distance;
}

```

---

<div class="post-metadata">

**Author:** ![k0ral78](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@k0ral78](https://forum.arduino.cc/u/k0ral78)\
**Post date:** [January 7, 2024, 2:32pm UTC](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092/6 "2024-01-07T14:32:20Z")

</div>

If you look at the [AliX product page](https://it.aliexpress.com/item/1005004273107315.html) there are many details.  
Maybe I have to connect together RX and ground...

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [January 7, 2024, 2:48pm UTC](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092/7 "2024-01-07T14:48:25Z")

</div>

I don't see the necessary details

---

<div class="post-metadata">

**Author:** ![stitech](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stitech/32/1016377_2.png) [@stitech](https://forum.arduino.cc/u/stitech)\
**Post date:** [January 7, 2024, 2:51pm UTC](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092/8 "2024-01-07T14:51:44Z")

</div>

It looks similar to this one, might be even the same manufacturer.  
Datasheet: [https://cdn-shop.adafruit.com/product-files/4664/4664\_datasheet.pdf](https://cdn-shop.adafruit.com/product-files/4664/4664_datasheet.pdf)

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [January 7, 2024, 2:55pm UTC](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092/9 "2024-01-07T14:55:02Z")

</div>

then that would be the important information to deal with

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/5/1/7/51758a80adad58b5b52d1265d0918bbd00d04b52.png)

OP needs to clarify how the wires have been really connected

---

<div class="post-metadata">

**Author:** ![k0ral78](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@k0ral78](https://forum.arduino.cc/u/k0ral78)\
**Post date:** [January 7, 2024, 5:07pm UTC](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092/10 "2024-01-07T17:07:01Z")

</div>

These images are in the link I copied above.

![connector_pins](https://europe1.discourse-cdn.com/arduino/original/4X/b/f/4/bf4a9148bf7f2585c7b1cfc49538b7c68d4a9a2f.png)

 ![connector_pins_2](https://europe1.discourse-cdn.com/arduino/original/4X/6/7/6/67621b7d0c50f264a4b314d3c3b66d186d515008.png)  
 ![connector_pins_3](https://europe1.discourse-cdn.com/arduino/original/4X/f/1/d/f1dfce8d512e85e4f87a8e1adf667518830a97b3.png)

---

<div class="post-metadata">

**Author:** ![k0ral78](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@k0ral78](https://forum.arduino.cc/u/k0ral78)\
**Post date:** [January 7, 2024, 5:12pm UTC](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092/11 "2024-01-07T17:12:12Z")

</div>

Thank you for this. I often used the UART auto with this information [https://wiki.dfrobot.com/A01NYUB%20Waterproof%20Ultrasonic%20Sensor%20SKU:%20SEN0313](https://wiki.dfrobot.com/A01NYUB%20Waterproof%20Ultrasonic%20Sensor%20SKU:%20SEN0313)

Now I bought an UART controlled but I can't understand how to read the output.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [January 7, 2024, 5:24pm UTC](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092/12 "2024-01-07T17:24:39Z")

</div>

I had not seen you could get more info

> [@k0ral78](#):
>
> but I can't understand how to read the output.

So it seems the frame starts with FF then you have the distance over 2 bytes and then a checksum.

If your wiring is correct my basic sketch at 9600 bauds should spit out the information

---

<div class="post-metadata">

**Author:** ![k0ral78](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@k0ral78](https://forum.arduino.cc/u/k0ral78)\
**Post date:** [January 7, 2024, 5:56pm UTC](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092/13 "2024-01-07T17:56:54Z")

</div>

I think that UART controlled means that there is the need of the trigger to read the output.  
In the datasheet posted above i can read:

> When the cable “RX” receives a trigger pulse with falling edge or any serial port  
> data, the module makes a measurement, the cable “TX” will output a  
> measurement distance value when the measurement is finished every time

Is it enough to trig the output enabling/disabling a digital I/O?

---

<div class="post-metadata">

**Author:** ![stitech](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stitech/32/1016377_2.png) [@stitech](https://forum.arduino.cc/u/stitech)\
**Post date:** [January 9, 2024, 1:48am UTC](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092/14 "2024-01-09T01:48:16Z")

</div>

It’s possible that these are 4 different products, in which case I wouldn’t know which one I’m buying, I just have to hope there’s some description within the package to work with.  
If it’s one product with several output options, the data in the description on the AliX site is severely lacking on how to set 4 options with 1 input, again just hoping the accompanying paper tells more.

![GL-A19](https://europe1.discourse-cdn.com/arduino/original/4X/c/5/e/c5e8d4b1834b9fdd69eb8d252123bd9313a65306.png)

---

<div class="post-metadata">

**Author:** ![bluejets](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bluejets/32/1013044_2.png) [@bluejets](https://forum.arduino.cc/u/bluejets)\
**Post date:** [January 9, 2024, 2:33am UTC](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092/15 "2024-01-09T02:33:49Z")

</div>

> [@k0ral78](#):
>
> The sensor is this [AliX ultrasonic sensor](https://it.aliexpress.com/item/1005004273107315.html)

You might think about using a standard waterproof sensor which already works with Arduino via it's own driver board.  
[https://www.ebay.com.au/itm/355328279127](https://www.ebay.com.au/itm/355328279127)

 ![Image1](https://europe1.discourse-cdn.com/arduino/original/4X/e/b/4/eb46d6d56256091be85816fcceb8f92ab161f66a.jpeg)  
 ![Image2](https://europe1.discourse-cdn.com/arduino/original/4X/5/d/3/5d3e6369921ed836c3c1df031117246552b03d67.jpeg)

---

<div class="post-metadata">

**Author:** ![k0ral78](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@k0ral78](https://forum.arduino.cc/u/k0ral78)\
**Post date:** [January 9, 2024, 7:10am UTC](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092/16 "2024-01-09T07:10:31Z")

</div>

I bought UART controlled

---

<div class="post-metadata">

**Author:** ![k0ral78](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@k0ral78](https://forum.arduino.cc/u/k0ral78)\
**Post date:** [January 9, 2024, 7:12am UTC](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092/17 "2024-01-09T07:12:47Z")

</div>

I already used this and it's not so accurate used on top of a tank. Did you have a code that you tried and it measures accurate data?

---

<div class="post-metadata">

**Author:** ![stitech](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stitech/32/1016377_2.png) [@stitech](https://forum.arduino.cc/u/stitech)\
**Post date:** [January 9, 2024, 7:21am UTC](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092/18 "2024-01-09T07:21:35Z")

</div>

Looking at those pictures, I’m not sure these belong together. The sensor has a USB plug, the board has a 2 pin HY connector. Am I oldfashioned with my idea that a vendor should show what it’s really selling?  
Or: do they really sell something from one box with something from another box without questioning if they go together?

---

<div class="post-metadata">

**Author:** ![JohnLincoln](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@JohnLincoln](https://forum.arduino.cc/u/JohnLincoln)\
**Post date:** [January 9, 2024, 7:36am UTC](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092/19 "2024-01-09T07:36:47Z")

</div>

> [@stitech](#):
>
> The sensor has a USB plug, the board has a 2 pin HY connector.

Look more closely, that's not a USB connector it's the mating part for the connector on the rear of the PCB.

![2 pin connector](https://europe1.discourse-cdn.com/arduino/original/4X/b/c/1/bc1a9d9736bbf991ca2acb7676f70207526ef21e.png)

---

<div class="post-metadata">

**Author:** ![stitech](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stitech/32/1016377_2.png) [@stitech](https://forum.arduino.cc/u/stitech)\
**Post date:** [January 9, 2024, 7:41am UTC](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092/20 "2024-01-09T07:41:07Z")

</div>

I guess you’re right about the connector. It doesn’t help OP with their problem tough, totally different output.

[Next page](https://forum.arduino.cc/t/uart-controlled-ultrasonic-sensor/1208092.md?page=2)
