Hi guys,
I'm trying to measure a distance of a 3 meter water tank, with ultrasonic sensor to be exact from 3000mm to 0mm.
Example: We have a water tank with a capacity of 3000mm, so when the water gets closer to the sensor we want it to start counting till it reach it fullness which is 3000mm, also when the water starts down warding we want it to start counting till it reaches it's emptiness 0mm.
How can i do that?
This is what i have done but unfortunately it doesn't measure as it should be, if it's close it measures till 2743 and if it is far it measure just till 1700.
Here is the code:
//------------
//SENSOR
//------------
int readDistance() {
// Transmitting pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long t = 0, h = 0;
// Waiting for pulse
t = pulseIn(echoPin, HIGH);
// Calculating distance
h = t / 58;
h = h - 6 ; // offset correction
h = 20 - h; // water height, 0 - 50 cm
return 5 * h; // distance in %, 0-100 %;
}
My rusty math says:
If speed of sound = 343 m/s round trip time in µs for 3m = 3 / 343 * 1000000 * 2 = 17492µs, converted to 3000mm divide by 5.83. to invert, subtract result from 3000.
h = 3000 - (t / 5.83);
Adjust for offsets, etc.
You may be having echo problems if looking down into a square or rectangular tank, try spacing readings a few seconds apart to allow for ringing to subside.
Great it measures as expected but the highest value it reach when the object is closer to the sensor is 97 it should be 3000 and the lowest value it reach when the object is far from the sensor is 19 which is fair enough that means we are closer to the solution.
So do think it's reaching 97 as a highest value which it should be 3000 because of the offset correction here:
I am assuming the sensor is mounted above the tank, aimed downward. If that is correct, we need to know the distance in mm from the sensor to the water surface when the tank is full AND when empty. Surely the sensor is not mounted so it is touching the water when full.
What i'm trying to point to is yes the sensor is going to be above the tank and yes as you pointed to it is aiming downward. and that's 100% correct.
So when the water is close to the sensor which is about to be full it's going to measure 3000 and when it's down-warding to be empty it's going to measure until it reaches 0
OK then, with the tank full, measure how far the face of the sensor is above the water AND when the tank is empty, measure how from the sensor to the bottom of the tank and tell us.
Okay great, it should measure 3000 when it's full and when it's empty it should measure 0.
Please just be bare with me i will provide a picture of it to show you how it's going to working
The ultrasonic sensor measures distance from it to the target, in this case the water surface.
You need to measure the distance with a tape measure;
When the tank is full
OR
When the tank is empty.
This will give some parameters to do some calculations in your code to get the result you need.
We await your measurements with a tape measure from the sensor to the water surface, not just the ultrasonic reading.
Remember the sensor measures distances from it to the water surface, not directly the height of the water level.
Tom....
PS. I think you are pushing it a bit to get mm resolution from a water surface in a tank.
What is the volume of the tank?