measuring water volume in a rectangular tank using arduino and ultrasonic sensor

hello guys. i hope someone can help me on this project.

my project is based on ultrasonic sensor. measuring water volume in the rectangular tank by using arduino and ultrsonic sensor. so basically i use ultrasonic sensor to measure distance and then manipulate the distance to calculate volume of water in a rectangular tank.

first i need to measure the initial distance between sensor and empty base tank.
second, fill the rectangular tank with water.
third, measure the distance from sensor to water surface.
fourth, calculate the difference between initial distance and distance of sensor to water surface to get height of water in the rectangular tank.
fifth, calculate the volume of the water in the tank using basic rectangular volume which is height x surface area of tank based.
lastly, show the volume measurement to lcd.

so i used arduino uno, ultrasonic sensor, jumper wires, lcd.

may i know how to get the coding?

EDIT: THIS IS MY LATEST CODING. IS THERE ANYWAY I CAN IMPROVE THIS SO I DO NOT NEED ENTER VALUE OF INITIAL DISTANCE MANUALLY IN THE CODING EVERYTIME I WANT TO START THE PROTOTYPE AND MEASURE DIFFERENT TANK? MEANS I NEED THE SENSOR WILL MEASURE AND AUTOMATICALLY BECOME THE INPUT OF THE CODING AS INITIAL DISTANCE.

const int trigPin = 12;
const int echoPin = 11;

float initialdistance = 40; // insert initial distance or distance between sensor and empty base tank
float surfacearea = 10; // enter value of surface area of the rectangular tank base by multiplying width and length

float duration, distance, heightofwater, volume;

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}

void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = (duration*.0343)/2;
heightofwater = initialdistance-distance;
volume = heightofwater*surfacearea;
Serial.print("Distance: ");
Serial.println(distance);
delay(1000);
Serial.print("Volume: ");
Serial.println(volume);
delay(1000);
}

First step will be to decide how your going to actually measure the level.

Having a external pipe that extends up past your fill line offers many advantages for sensor placement as most ultrasonic sensors have a minimum distance of a couple centimeters.

Then decide on which ultrasonic sensor your going to use, (suggest waterproof).

Then plan out everything your going to need for the LCD and Sensor.

The code to time a ultrasonic sensor pulse and convert into distance is common.
The code to display to a LCD is common.
Figuring out your calibration will take some skill, but you should be able to put together a test rig (pvc tube) and map distances with little fuss.

This has been covered a few times , another option worth looking at, which may be more rugged , is to put the tank on a load cell and weigh it .

It looks like school work so the OP will not have the flexibility to optimise the solution.
Googling for "arduino ultrasonic sensor" and "arduino lcd 1602" (or whatever the LCD type is) would be a good start.
Printing out a distance derived from the sensor on the serial monitor and printing "hello world" on the LCD would be the next steps-

Since the question is about writing the sketch and since the hardware has been chosen, the anser is use. Newping.
The newping library will tell you the distance.

As the math on calculating volume.

Measure the width front to back and side to side. Multiply to get area.

Newping will tell your height
Say it reads 87 when empty and 12 when full
You can scale. 87 to 12 to a new scale of 0 to 100 percent. Or to 70 cm

Area is fixed so
SqCM =Area x height
You need to culate how many liters per sq cm to get a value
Volume = SqCM x litrePerSqCM

6v6gt:
It looks like school work so the OP will not have the flexibility to optimise the solution.

If a post looks like school work, best to quote the question and save it so we do not get another. Post that is. Just .

hammy:
This has been covered a few times , another option worth looking at, which may be more rugged , is to put the tank on a load cell and weigh it .

i did not find any in the forum cover about coding to calculate the volume of water in a tank using formula from ultrasonic sensor input. ca u share one?

mohdfiras19:
i did not find any in the forum cover about coding to calculate the volume of water in a tank using formula from ultrasonic sensor input. ca u share one?

Come on! You must be brighter than you are letting on. If you can measure the horizontal sides of a rectangle and compute the area, then you can multiply the area by the water height. The height you get by subtracting the measured water level from the total height of the container.

You should have learned that in grammar school.

Paul

mohdfiras19:
i did not find any in the forum cover about coding to calculate the volume of water in a tank using formula from ultrasonic sensor input. ca u share one?

Did you google for the newping library ?
Did you watch any youtube on the sensor ?
There are probably hundreds of ways to copy the work of others for your project.

Your Arduino IDE has ping as an example .
We are here to help those who are having problems to figure out what they did wrong.
it sounds like you just want us to write the code for you and you are not doing your own research.

run the PING example in your IDE with your sensor and you should get readings.
In case you are really not any good at math, the long distance to an "empty" tank is just thinking backwards.
it is the distance of the box full of AIR.
Displace the air with water and you can calculate how much air is in the box.
if the empty box contains, 700 CU CM of air, but only 212.34 are left.....

dave-in-nj:
Did you google for the newping library ?
Did you watch any youtube on the sensor ?
There are probably hundreds of ways to copy the work of others for your project.

Your Arduino IDE has ping as an example .
We are here to help those who are having problems to figure out what they did wrong.
it sounds like you just want us to write the code for you and you are not doing your own research.

run the PING example in your IDE with your sensor and you should get readings.
In case you are really not any good at math, the long distance to an "empty" tank is just thinking backwards.
it is the distance of the box full of AIR.
Displace the air with water and you can calculate how much air is in the box.
if the empty box contains, 700 CU CM of air, but only 212.34 are left.....

i understand where u come from. i already did all the research. i think my question is not clear enough and lead to this misunderstanding. actually i already done the measuring volume part and i get the result. it just i want to know how can i measure the depth of the empty tank at first using sensor and it will automatically become the input to calculate the value of water level or height of the water. i cant get how to create the right coding for that part. measuring distance from sensor to water surface part? done. calculate volume? done. i need to improve the coding so i do not need to set initial distance (distance from sensor to base of the empty tank).

Paul_KD7HB:
Come on! You must be brighter than you are letting on. If you can measure the horizontal sides of a rectangle and compute the area, then you can multiply the area by the water height. The height you get by subtracting the measured water level from the total height of the container.

You should have learned that in grammar school.

Paul

hello paul. sorry for my not clear question. i had done all the part actually. is just the first part or calibration part. everytime i want to start, i need to measure the distance between sensor and empty base tank and manually enter the input in coding which i called initial distance. after that all the mathematics simple formula take part and i get the results. yeah i done that part. actually my problem now is, is there any way i can automatically measure using sensor the initial distance and it will automatically become the input in the coding so i dont need manually edit the coding again and again when i used different tank. sorry for the misunderstanding

mohdfiras19:
i want to know how can i measure the depth of the empty tank at first using sensor and it will automatically become the input to calculate the value of water level or height of the water.
i cant get how to create the right coding for that part.
measuring distance from sensor to water surface part? done.
calculate volume? done.
i need to improve the coding so i do not need to set initial distance (distance from sensor to base of the empty tank).

I am going to read into this pretty deep.
You want to have a devcie, put it onto an unknown tank
Then have it automatically display the volume.
You would need to have a 'calibrate' code
Put the unit into 'calibrate'
have it ask
'press the button when the tank is empty'
press the button when the tank is full
enter the area of the tank (use a pot or endocder for each digit)
save this information to EEPROM

or use blutooth and have it enter this from a cell phone.

I cannot see any way to automatically sense if the tank is a cylinder on the side or on end or a rectangle.
each might be different, but you could have it ask for the data and do the calculations for the shape.

an ECNODER could do all the button work. you could use 3 buttons on one Analog pin.

mohdfiras19:
i understand where u come from. i already did all the research. i think my question is not clear enough and lead to this misunderstanding. actually i already done the measuring volume part and i get the result. it just i want to know how can i measure the depth of the empty tank at first using sensor and it will automatically become the input to calculate the value of water level or height of the water. i cant get how to create the right coding for that part. measuring distance from sensor to water surface part? done. calculate volume? done. i need to improve the coding so i do not need to set initial distance (distance from sensor to base of the empty tank).

Thank you! A perfectly formed question.

I see you got a good answer later on. But, there is no difference in measuring an empty tank than a tank with some water or a full tank. Your problem is knowing and remembering the value for an empty tank.

Paul

dave-in-nj:
I am going to read into this pretty deep.
You want to have a devcie, put it onto an unknown tank
Then have it automatically display the volume.
You would need to have a 'calibrate' code
Put the unit into 'calibrate'
have it ask
'press the button when the tank is empty'
press the button when the tank is full
enter the area of the tank (use a pot or endocder for each digit)
save this information to EEPROM

or use blutooth and have it enter this from a cell phone.

I cannot see any way to automatically sense if the tank is a cylinder on the side or on end or a rectangle.
each might be different, but you could have it ask for the data and do the calculations for the shape.

an ECNODER could do all the button work. you could use 3 buttons on one Analog pin.

the idea is at first it will start measure the distance between sensor and empty base tank for 1 time only. like u said calibration. it is specifically only for rectangular tank. but different rectangular tank will have different initial distance from sensor to tank base since the height might be vary. for the surface area, i think i just set the input early in the coding. Thank you so much for the respons sir!

Paul_KD7HB:
Thank you! A perfectly formed question.

I see you got a good answer later on. But, there is no difference in measuring an empty tank than a tank with some water or a full tank. Your problem is knowing and remembering the value for an empty tank.

Paul

yes there is no difference. just i need to set the sensor by coding to measure one time only at the beginning to find the distance between sensor and empty rectangular tank base. then the distance will become the input for the calculation coding after the sensor measure the distance between sensor and surface of water. i think i need include button to set wether the sytem is on calibration mode or measuring mode.

mohdfiras19:
yes there is no difference. just i need to set the sensor by coding to measure one time only at the beginning to find the distance between sensor and empty rectangular tank base. then the distance will become the input for the calculation coding after the sensor measure the distance between sensor and surface of water. i think i need include button to set wether the sytem is on calibration mode or measuring mode.

this step might best be done in setup() and not loop()
also, if you are there and doing an installation, you can input the data and then store it in eeprom without having to upload the sketch in the field. that way, it will stay forever., until you want to change it.