Go Down

Topic: Ultrasonic Sensor using Uart Serial Communication? (Read 184 times) previous topic - next topic

JugglingPro

Mar 14, 2019, 05:43 am Last Edit: Mar 14, 2019, 06:00 am by JugglingPro
Hi everyone!

I have some decent experience tinkering with Arduino using simple digital and analog pins, but I've never dealt with a sensor that uses a communication protocol like UART serial communication.

I've followed some of the examples around the forums, but it seems as if the SoftwareSerial library is mostly for sensors that have both RX and TX lines. My sensor is odd in the sense that there is only a transmit line, therefore it seems like the Arduino is always reading data from it. The datasheet from the sensor in question.

I'm currently just reading the raw data from the sensor in binary format, using the code below and the single output pin connected to digital 10 (TX pin 11 is just not plugged in).

Code: [Select]

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 10); // RX, TX
byte reading; 

void setup() {
  mySerial.begin(9600);
  Serial.begin(9600);
}

void loop() {
  if (mySerial.available() > 0){
    reading = mySerial.read();
    Serial.println(reading, BIN);
  }
}



The datasheet says that with a baud rate of 9600, a complete 'message' should be 4 bytes long: a start signal 0xFF, a 8 bit signal, another 8 bit signal, and finally a stop signal 0xFF. Looking at my serial monitor, the output looks like:

Code: [Select]

11111111
10100
0
0
11111111
10100
0
0
11111111
10100
0
0


Two questions from this:
1. Why is mySerial.available() > 0 required? The datasheet says the baud rate for signals is 9600, doesn't that mean no matter what comes out of the serial monitor, it will be in the format start_data_data_stop? It seems that here, the start bit is sent a bunch of times before a complete 'data transfer' is done.
3. I'm moving my sensor around and the output doesn't change from what I copied from above. Is this likely a sensor issue, or is my code not parsing/missing/omitting some data being sent? Is this because I'm not using TX?

This has been a great learning experience so far, just hoping to get some guidance now with the fun stuff. Any help is appreciated!


JugglingPro

I just noticed that '10100' refers to a decimal of 20mm, which is the minimum detection range specified on the datasheet. I am still not able to get it to read an actual value when it is pressed against the bottom of a water bucket....

pylon

Code: [Select]
SoftwareSerial mySerial(10, 10); // RX, TX

If you don't need RX or TX set the corresponding pin to -1 and not equal the pin of the other direction!


Quote
a complete 'message' should be 4 bytes long: a start signal 0xFF, a 8 bit signal, another 8 bit signal, and finally a stop signal 0xFF.
I read the datasheet differently:

Quote
The data of the sensor consists of boot code 0xFF (DATA1), distance signal (DATA2+DATA3) and Last Frame 0x00 (DATA4
The end byte is 0x00 and not 0xFF. So the output you get corresponds with the datasheet.

Quote
1. Why is mySerial.available() > 0 required? The datasheet says the baud rate for signals is 9600, doesn't that mean no matter what comes out of the serial monitor, it will be in the format start_data_data_stop? It seems that here, the start bit is sent a bunch of times before a complete 'data transfer' is done.
The available() method tells you how much data already arrived (the number of complete bytes, not bits), so it's necessary to know if you already got data or if you still have to wait for them to arrive.
The rest of the question seems to be a wrong mixing of the terms "bit" and "byte".

Quote
3. I'm moving my sensor around and the output doesn't change from what I copied from above. Is this likely a sensor issue, or is my code not parsing/missing/omitting some data being sent? Is this because I'm not using TX?
Moving around doesn't help if it already sees the minimal distance but it depends on how you move it around and what environment you're doing that in. The sensor is measuring a distance, not acceleration, so it won't detect movement (at least not directly).

Quote
I am still not able to get it to read an actual value when it is pressed against the bottom of a water bucket....
English is not my mother tongue so I might get the wrong sense from that sentence but from what I understand you're using the sensor wrongly. You have to mount the sensor to the cover of your water bucket (or a similar position if it hasn't a cover), the measurement direction downwards to the water surface.

JugglingPro

Hey, thanks for the quick response! After reading your response and the output protocol on the datasheet, it does seem that my output is correctly the 4 byte message.

In regards to the tests I've been doing, I've just been putting the sensor against the bottom of multiple containers with different levels of water (above 2cm). No matter which container I use, or how hard I press the sensor against the bottom of the tank, I cannot get a reading higher than the lower limit of 20mm. I heard that using a liquid medium helps with ultrasonic, but I am not even getting anything even close to accurate here. That is why I was curious about the output data, ie. whether I was somehow missing information or not outputting the correct one to Serial Monitor.

The datasheet does state that:
Code: [Select]
This product is installed under the bottom of containers, so it can prevent the detected object from polluting.

Is my understanding of where to mount it wrong? I saw that they had a similar top level sensor (which I assume is mounted on the top), but I'm not sure how they differ functionality wise.

I'm also confused by the assembly safe distance specified in the datasheet:
Code: [Select]
Assembly Safety Distance (the distance between the sensor and the container wall => 50mm.
I can't tell if thats saying to leave a 50mm gap on the sides of the sensor, or if the ultrasonic sensor needs a 50mm gap before it hits the bottom of the container and then the liquid.

pylon

Quote
No matter which container I use, or how hard I press the sensor against the bottom of the tank, I cannot get a reading higher than the lower limit of 20mm.
Again: That sensor must not be pressed against the bottom of the tank but mounted at the ceiling (cover) and directed downwards. I have no clue how you can think of pressing that thing to the bottom.

Although the datasheet also reads as an installation on the bottom of the container, the installation drawing shows that the installation is at the cover/ceiling. Keep in mind that the datasheet was written by a Chinese, so I would rely on the pictures more than on the words.

Quote
I'm also confused by the assembly safe distance specified in the datasheet:
That means you must install it in the middle of the cover because the walls might be otherwise detected as the water level.

DaveEvans

#5
Mar 14, 2019, 07:26 pm Last Edit: Mar 14, 2019, 09:49 pm by DaveEvans
Again: That sensor must not be pressed against the bottom of the tank but mounted at the ceiling (cover) and directed downwards. I have no clue how you can think of pressing that thing to the bottom.
It's probably because the datasheet says...
Quote
The product is installed under the bottom of containers...
The installation drawing is somewhat ambiguous, but I think it shows the sensor mounted to the bottom of a tank.  In screenshot below: red=sensor...pressed against tank bottom.  blue=connector...noted merely for orientation purposes.  Note also that it seems to stipulate that the tank bottom be less than 5 mm thick (green).



Also, to get it to work...as @wvmarle noted in the other thread where you cross-posted your question, you probably have to use a coupling agent between the sensor head and the bottom of the tank (such as glue, vaseline, or made-for-purpose coupling fluid)...in addition to holding it tight against the tank surface.



JugglingPro

Yeah that was my guess as well, the instructions seem to point that it has to be mounted on the bottom. There is a similar product from the same company that specifically says top mounted ultrasonic sensor,  datasheet found here. That being said, I assumed it was safe to say that bottom mounting was correct.

I did notice that the sensing area on the sensor has some sort of liquid medium in it too, but I thought that was sufficient when pressed firmly against the bottom of a surface. I will try with some like ultrasonic couplant material and see if it helps!

Go Up