Fuel volume of rectangular tank using Arduino

I'm using an HC-SR04 ultrasonic sensor to detect level and use that to calculate fuel volume in a rectangular tank. When I get fuel volume, I get some negative answer, which does not match my manual calculation. All calculations are in millimeters.
Your help will be crucial so I can complete my project.```

  // Set the trigger pin LOW for 2uS
  digitalWrite(TRIGPIN, LOW);
  delayMicroseconds(2);
 
  // Set the trigger pin HIGH for 20us to send pulse
  digitalWrite(TRIGPIN, HIGH);
  delayMicroseconds(20);
 
  // Return the trigger pin to LOW
  digitalWrite(TRIGPIN, LOW);
 
  // Measure the width of the incoming pulse
  duration = pulseIn(ECHOPIN, HIGH);
 
  // Determine distance from duration
  // Use 343 metres per second as speed of sound
  // Divide by 1000 as we want millimeters
  
//Maximum water level = 257mm;
//Spacing btwn sensor and max height = 43mm;
float distance = (duration / 2) * 0.343;


  // Volume Calculation
  
  float l = 200;
  float w = 200;
  //distance = 253;
  // Volume of rectangular tank V =l*w*h
float V1 = l*w*new_distance;

// Change mm to Litres
float V = V1*(0.000001) ;

 
  // Print result to serial monitor
  Serial.print("Water Volume: ");
  Serial.print(V);
  Serial.println(" Litres");
 
  // Delay before repeating measurement
  delay(1000);
}
distance = (duration / 2) * 0.343; //<- the result is put in an int. values will range from 0,1....?

  int l = 210;
  int w = 210;
  // Volume of rectangular tank V =l*w*h
float V1 = l*w*distance;

int * int * int. If the result is >32767, the result is still int, but (-).
Your smallest distance will result in 0, quickly followed by bigger numbers. But,
210*210 = 44,100. Try making them long ints, or start them all as floats.

1 Like

If that's it, please flag the topic as solved.
Thanks!

I change all parameters to float and give positive numbers. But the result does not make sense.

Not much help there. Try printing your intermediate numbers as well. The duration value from the sensor, then the intermediate calc'd values. One long print line is fine, characters are free.
Does the initial duration make any sense? If you're getting garbage there, the rest is a waste.

1 Like

I'd have thought that if a calculation was returning a result in meters, you'd want to multiply the result by 1000 to get millimetres.

1 Like

I'm using microseconds that is why I divide

Thank you
able to solve the problem

OK. When you test the device, are you getting plausible results for the distance in millimetres?
I don't think that your use of distance in the volume calculation is correct. I imagine that the ultrasonic sensor is above the fuel so the shorter the distance the greater the volume of fuel. Your calculation should include the distance between the sensor and the bottom of the tank.

1 Like

Your topic was moved to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

It will help you get the best out of the forum in the future.

Thank you

Speed of sound = approximately 342 meters per second or 342000 mm / sec, 342000 / 1000000 micros per second = 0.342 / 2 (1/2 of ping and echo return time) = 0.171 mm per microsecond. So duration in micros * 0.171 = millimeters.

float distance = duration * 0.171;

Hi, @joeblacck

Can you please post a diagram of your tank and where the ultrasonic sensor is?
Include the tank dimensions and how far the sensor is from the BOTTOM of the tank.

Your equation, volume increases in value as the distance from the sensor to the fuel surface increases this is as the level goes down.

You need to subtract the sensor reading from the distance from the bottom of the tank to the sensor to get the fuel depth, then you can calculate volume.

You appear to be calculating the volume of the open space above the fuel.

Have you looked at the NewPing library to take care of ALL your ultrasonic measurements?

Tom.. :smiley: :+1: :coffee: :australia:

1 Like

No worries
I was able to solve the issue

No worries
I was able to solve the issue
Thanks

How???
To help others who might have a similar problem, posting your solution would be the best thing to do.

Thanks... Tom... :smiley: :+1: :coffee: :australia:

3 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.