Projects / Tank volume calculator

Tank volume calculator

Beginner1 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO

    Test the sensor

    Print distances over Serial.

  2. NANO

    Measure the empty distance

    Write it in the sketch.

  3. NANO

    Measure the full distance

    Write it in the sketch.

  4. NANO

    Enter the tank size

    Round or box.

  5. NANO

    Show litres

    Check against a bucket.

  6. NANO

    Add the median

    Remove stray readings.

  7. NANO

    Fit it above the tank

    Aim straight down.

In short

How do you build a tank volume calculator?

Measure the distance to the water and work out litres for a round or square tank. You need Arduino Nano, JSN-SR04T waterproof ultrasonic sensor and 16x2 LCD with I2C backpack (PCF8574). It takes 7 steps and about 60 minutes.

Level
Beginner
Time
About 60 min
Parts
3

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
JSN-SR04T VCCNano 5VPower
JSN-SR04T GNDNano GNDGround
JSN-SR04T TRIGNano D9Trigger pulse
JSN-SR04T ECHONano D10Echo pulse
LCD SDA and SCLNano A4 and A5I2C
LCD VCC and GNDNano 5V and GNDPower

Code

Distance is easy to measure; what you want to know is how many litres are left. The sketch finds the depth of the water as the empty distance minus the measured one, multiplies by the floor area, and shows percent and litres. Measure the empty and full distances in your own tank and type them in; the sensor cannot read closer than about 20 to 25 cm, so mount it that far above the highest water level. The sketch takes nine readings and uses the middle one, which ignores the odd stray echo from foam or a pipe. The probe is waterproof but the small control board is not, so keep the board in a dry box away from steam and condensation. Aim the sensor straight down, clear of the tank walls and pipes. For a tank that holds drinking water, keep the probe out of the water and use only sealed, food-safe fittings. Round tanks are the common case; for other shapes use the area you can measure. Arduino IDE, board "Arduino Nano" (try the Old Bootloader option if the upload fails).

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// Tank volume calculator: distance to the water -> percent and litres. Libraries: LiquidCrystal_I2C.
// Board: Arduino Nano. Set the numbers below for your tank, in centimetres.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte TRIG = 9, ECHO = 10;
const bool ROUND_TANK = true;        // true: cylinder, false: box
const float DIAMETER_CM = 100.0;     // used for a round tank
const float LENGTH_CM = 100.0, WIDTH_CM = 100.0;   // used for a box
const float EMPTY_CM = 120.0;        // distance from the sensor to the bottom when the tank is empty
const float FULL_CM = 25.0;          // distance when full (the sensor cannot read closer than about 20 to 25 cm)
float readCm() {
  digitalWrite(TRIG, LOW); delayMicroseconds(5);
  digitalWrite(TRIG, HIGH); delayMicroseconds(15); digitalWrite(TRIG, LOW);
  unsigned long us = pulseIn(ECHO, HIGH, 30000UL);
  return us == 0 ? -1 : us * 0.0343 / 2.0;
}
float medianCm() {
  float v[9]; int n = 0;
  for (int i = 0; i < 9; i++) { float d = readCm(); if (d > 0) v[n++] = d; delay(60); }
  if (n == 0) return -1;
  for (int i = 1; i < n; i++) { float k = v[i]; int j = i - 1; while (j >= 0 && v[j] > k) { v[j + 1] = v[j]; j--; } v[j + 1] = k; }
  return v[n / 2];
}
void setup() {
  pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT);
  lcd.init(); lcd.backlight();
}
void loop() {
  float d = medianCm();
  lcd.clear();
  if (d < 0) { lcd.print("No echo"); delay(2000); return; }
  float level = constrain(EMPTY_CM - d, 0, EMPTY_CM - FULL_CM);        // water depth in cm
  float area = ROUND_TANK ? 3.14159 * (DIAMETER_CM / 2) * (DIAMETER_CM / 2) : LENGTH_CM * WIDTH_CM;
  float litres = level * area / 1000.0;
  float pct = 100.0 * level / (EMPTY_CM - FULL_CM);
  lcd.setCursor(0, 0); lcd.print("Level "); lcd.print(pct, 0); lcd.print(" %");
  lcd.setCursor(0, 1); lcd.print(litres, 0); lcd.print(" litres");
  delay(2000);
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Empty tankAbout 0 percent
Add 20 litresReads about 20 litres more
Full tankAbout 100 percent
Cover the sensorNo echo message

If it does not work

Jumping values

Keep the beam clear of walls and pipes.

Reads always 0

Check the 20 cm minimum distance.

No echo

Check the TRIG and ECHO wires.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the tank volume calculator?

Arduino Nano, JSN-SR04T waterproof ultrasonic sensor and 16x2 LCD with I2C backpack (PCF8574).

How long does the tank volume calculator take to build?

About 60 minutes for a beginner maker, in 7 steps.

How do I know the tank volume calculator works?

Empty tank: About 0 percent. Add 20 litres: Reads about 20 litres more. Full tank: About 100 percent.

Jumping values: what should I check?

Keep the beam clear of walls and pipes.

Reads always 0: what should I check?

Check the 20 cm minimum distance.

No echo: what should I check?

Check the TRIG and ECHO wires.