Working Principle
Ultrasonic level sensors (like the JSN-SR04T and A02YYUW) measure liquid level by emitting an ultrasonic pulse downward from above the liquid surface and timing the echo return. The distance to the surface is calculated from the time-of-flight: d = (t × vsound) / 2. The liquid level is then: level = tank_height − d.
Difference from HC-SR04: These sensors use sealed, waterproof transducers (IP67/IP68) rated for humid and wet environments, with a single combined transmitter/receiver element. The HC-SR04 uses separate T/R elements and is not waterproof.
Electrical Characteristics
| Parameter | JSN-SR04T | A02YYUW |
|---|---|---|
| Range | 20–450 cm | 3–450 cm |
| Resolution | ~1 mm | ~1 mm |
| Accuracy | ±1 cm | ±1 cm |
| Blind Zone | 20 cm | 3 cm |
| Supply Voltage | 5 V (board) / 3.3–5 V (A02YYUW) | |
| Interface | Trigger/Echo (HC-SR04 compatible) | UART (9600 baud) or PWM |
| Transducer Rating | IP67 (waterproof) | IP67 |
| Cable Length | ~2.5 m | ~2.5 m |
Interfacing with an MCU
JSN-SR04T: Uses the same trigger/echo protocol as HC-SR04 — send 10 µs trigger pulse, measure echo pulse width with pulseIn(). The waterproof transducer connects via a cable to the main board.
A02YYUW: Outputs distance in millimetres via UART (4 bytes: header, distance high, distance low, checksum) at 9600 baud. No trigger needed — it auto-measures continuously.
Calibration
- Empty tank distance: Measure distance when tank is empty; this is your maximum distance
- Full tank distance: Measure at full level; level = empty_distance − current_distance
- Temperature compensation: Speed of sound varies ~0.6 m/s per °C; for outdoor tanks, compensate using an ambient temperature sensor
- Averaging: Take median of 5 readings to reject spurious echoes from splashing or foam
Code Example
/*
* JSN-SR04T Ultrasonic Level Sensor — Arduino
* Wiring: Trig → Pin 9, Echo → Pin 10, VCC → 5V
* Measures water level in a known-height tank
*/
#define TRIG_PIN 9
#define ECHO_PIN 10
#define TANK_HEIGHT_CM 100.0 /* Total tank height */
void setup() {
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.println("Ultrasonic Level Sensor Ready");
}
float measureDistance() {
/* Median of 5 readings */
float readings[5];
for (int i = 0; i < 5; i++) {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long dur = pulseIn(ECHO_PIN, HIGH, 30000);
readings[i] = dur * 0.0343 / 2.0;
delay(60);
}
/* Simple sort for median */
for (int i = 0; i < 4; i++)
for (int j = i+1; j < 5; j++)
if (readings[j] < readings[i]) {
float t = readings[i];
readings[i] = readings[j];
readings[j] = t;
}
return readings[2]; /* Median */
}
void loop() {
float dist = measureDistance();
float level = TANK_HEIGHT_CM - dist;
float pct = (level / TANK_HEIGHT_CM) * 100.0;
if (level < 0) level = 0;
Serial.print("Distance: ");
Serial.print(dist, 1);
Serial.print(" cm Level: ");
Serial.print(level, 1);
Serial.print(" cm (");
Serial.print(pct, 0);
Serial.println("%)");
delay(2000);
}
Real-World Applications
Tank Monitoring, Flood Detection & Agricultural Irrigation
Non-contact ultrasonic level sensors are used in water tank automation (automatic pump control when level drops), flood warning systems (river/drain level monitoring), agricultural reservoir management, chemical tank level in factories, and sewage treatment plant level monitoring. The non-contact nature means no corrosion or fouling from the liquid itself.
Limitations
- Foam and turbulence: Surface foam absorbs ultrasonic pulses; turbulent surfaces scatter echoes.
- Temperature sensitivity: Speed of sound changes ±0.17%/°C, affecting accuracy in uncompensated systems.
- Blind zone: Cannot measure within 3–20 cm of the transducer face (varies by model).
- Beam angle: The ~30° cone can reflect off tank walls in narrow tanks, producing false readings.
- Vapour and humidity: Dense vapor or condensation on the transducer face attenuates the signal.