Water tank level indicator with a JSN-SR04T and pump control on Arduino: design worksheet and code

A ₹1,445 design worksheet for an ultrasonic water tank level meter with fill percentage and litres, hysteresis pump control and dry-run protection, with the distance-to-level maths for a cylindrical tank.

About the author · Editorial & verification standards

  • Parts₹1,445
  • Bench time5–7 h
  • LevelIntermediate
  • ReviewedNov 2026

A ₹1,445 tank monitor that shows how full a water tank is and can switch a pump with sensible limits. A waterproof ultrasonic sensor sits above the water, the sketch turns the distance into a percentage and litres, and the pump starts when the tank is low and stops when it is nearly full. The mains side of any pump needs a qualified electrician, so the page treats the relay as the boundary.

Scope: intermediate. This is a design worksheet with illustrative costs. It has not been bench-tested by this site. The level table was computed by script for an example round tank 85 cm across, holding about 5.7 litres per centimetre of depth. The JSN-SR04T has a minimum range, commonly listed around 20 to 25 cm, so the full mark must be set below it. This is a hobby monitor; do not use it as the only protection against overflow or a dry-running pump, and have any mains wiring done by a qualified electrician.

How it works

The sensor measures the distance from itself down to the water surface. If the sensor is 130 cm above the bottom (empty) and the full mark is at 30 cm from the sensor, the usable range is 100 cm. Level in percent is (130 − distance) ÷ 100 × 100. For a round tank of radius 0.425 m the area is about 0.5675 m², which is 5.675 litres per centimetre of depth, so 100 cm of depth holds about 567 litres.

Pump control uses two thresholds so it does not chatter: it starts below 30 percent and stops at 90 percent or above. The sketch also stops the pump if it has run for more than 20 minutes, which catches an empty source tank or a stuck valve, and takes the median of five readings, because a single echo from ripples or a pipe can be wrong.

Bill of materials

PartQtyUnitTotal
Arduino Uno R3 (or compatible)1₹550₹550
JSN-SR04T waterproof ultrasonic sensor with board1₹450₹450
5 V relay module (to switch a contactor or low-voltage pump)1₹70₹70
16×2 LCD with I2C backpack1₹250₹250
Active buzzer module1₹15₹15
Breadboard1₹60₹60
Jumper wires1₹50₹50
Parts total₹1,445

Scroll the table sideways to see every column.

Prices checked Nov 2026. Shipping and tools are not included.

Prices are illustrative figures for Indian sellers in November 2026, before shipping, and have not been checked against current listings.

The numbers

Distance from sensorLevelVolume (5.675 L per cm)
130 cm0%0 L
105 cm25%142 L
80 cm50%284 L
55 cm75%426 L
30 cm100%568 L
Level = (130 − d) / 100; volume = level × 567.5 L for an example round tank 85 cm across. Measure your own tank and the sensor mounting height.

Because the minimum distance is about 25 cm for this sensor type, the sensor must be mounted at least that far above the highest water level. The speed of sound changes by about 0.6 m/s per degree, so on a hot roof the reading can be off by a percent or two; for tanks that is usually fine.

Power and wiring

PartConnects toNote
JSN-SR04T VCC and GND5V and GND
JSN-SR04T TRIG / ECHOD9 / D10Check your board’s mode jumper or resistor
Relay IND7Many modules switch on a LOW signal
LCD SDA / SCLA4 / A5Address often 0x27 or 0x3F
BuzzerD8
The relay contacts should switch a rated contactor or a low-voltage pump. Any 230 V wiring must be done by a qualified electrician with proper enclosures and protection.

The sketch

// Water tank level and pump control: Uno + JSN-SR04T + relay + LCD
// Design worksheet: measure H_EMPTY and H_FULL on your tank.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

const int TRIG = 9, ECHO = 10, RELAY = 7, BUZ = 8;
const bool RELAY_ON = LOW;                 // many modules are active-low
const float H_EMPTY = 130.0;               // cm from sensor to the bottom
const float H_FULL = 30.0;                 // cm from sensor to the full mark
const float LITRES_PER_CM = 5.675;         // example round tank 85 cm across
const float START_BELOW = 30.0, STOP_AT = 90.0, LOW_ALARM = 15.0;
const unsigned long MAX_RUN_MS = 1200000UL;    // 20 minutes

LiquidCrystal_I2C lcd(0x27, 16, 2);
bool pumping = false;
unsigned long pumpStart = 0;

float pingOnce() {
  digitalWrite(TRIG, LOW);  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH); delayMicroseconds(15);
  digitalWrite(TRIG, LOW);
  unsigned long t = pulseIn(ECHO, HIGH, 30000UL);
  return t == 0 ? -1 : t / 58.3;
}

float distance() {                           // median of five
  float v[5];
  for (int i = 0; i < 5; i++) { v[i] = pingOnce(); delay(60); }
  for (int i = 0; i < 4; i++) for (int j = i + 1; j < 5; j++)
    if (v[j] < v[i]) { float t = v[i]; v[i] = v[j]; v[j] = t; }
  return v[2];
}

void pump(bool on) {
  digitalWrite(RELAY, on ? RELAY_ON : !RELAY_ON);
  pumping = on;
  if (on) pumpStart = millis();
}

void setup() {
  pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT);
  pinMode(RELAY, OUTPUT); pinMode(BUZ, OUTPUT);
  pump(false);
  lcd.init(); lcd.backlight();
}

void loop() {
  float d = distance();
  if (d < 0) {                                // no echo: stop and warn
    pump(false); digitalWrite(BUZ, HIGH);
    lcd.setCursor(0, 0); lcd.print("Sensor error    ");
    delay(1000); return;
  }
  digitalWrite(BUZ, LOW);

  float pct = constrain((H_EMPTY - d) / (H_EMPTY - H_FULL) * 100.0, 0, 100);
  float litres = pct / 100.0 * (H_EMPTY - H_FULL) * LITRES_PER_CM;

  if (!pumping && pct < START_BELOW) pump(true);
  if (pumping && (pct >= STOP_AT || millis() - pumpStart > MAX_RUN_MS)) pump(false);
  if (pct < LOW_ALARM) digitalWrite(BUZ, (millis() / 500) % 2 == 0);

  lcd.setCursor(0, 0); lcd.print("Level "); lcd.print(pct, 0); lcd.print("%  ");
  lcd.print(pumping ? "ON " : "off");
  lcd.setCursor(0, 1); lcd.print(litres, 0); lcd.print(" L      ");
  delay(500);
}

A sensor fault stops the pump, which is the safer failure for filling a tank. The 20 minute run limit assumes a pump that fills the span in less time; set it from a measured fill time. A float switch as an independent overflow cut-off is strongly recommended in addition to this sketch. The sketch has been proofread but not compiled on this site.

Test plan

  1. Distance check: hold a flat board at 40, 80 and 120 cm and compare readings with a tape measure.
  2. Tank calibration: record the distance with the tank empty and with it at the full mark, and set H_EMPTY and H_FULL.
  3. Volume check: add a known volume and compare the litres reading.
  4. Pump logic on the bench: use the relay to switch a small low-voltage pump or an LED and run the level up and down by moving a board.
  5. Ripples and condensation: check readings during filling and in humid weather.
  6. Failure tests: unplug the sensor and confirm the pump stops and the buzzer sounds.

What usually goes wrong

SymptomLikely causeFix
Reads far too shortSensor closer to the water than its minimum rangeMount it higher
No echoWiring or board mode wrongCheck TRIG and ECHO and the module’s mode setting
Level jumps while fillingRipples or splashing from the inletUse a stilling tube or take a longer median
Pump chattersThresholds too closeWiden the START_BELOW and STOP_AT gap
Pump runs drySource emptyRely on the run limit and add a source float switch

Upgrades

  • Float switch cut-off: add an independent overflow and dry-run switch.
  • Wi-Fi alert: use an ESP32 to message you when the level is low.
  • Log usage: record the level each hour to estimate daily consumption.
  • Temperature correction: add a temperature sensor for the speed of sound.

Cost and parts notes

The ₹1,445 total is an illustration and does not include shipping. Use the cost calculator to compare sellers.

Common questions

It is a waterproof probe designed for damp places, which suits a tank. An ordinary sensor can fail in condensation.

Listings commonly give about 20 to 25 cm, so mount it above the highest water level by at least that much.

Not safely as a hobby build. Use a rated contactor and have the mains wiring done by a qualified electrician.

A single echo can be wrong from ripples or a pipe; the median ignores outliers.

Last reviewed: November 2026. This is a design worksheet with illustrative costs. It has not been bench-tested by this site; library names, pins and limits must be checked against your board and the current library versions, and measurements from your build are welcome.

More builds

Next builds to try

Free: the final-year project pack

A shortlist with BOMs, a report outline that matches the usual chapter scheme, a costing sheet and 40 viva questions. Built for the February–April submission season.

The Build Note

One short email when something is worth your bench time.

A new build sheet, a part price that moved, or a mistake worth avoiding. No daily drip, no recycled news.

One short email when a build or a price is worth your time. Unsubscribe in one click.