Hello to everybody,
I bought an Ultrasonic underwater ranging sensor on AliX.
It claims to be IP68.
Im trying to use it with an Arduino Yun following the instructions in the MANUAL
WIRING
The RX of the sensor is connected to triggerPin of Arduino;
The TX of the sensor is connected to receivePin of Arduino;
5V and Ground to the pin of Arduino.
CODING
I wrote the code below to read the serial but I can read only 0 in data[1] and data[2] as I Paste here:
20:58:56.746 -> Data[0]: 255
20:58:56.746 -> Data[1]: 0
20:58:56.746 -> Data[2]: 0
20:58:56.746 -> Data[3]: 255
20:58:57.913 -> Data[0]: 0
20:58:57.913 -> Data[1]: 0
20:58:57.913 -> Data[2]: 255
20:58:57.913 -> Data[3]: 255
20:58:57.913 -> Data[0]: 255
20:58:57.913 -> Data[1]: 0
20:58:57.913 -> Data[2]: 0
20:58:57.913 -> Data[3]: 255
What am I missing?
#include <SoftwareSerial.h>
const int triggerPin = 8;
const int receivePin = 9;
unsigned char data[4] = {};
float distance;
SoftwareSerial mySerial(9, 8); // RX_PIN = 9, TX_PIN = 8
void setup() {
pinMode(triggerPin, OUTPUT);
pinMode(receivePin, INPUT);
mySerial.begin(115200); // Initialize software serial
Serial.begin(115200); // initialize UART with baud rate of 9600 bps
}
void loop() {
// Trigger a falling edge
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10); // Adjust delay if needed
digitalWrite(triggerPin, LOW);
delayMicroseconds(1); // Adjust delay if needed
digitalWrite(triggerPin, HIGH);
delayMicroseconds(20); // Adjust delay if needed
// START READING FROM PIN RX //
do {
for (int i = 0; i < 4; i++)
{
Serial.print("Data[");
Serial.print(i);
Serial.print("]: ");
Serial.println(data[i]);
data[i] = mySerial.read();
}
} while (mySerial.read() == 0xff);
mySerial.flush();
if (data[0] == 0xff) // il primo carattere è 0xFF quindi l'Header iniziale
{
int sum;
sum = (data[0] + data[1] + data[2]) & 0x00FF;
if (sum == data[3])
{
distance = (data[1] << 8) + data[2];
if (distance > 280)
{
Serial.print("distance=");
Serial.print(distance / 10);
Serial.println("cm");
} else
{
Serial.println("Below the lower limit");
}
} else Serial.println("ERROR");
}
delay(150);
// STOP READING FROM PIN RX //
digitalWrite(triggerPin, LOW);
delayMicroseconds(1); // Adjust delay if needed
digitalWrite(triggerPin, HIGH);
// Wait for some time or perform other tasks
delay(1000);
}
Try following the instructions and code for on the DFRobot site for this sensor:
However, SoftwareSerial is very unreliable at 115200 Baud, so unless you can change that speed, you would be much better off using an Arduino with more than one serial port.
Thak You jremington! I am using ARduino Yun.
It looks that it is the same sensor but I cannot read anything.
I also tried to print "SerialAvailable" after if(mySerial.available() > 0) but the Serial Monitor in ARduino IDE is empty.
Using Yun, maybe I can use the serial with Bridge library to read the real serial port?
Sorry, I know nothing about the Yun. If it has more than one hardware serial port, you are always better off using that instead of SoftwareSerial.
I really doubt that the code you posted can work. Is that code actually recommended in the manual that you cannot post, because it could never work with a hardware serial connection. You cannot manipulate hardware serial outputs with digitalWrite().
According to Arduino, the Yun runs linux using an Atheros processor.
It could hardly be more distantly related to the Uno. It also has an ATmega32u4, which does have the possibility of an extra hardware serial, if the USB substitutes for Serial.
The link you posted points to four different models of the sensor. Which do you have?
@jremington Reading the link you posted I understood that it works ONLY underwater.
I would like to use it on top of a water tank to measure the water level. Therefore it's not useful to me. I would like to have IP68 because the other DFRobot sensor IP67 in the same scenario (on top of water tank) stops working after few months. I thought it was because of water infiltration.
No surprises there. Any powered electronics operating in a moist environment will eventually corrode due to condensation and electrolysis on the PCB, no matter how well is is protected against water intrusion. For operation inside a tank, at 100% humidity, it needs to be completely encapsulated or "potted" in silicon compound, as done for underwater use. Check the Maxbotix site for their recommendations.
A much better approach to monitor water tank level is to use a liquid pressure sensor, mounted at the bottom of the tank, or in the outlet pipe. If the latter, expect spurious readings when water is flowing due to the Bernoulli effect. I've been doing that for over 10 years, with only one sensor failure.
You can also buy absolute tank pressure sensors that are intended to be permanently submerged on the tank bottom, and don't suffer from the Bernoulli effect.
@jremington Thank you for your advices! I appreciate them.
I am looking for a cheap and "not invasive" system to measure liquid. I will see Maxbotix recommendations.
This time I will try with the same sensor but I will use silicon.
I will also look for liquid pressure sensor but I think it will be more expensive.
If placed at the bottom of the tank pointing up, the underwater sensor should read out the distance to the water surface. The water surface very effectively reflects sound -- as well as any solid object.