Built-in project · Arduino Uno R3

JSN-SR04T water tank level on an Arduino Uno

A JSN-SR04T’s sealed probe looks down at the water from a tank lid, NewPing times the echo, and the serial monitor prints the depth and how full the tank is. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $35. On Mokxi they're free.

JSN-SR04T water tank level · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Sensors
  • School
  • Using the Waterproof ultrasonic
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

A JSN-SR04T’s sealed probe looks down at the water from a tank lid, NewPing times the echo, and the serial monitor prints the depth and how full the tank is.

Arduino Uno R3 runs the sketch below. The circuit around it: Waterproof ultrasonic, JSN-SR04T. delay waits 500 milliseconds. Serial.begin opens the serial port at 9600 baud.

Parts list

3 parts, plus the jumper wires. They retail for about $35; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Waterproof ultrasonic, JSN-SR04T pin TRIGArduino Uno R3 pin 12
Waterproof ultrasonic, JSN-SR04T pin ECHOArduino Uno R3 pin 11
Waterproof ultrasonic, JSN-SR04T pin 5V5 V
Waterproof ultrasonic, JSN-SR04T pin GNDGround

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Water tank level with a JSN-SR04T waterproof ultrasonic sensor.
//
// The sealed probe sits in the tank's lid, looking down at the water; the
// control board stays dry outside on its cable. The farther the echo, the
// emptier the tank.
//
//   JSN-SR04T  5V -> 5V, GND -> GND, TRIG -> pin 12, ECHO -> pin 11
//
// HOW IT MEASURES. NewPing sends a 10 us trigger and times the echo:
//
//   distance  = the echo off the water, in cm (ping_cm)
//   depth     = TANK_CM - distance
//   full %    = depth / (TANK_CM - TOP_CM) x 100
//
// TANK_CM is the probe to the bottom of the tank, TOP_CM the probe to the
// water when the tank is full. Measure your own tank and change them.
//
// THE BLIND ZONE. The probe sends and listens with one transducer, which is
// still ringing for the first 20 cm. Anything nearer reads about 20 cm,
// never less, so a reading under BLIND_CM is not a distance: the tank is
// overfull or something is on the probe. That is why TOP_CM is 30, not 0:
// mount the probe at least 25 cm above the highest the water can get.
//
// THINGS TO TRY. Drag the slider down to 15 cm: the reading sticks at 20.
// Drag it past 6 m: no echo, and NewPing gives 0. Set the part's mode to
// serial-auto and read the frames with Serial instead (FF, high, low, sum).

#include <NewPing.h>

const int TRIG = 12;
const int ECHO = 11;
const int TANK_CM = 200;   // probe to the bottom of the tank
const int TOP_CM = 30;     // probe to the water when the tank is full
const int BLIND_CM = 25;   // nearer than this is the blind zone

NewPing sonar(TRIG, ECHO, 450);

void setup() {
  Serial.begin(9600);
  Serial.println("JSN-SR04T tank level");
}

void loop() {
  delay(500);
  unsigned int cm = sonar.ping_cm();
  if (cm == 0) {
    Serial.println("No echo: check the probe's cable.");
    return;
  }
  if (cm < BLIND_CM) {
    Serial.print(cm);
    Serial.println(" cm: blind zone, the tank is overfull");
    return;
  }
  int depth = TANK_CM - (int)cm;
  if (depth < 0) depth = 0;
  int percent = (long)depth * 100 / (TANK_CM - TOP_CM);
  if (percent > 100) percent = 100;
  Serial.print("Water ");
  Serial.print(cm);
  Serial.print(" cm down, ");
  Serial.print(depth);
  Serial.print(" cm deep, ");
  Serial.print(percent);
  Serial.println("% full");
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Runs once when the board starts. It gets the pins and libraries ready.

void setup() {
  Serial.begin(9600);
  Serial.println("JSN-SR04T tank level");
}

loop sketch.ino

Runs over and over for as long as the board has power. This is where the project does its work.

void loop() {
  delay(500);
  unsigned int cm = sonar.ping_cm();
  if (cm == 0) {
    Serial.println("No echo: check the probe's cable.");
    return;
  }
  if (cm < BLIND_CM) {
    Serial.print(cm);
    Serial.println(" cm: blind zone, the tank is overfull");
    return;
  }
  int depth = TANK_CM - (int)cm;
  if (depth < 0) depth = 0;
  int percent = (long)depth * 100 / (TANK_CM - TOP_CM);
  if (percent > 100) percent = 100;
  Serial.print("Water ");
  Serial.print(cm);
  Serial.print(" cm down, ");
  Serial.print(depth);
  Serial.print(" cm deep, ");
  Serial.print(percent);
  Serial.println("% full");
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.