JSN-SR04T Arduino: Waterproof Ultrasonic Sensor Guide and Code
JSN-SR04T Arduino guide: wiring, trigger and UART modes via the R27 resistor, the 20 cm blind zone, HC-SR04 comparison, tank level in litres and ESP32 code.
The JSN-SR04T is the waterproof ultrasonic distance sensor that Arduino and ESP32 makers reach for when water, humidity or dust would kill an ordinary module. It measures with sound like the HC-SR04, but the transmitter and receiver sit inside a single sealed probe, and the electronics board is separated from the probe by a 2.5 m cable. That lets you mount the probe in a water tank lid, a rain barrel, a well opening or a car bumper while the board stays dry in a protected box.
In this guide you'll learn how the JSN-SR04T works, where it differs from the HC-SR04, how to switch between trigger/echo and serial (UART) modes with the mode resistor (R27) on the board, why there's a blind zone of roughly 20–25 cm, and how to turn a distance reading into tank level in litres. Ready-to-use code for the Arduino Uno, ESP32 and Raspberry Pi Pico, plus 5 V versus 3.3 V compatibility, is included.
What is the JSN-SR04T and how does it work?
The sensor has two parts: a black probe, much like a car parking sensor, with a 40 kHz piezo transducer inside, and a control board carrying a microcontroller, a drive transformer and an amplifier. The HC-SR04 sends and receives through two separate "eyes"; on the JSN-SR04T the same transducer first emits the sound burst and then listens like an ear. The board times how long the echo takes to come back and works out distance as time × speed of sound / 2. At 20 °C sound travels at about 343 m/s, so every centimetre equals roughly 58 microseconds of round trip. The price of a single transducer is the blind zone: right after the burst the transducer keeps ringing for a while, and during that time it can't hear an echo from something very close.
| Feature | Value |
|---|---|
| Supply | 3.0–5.5 V (version 3.0); most reliable at 5 V |
| Current | About 5 mA idle, up to 30 mA while measuring |
| Range | From about 20–25 cm to 450 cm (version 2.0) or 600 cm (version 3.0) |
| Accuracy | About ±1 cm (on a flat, hard surface) |
| Frequency | 40 kHz |
| Beam angle | Wide cone, roughly 45–75 degrees depending on version |
| Probe | Sealed waterproof probe, 2.5 m cable |
| Board | About 41 × 28 mm, NOT waterproof |
| Output | Trigger/echo pulse or 9600 baud serial (UART) |
| Operating temperature | About −20 to +70 °C |
JSN-SR04T vs HC-SR04
| Feature | HC-SR04 | JSN-SR04T |
|---|---|---|
| Water and humidity | None, open circuit board | Probe is waterproof, board must stay dry |
| Transducer | Two separate (transmitter + receiver) | One sealed transducer |
| Minimum distance | About 2 cm | About 20–25 cm |
| Maximum distance | About 4 m | 4.5–6 m (depending on version) |
| Beam angle | Narrow, about 15 degrees | Wide, about 45–75 degrees |
| Trigger pulse | 10 µs | At least 20 µs recommended |
| Output modes | Trigger/echo only | Trigger/echo and serial (UART) |
| Supply | 5 V | 3.0–5.5 V (5 V recommended) |
| Cable | On the board, no cable | 2.5 m between probe and board |
In short, the HC-SR04 is better on the desk, on a robot or for close readings down to 2 cm; outdoors, in a water tank, a toilet cistern or a bumper, the JSN-SR04T is the one to pick. The wide beam is an advantage for "seeing" a large water surface, but in a narrow well or when mounted close to a wall, reflections from the sides can cause trouble. We've put every option, including laser-based ones, side by side in our distance sensor comparison.
JSN-SR04T wiring: Arduino, ESP32 and Pico
| JSN-SR04T pin | Arduino Uno | ESP32 | Raspberry Pi Pico |
|---|---|---|---|
| 5V | 5V | VIN (USB 5 V) | VBUS (USB 5 V) |
| GND | GND | GND | GND |
| Trig (RX in serial mode) | D9 | GPIO 5 | GP15 |
| Echo (TX in serial mode) | D10 | GPIO 18, through a 1 kΩ / 2 kΩ divider | GP14, through a 1 kΩ / 2 kΩ divider |
Step by step: first reading with Arduino (trigger/echo mode)
- Plug the probe cable into the two-pin socket on the board; if you extend the cable, solder the joints and insulate them with heat-shrink.
- Connect the board's 5V and GND pins to the Arduino's 5V and GND, Trig to D9 and Echo to D10.
- Make sure there's no mode resistor (R27) and no shorted M1/M2 pad on the board; the factory setting is trigger/echo mode.
- Upload the code below, open the Serial Monitor at 9600 baud and point the probe at a wall more than 30 cm away.
- Move the probe slowly away; if the value rises smoothly, your wiring is correct.
1// JSN-SR04T tetik/yankı modu (mod direnci yok) / trigger/echo mode (no mode resistor)
2const int TRIG_PIN = 9; // sensörün Trig (RX) pini / sensor Trig (RX) pin
3const int ECHO_PIN = 10; // sensörün Echo (TX) pini / sensor Echo (TX) pin
4const unsigned long TIMEOUT_US = 40000UL; // ~6,8 m'lik yankı süresi / echo time for ~6.8 m
5
6float readCm() {
7 digitalWrite(TRIG_PIN, LOW);
8 delayMicroseconds(5);
9 digitalWrite(TRIG_PIN, HIGH);
10 delayMicroseconds(20); // en az 20 µs, 10 µs yetmeyebilir / at least 20 µs, 10 µs may fail
11 digitalWrite(TRIG_PIN, LOW);
12 unsigned long t = pulseIn(ECHO_PIN, HIGH, TIMEOUT_US);
13 if (t == 0) return -1; // yankı yok / no echo
14 return t * 0.0343 / 2.0; // 343 m/s (20 °C), gidiş-dönüş / round trip
15}
16
17// 5 ölçümün ortancası: tek tük sıçramaları atar / median of 5 drops the odd spike
18float medianCm() {
19 float v[5];
20 int n = 0;
21 for (int i = 0; i < 5; i++) {
22 float d = readCm();
23 if (d > 0) v[n++] = d;
24 delay(70); // önceki yankı sönsün / let the previous echo die out
25 }
26 if (n == 0) return -1;
27 for (int i = 1; i < n; i++) { // küçükten büyüğe sırala / sort ascending
28 float k = v[i];
29 int j = i - 1;
30 while (j >= 0 && v[j] > k) {
31 v[j + 1] = v[j];
32 j--;
33 }
34 v[j + 1] = k;
35 }
36 return v[n / 2];
37}
38
39void setup() {
40 Serial.begin(9600);
41 pinMode(TRIG_PIN, OUTPUT);
42 pinMode(ECHO_PIN, INPUT);
43}
44
45void loop() {
46 float d = medianCm();
47 if (d < 0) {
48 Serial.println(F("Olcum yok / No reading"));
49 } else {
50 Serial.print(F("Mesafe / Distance: "));
51 Serial.print(d, 1);
52 Serial.println(F(" cm"));
53 }
54 delay(500);
55}The key difference in this code is the 20-microsecond trigger pulse. The 10 µs that's enough for the HC-SR04 fails to start a measurement on many JSN-SR04T boards, or produces random bad readings. pulseIn() returns how long the Echo pin stays HIGH in microseconds; if no echo comes back within 40 ms it returns 0, and the code flags that as "no reading". Taking the median of five readings rejects the odd bad value caused by ripples on the water or an insect far better than an average would.
Mode resistor (R27) and M1/M2 pads: operating modes
The JSN-SR04T board has a small, empty SMD resistor footprint. On version 1.0 and 2.0 boards it's labelled R27; the value of the resistor you solder there decides which mode the board starts in at power-up. Version 3.0 boards do the same job with a MODE pad plus two solder-jumper pads called M1 and M2, and they offer more modes. The exact resistor value isn't critical: the board's processor reads the voltage the resistor creates and picks the mode from it, so a nearby standard value works too. The version is printed on the back of the board.
| Mode | Version 2.0 (R27) | Version 3.0 (MODE pad) | How it works |
|---|---|---|---|
| Trigger/echo (factory default) | Empty | No resistor, M1 and M2 open | Pulse Trig, then time how long Echo stays HIGH |
| Automatic serial | 47 kΩ | M1 shorted or 47 kΩ | Sends a 4-byte frame about every 100 ms at 9600 baud |
| Controlled serial | 120 kΩ | M2 shorted or 120 kΩ | Measures once when 0x55 arrives on RX; at least 60 ms between requests |
| Automatic pulse | None | 200 kΩ | Measures on its own about every 200 ms and outputs a pulse on Echo |
| Low-power trigger | None | 360 kΩ | Like trigger/echo; current drops to microamps between readings |
| Threshold switch | None | 470 kΩ | Echo goes HIGH when the distance is below the threshold (1.5 m by default) |
Reading distance in serial (UART) mode
In the serial modes the same two wires change roles: Trig becomes the sensor's RX input and Echo becomes its TX output. The sensor sends a four-byte frame at 9600 baud: the header 0xFF, the distance high byte, the low byte, and a checksum byte equal to the low 8 bits of the sum of the first three. Distance is in millimetres: high × 256 + low. For example, the frame FF 05 DC E0 means 0x05DC = 1500 mm, or 150 cm. Checking the checksum catches corrupted bytes on long cables. The main advantage of serial mode is that timing happens in the sensor: the Arduino doesn't sit waiting in pulseIn(), so other tasks aren't blocked. You'll find the basics in our UART serial communication guide.
1#include <SoftwareSerial.h>
2
3// Seri mod: Trig pini sensörün RX'i, Echo pini TX'i olur / serial mode: Trig becomes RX, Echo becomes TX
4SoftwareSerial sonar(10, 9); // RX = D10 ← Echo/TX, TX = D9 → Trig/RX
5const bool AUTO_MODE = false; // 47 kΩ (otomatik) için true, 120 kΩ (0x55 ile) için false / true for 47 kΩ, false for 120 kΩ
6
7// 0xFF, yüksek bayt, düşük bayt, toplam: mesafeyi mm döndürür / returns distance in mm
8int readFrameMm(unsigned long timeoutMs) {
9 byte buf[4];
10 byte idx = 0;
11 unsigned long start = millis();
12 while (millis() - start < timeoutMs) {
13 if (!sonar.available()) continue;
14 byte b = sonar.read();
15 if (idx == 0 && b != 0xFF) continue; // başlık baytını bekle / wait for the header byte
16 buf[idx++] = b;
17 if (idx == 4) {
18 byte sum = (byte)(buf[0] + buf[1] + buf[2]); // toplamın düşük baytı / low byte of the sum
19 if (sum != buf[3]) { // bozuk çerçeve, baştan / corrupt frame, restart
20 idx = 0;
21 continue;
22 }
23 return (buf[1] << 8) | buf[2];
24 }
25 }
26 return -1; // cevap yok / no reply
27}
28
29void setup() {
30 Serial.begin(9600);
31 sonar.begin(9600); // sensörün sabit hızı / the sensor's fixed baud rate
32}
33
34void loop() {
35 if (!AUTO_MODE) sonar.write(0x55); // ölçüm iste / request a measurement
36 int mm = readFrameMm(200);
37 if (mm < 0) {
38 Serial.println(F("Cerceve yok / No frame"));
39 } else {
40 Serial.print(F("Mesafe / Distance: "));
41 Serial.print(mm / 10.0, 1);
42 Serial.println(F(" cm"));
43 }
44 if (!AUTO_MODE) delay(200); // iki istek arası en az 60 ms / at least 60 ms between requests
45}Measuring water tank and well level
The sensor is mounted in the tank lid with the probe pointing down, and it measures the distance to the water surface. Water height = probe-to-bottom distance − measured distance. For a cylindrical tank the volume is π × r² × height, for a rectangular tank length × width × height; divide the result in cubic centimetres by 1000 to get litres. The tank in our example has an inner diameter of 100 cm, so each centimetre of water is about 7.85 litres. The most important rule: because of the blind zone, the probe must sit at least 25 cm above the overflow level. If water comes within 20 cm of the probe when the tank is full, the sensor will either report nonsense or not measure at all.
| Measured distance | Water height | Volume | Fill level |
|---|---|---|---|
| 30 cm | 155 − 30 = 125 cm | About 982 L | 96% |
| 80 cm | 155 − 80 = 75 cm | About 589 L | 58% |
| 140 cm | 155 − 140 = 15 cm | About 118 L | 12% |
| Under 20 cm | Blind zone | Invalid | Probe mounted too low |
1// ESP32 + JSN-SR04T: silindir depoda seviye, litre ve doluluk / cylindrical tank level, litres and fill
2const int TRIG_PIN = 5; // 3,3 V tetik çoğu kartta yeterli / 3.3 V trigger works on most boards
3const int ECHO_PIN = 18; // 5 V Echo → 1 kΩ / 2 kΩ bölücü → GPIO 18 / via a 1k/2k divider
4
5const float SENSOR_TO_BOTTOM_CM = 155.0; // prob yüzeyinden depo tabanına / probe face to tank bottom
6const float FULL_LEVEL_CM = 130.0; // taşma seviyesi; prob bunun 25 cm üstünde / overflow level, probe 25 cm above
7const float TANK_DIAMETER_CM = 100.0; // deponun iç çapı / inner diameter of the tank
8
9float readCm() {
10 digitalWrite(TRIG_PIN, LOW);
11 delayMicroseconds(5);
12 digitalWrite(TRIG_PIN, HIGH);
13 delayMicroseconds(20);
14 digitalWrite(TRIG_PIN, LOW);
15 unsigned long t = pulseIn(ECHO_PIN, HIGH, 40000UL);
16 return t ? t * 0.0343f / 2.0f : -1.0f;
17}
18
19// Ortanca filtre: dalga ve köpük kaynaklı sıçramalara karşı / median filter against ripples and foam
20float medianCm(int samples) {
21 float v[9];
22 int n = 0;
23 if (samples > 9) samples = 9;
24 for (int i = 0; i < samples; i++) {
25 float d = readCm();
26 if (d > 0) v[n++] = d;
27 delay(80);
28 }
29 if (n == 0) return -1.0f;
30 for (int i = 1; i < n; i++) {
31 float k = v[i];
32 int j = i - 1;
33 while (j >= 0 && v[j] > k) {
34 v[j + 1] = v[j];
35 j--;
36 }
37 v[j + 1] = k;
38 }
39 return v[n / 2];
40}
41
42void setup() {
43 Serial.begin(115200);
44 pinMode(TRIG_PIN, OUTPUT);
45 pinMode(ECHO_PIN, INPUT);
46}
47
48void loop() {
49 float d = medianCm(7);
50 if (d < 0) {
51 Serial.println("Yanki yok / No echo");
52 delay(1000);
53 return;
54 }
55 float level = constrain(SENSOR_TO_BOTTOM_CM - d, 0.0f, FULL_LEVEL_CM);
56 float r = TANK_DIAMETER_CM / 2.0f;
57 float liters = PI * r * r * level / 1000.0f; // cm³ → litre / litres
58 float percent = level / FULL_LEVEL_CM * 100.0f;
59 Serial.printf("Mesafe/Distance %.1f cm | Seviye/Level %.1f cm | %.0f L | %.0f%%\n",
60 d, level, liters, percent);
61 delay(2000);
62}In the ESP32 code you only need to change three constants for your own tank: probe-to-bottom distance, overflow level and inner diameter. Because the Echo line goes through a 1 kΩ / 2 kΩ divider, GPIO 18 never sees more than 3.3 V; the 3.3 V trigger signal is recognised by most boards, and if yours ignores it, add a logic level converter on the Trig line. Wells are a bit different: if the water is deeper than 4.5–6 m, the ultrasonic burst won't come back, and in a narrow borehole the wide beam bounces off the casing. In wide, hand-dug wells place the probe dead centre, pointing straight down; for deep, narrow wells a submersible pressure level sensor gives more accurate results. Since the speed of sound changes with temperature, readings come out about 3.5% long at 0 °C and about 3% short at 35 °C; for precise work, measure temperature with a DS18B20 and correct the speed of sound as 331.3 + 0.606 × T m/s.
JSN-SR04T with Raspberry Pi Pico and MicroPython
On the Pico, power the sensor from the VBUS pin (the USB's 5 V) and again connect Echo to GP14 through the 1 kΩ / 2 kΩ divider. MicroPython's machine.time_pulse_us() does the same job as pulseIn() on Arduino and returns a negative value if no echo arrives. If you'd rather use serial mode, wire the sensor's TX (through the divider) to GP1 and its RX to GP0, then read the same four-byte frame with UART(0, 9600).
1from machine import Pin, time_pulse_us
2import time
3
4trig = Pin(15, Pin.OUT, value=0) # GP15 → Trig
5echo = Pin(14, Pin.IN) # GP14 ← Echo, 1 kΩ / 2 kΩ bölücüyle / through a 1k/2k divider
6
7def read_cm():
8 trig.value(0)
9 time.sleep_us(5)
10 trig.value(1)
11 time.sleep_us(20) # en az 20 µs tetik / at least a 20 µs trigger
12 trig.value(0)
13 t = time_pulse_us(echo, 1, 40000)
14 if t < 0: # -1 ya da -2: yankı yok / no echo
15 return None
16 return t * 0.0343 / 2
17
18def median_cm(n=5):
19 vals = []
20 for _ in range(n):
21 d = read_cm()
22 if d is not None:
23 vals.append(d)
24 time.sleep_ms(70) # yankılar sönsün / let echoes die out
25 if not vals:
26 return None
27 vals.sort()
28 return vals[len(vals) // 2]
29
30while True:
31 d = median_cm()
32 if d is None:
33 print("Olcum yok / No reading")
34 else:
35 print("Mesafe / Distance: {:.1f} cm".format(d))
36 time.sleep(1)Mounting tips: water, condensation and reflections
- The probe is waterproof, not an underwater sensor: Don't submerge it; sound travels at a different speed in water and the sensor can't measure.
- Put the board in a box: Place the control board in a sealed junction box and close the cable entry with a cable gland or silicone.
- Watch out for condensation: Humidity in the tank can form droplets on the probe face, and droplets create false echoes. Mount the probe face-down, sticking out slightly.
- Keep away from walls: Because of the wide beam, don't mount the probe near the tank edge, a ladder, a float or a pipe; centre it if you can.
- Mount it square: If the probe isn't perpendicular to the surface, the echo is deflected sideways and the sensor may not measure at all.
- Extending cables: The probe-to-board cable carries the raw signal, so extend the cable between the board and the Arduino instead; in serial mode that line works fine over a few metres.
JSN-SR04T project ideas
- Tank level gauge: Show the fill level of a rooftop or basement tank in litres and percent on an OLED display.
- Automatic pump control: Start the pump with a relay when the level drops below 20% and stop it at 95%; add a float level switch as a backup safety.
- Rain barrel monitor: Use an ESP32 to watch the level of the tank you use for garden watering from your phone.
- Waterproof parking sensor: Fit the probe in the bumper and make the buzzer beep faster as the distance shrinks.
- Snow or river level gauge: Hang the probe on a pole pointing down and log snow depth or stream level.
- Overflow alarm: Sound an alert when the water in a basement sump pit passes a set level.
Common problems and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| Always 0 or "no reading" | 10 µs trigger, Trig and Echo swapped, board in serial mode | Use a 20 µs pulse, check the wires, check R27 / M1 / M2 |
| Stuck at about 20–25 cm | Target inside the blind zone or a water drop on the probe face | Raise the probe, dry its face, make sure it isn't tilted |
| Values jump around randomly | Wall or pipe reflections, rippled surface, too short a pause | Centre the probe, use a median filter, wait 70 ms between readings |
| No reading on ESP32 | Echo is 5 V without a divider, or the sensor runs on 3.3 V | Power the sensor from 5 V, add a 1 kΩ / 2 kΩ divider on Echo |
| No frames in serial mode | Wrong mode, RX/TX swapped or baud not 9600 | Check the resistor value, send 0x55 in controlled mode |
| Drops out at long range | Soft target, angled surface or 3.3 V supply | Measure square to a hard, flat surface and power it from 5 V |
The JSN-SR04T is the easiest way to take everything you learned with the HC-SR04 outdoors and next to water: the same formula, the same code logic, but a rugged probe and a serial output option. To refresh the basics of ultrasonic measurement, read our HC-SR04 distance sensor guide. In the BlueGrays app you'll find step-by-step distance measuring, parking sensor and smart irrigation projects with circuit diagrams and code, and you can swap in the JSN-SR04T to build your own waterproof version.
Frequently asked questions
What's the difference between the JSN-SR04T and HC-SR04?
The JSN-SR04T has a waterproof probe on a 2.5 m cable; because it uses a single transducer it has a blind zone of about 20–25 cm, but it reaches 4.5–6 m and can output serial (UART) data. The HC-SR04 starts measuring at 2 cm and has a narrower beam, but it can't handle water or humidity. The code logic is the same; just make the trigger pulse 20 µs for the JSN-SR04T.
Can the JSN-SR04T be submerged in water?
No. The probe is sealed against rain, splashes and heavy humidity, but it isn't designed to measure distance underwater; sound travels at a different speed in water and the sensor can't interpret the echo correctly. The control board isn't waterproof at all and must sit in a dry box.
Does the JSN-SR04T work with 3.3 V and ESP32?
Version 3.0 is rated for 3.0–5.5 V on paper, but at 3.3 V the range shrinks and readings get unstable. The most reliable approach is to power the sensor from the ESP32's VIN (5 V) pin and bring the 5 V Echo output down to 3.3 V with a 1 kΩ / 2 kΩ voltage divider. The ESP32's 3.3 V signal is usually enough on the Trig line.
Why does my JSN-SR04T always read 0 or the same value?
The most common cause is the 10 µs trigger pulse from HC-SR04 code; the JSN-SR04T needs at least 20 µs. If there's a mode resistor on the board, the sensor may be in serial mode and won't respond to the trigger. If you see a constant value around 20–25 cm, the target is inside the blind zone or there's a water drop on the probe face.
What is the minimum distance of the JSN-SR04T?
About 20–25 cm, depending on the version. Because its single transducer keeps ringing briefly after each burst, it can't hear echoes closer than that. In a tank, mount the probe at least 25 cm above the overflow level; if you need closer readings, choose an HC-SR04 or a laser sensor such as the VL53L0X.
How do I measure water tank level with a JSN-SR04T?
Mount the probe in the lid pointing down and measure the probe-to-bottom distance once. Water height is that distance minus the reading; for a cylindrical tank, π × r² × height gives the volume in cubic centimetres, and dividing by 1000 gives litres. A median filter reduces spikes caused by ripples.
Projects to build after reading this
See all projects
HC-SR04 Distance Measurement (Serial Display)
The HC-SR04 sensor works just like a bat finding its way in the dark: it sends out a sound you cannot hear, listens for the echo and measures how far away the object in front of it is.

ESP32 HC-SR04 Ultrasonic Distance Sensor
The HC-SR04 ultrasonic sensor measures distance by timing how long its sound pulse takes to bounce off an object and return (time of flight). In this project the ESP32 sends a 10-microsecond pulse to the TRIG pin and reads the echo time from the ECHO pin with pulseIn(). The distance is shown in cm on a 16x2 I2C LCD. The HC-SR04 runs on 5V, so the ECHO pin needs a voltage divider, or the ESP32's 3.3V GPIO gets damaged.

Parking Sensor with HC-SR04
A mini version of a car's parking sensor: the closer something gets, the faster the beeps.

ESP32 Parking Sensor (HC-SR04 + 5-LED Bar + Buzzer + OLED)
The ESP32 version of a car parking sensor! The HC-SR04 ultrasonic sensor measures the distance to the object in front. As it gets closer, a 5-LED bar fills up (green → yellow → red) and the buzzer beeps faster. Very close (under 10 cm), a continuous alarm sounds and all LEDs light up. The SSD1306 OLED display shows the live distance in cm in large text, plus a status message.

Measuring Distance Using LCD Display
A tool that measures distance without a ruler: move your hand back and forth in front of the sensor and watch the number change.

Height Measurement Project
A height meter that hangs from a door frame and tells you, in centimetres, how tall the person standing under it is.

Smart Irrigation Station
A gardener that waters your plants on its own, so they don't dry out while you're on vacation.
More articles
BlueGrays app
Build this circuit step by step on your phone
Wiring diagram, code and parts list on one screen. 500+ Arduino, ESP32 and Pico projects ready to build.
Scan with your phone
Point your camera at the code and your phone's app store opens.
or open the store

