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.
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.
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.
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.
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?