I want to use the US-100 ultrasonic sensor with Arduino uno to measure water level. But the before using it for liquid level measurement I am putting an object in front of it with a scale at the side. The sensor is giving very poor precision. When I move away the object it is sometimes showing a lower distance value. Even when I'm getting increased value the precision is not the same as I have read as 0.3 cm.
I am using the following code. I am a beginner and don't have much experience pls guide.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 2);
unsigned int HighByte = 0;
unsigned int LowByte = 0;
unsigned int Len = 0;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}
void loop() {
mySerial.flush();
mySerial.write(0X55); // trig US-100 begin to measure the distance
delay(500);
if (mySerial.available() >= 2) // check receive 2 bytes correctly
{
HighByte = mySerial.read();
LowByte = mySerial.read();
Len = HighByte * 256 + LowByte; // Calculate the distance
if ((Len > 1) && (Len < 10000))
{
Serial.print("Distance: ");
Serial.print(Len, DEC);
Serial.println("mm");
}
}
delay(300);
}