Measuring Volume of Water in a Tank Using Arduino and Ultrasonic Sensor

Introduction: Measuring Volume of Water in a Tank Using Arduino and Ultrasonic Sensor

This instructable outlines the process of measuring water volume in a tank using an Arduino and ultrasonic sensor. This is a quick and easy solution to approximate the water capacity remaining in a tank.

The following apparatus will be used for the purposes of this demonstration:

- Computer with Arduino IDE

- Arduino Nano

- HC-SR04 Ultrasonic Sensor

- Breadboard


- Water Tank

- Tank lid or clamp (for positioning Arduino)

Step 1: Physical Set Up

1. Insert the Arduino onto the breadboard

2. Connect the ultrasonic sensor as follows on the breadboard

- Ground wire -> GND input

- V+ wire -> Vin input

- Trigger wire -> D9

- Echo wire -> D10

3. Fill the tank with the desired amount of water.

4. Position the Ultrasonic sensor on the top of the water tank using a clamp or mounting apparatus of choice that ensures stability when in use (for accurate readings)

5. Connect the Arduino to the PC of choice running the Arduino IDE software using the USB cable

Step 2: Coding in the Arduino IDE

1. Copy and Paste the template code below into the IDE

//all volume quantities are in [cm^3]

const int trigPin = 9;

const int echoPin = 10;

long duration;

long distance;

float waterHeight;

float tankHeight = 52; //enter your water tank height here in [cm]

float totalVolume = 2613; // volume here is in [cm^3], change depending on tank volume

float waterQuantity;

float volume;

float pi = 3.14159265359;

float offset = 245;

void setup() { // put your setup code here, to run once:

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

Serial.begin(9600); }

void loop() {

// put your main code here, to run repeatedly:

digitalWrite(trigPin, LOW);

delayMicroseconds (2);

digitalWrite(trigPin, HIGH);

delayMicroseconds (10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = duration*0.034/2;

volume = pi*16*distance; //change based on the volume for the tank

waterHeight = tankHeight-distance;

waterQuantity = totalVolume-volume-offset;

Serial.print("Water level [cm]: ");


Serial.print("Water Quantity [ml]: ");





2. Click verify and upload the code to the Arduino (this may take a few moments)

3. Observe the code run

Verify the functionality of the code and the accuracy of the calculation by changing the level of water in the tank and reading it from the IDE results.

Step 3: Possible Sources of Error & Troubleshooting

Please note that although the volume measurement system using the Arduino and ultrasonic sensor is a low-cost solution to measure volume, possible sources of error may arise.

Measuring water capacity - possible errors

1. Vibrations in the container may cause the level of water to shift, despite the volume remaining the same, this may cause the ultrasonic sensor to give an inaccurate reading in the IDE.

2. Ensure that the volume of your tank is correctly entered into the IDE.

Troubleshooting - Arduino connections

1. Ensure when using the Arduino nano, that the correct configuration settings have been entered.

a. Select 'Tools' and select 'Board'. Choose the appropriate Arduino (in this case chose 'Arduino Nano')

b. Select 'Processor' under the 'Tools' menu and choose 'ATmega328P'

c. Again, under the 'Tools' menu, choose the appropriate port as connected via USB

d. Ensure that your 'Programmer' is set to 'AVRISP mkII'

If problems persist, try the following;

- Change your Arduino

- Update & restart your Arduino IDE

- Change your breadboard

- Reconnect your Arduino to the PC

Be the First to Share


    • Fried Food Challenge

      Fried Food Challenge
    • Fandom Contest

      Fandom Contest
    • Halloween Contest

      Halloween Contest



    Question 1 year ago

    I would like to adopt this for a 5000L water tank in my house. The tank is rectangular with rounded ends each side whereas it looks like your tank is cylindrical which is probably why you had to use Pi .

    Re the code:
    * What's the offset = 245?
    * Also the volume calculation has a factor 16. What's that for?

    To calculate volume on my tank would be a lot more engaged and more measurements would be required. Wouldn't it be possible to simply look at this as an inversely proportional relationship between distance and volume? Say my tank is 2m high with a volume of 5000L. That makes it 2500L at 1m or 25L per cm.