SR04T Waterproof Ultrasonic Sensor with Arduino — Wiring & Code

Welcome to this beginner-friendly Arduino tutorial on the the sr04t, sr04m waterproof ultrasonic. By the end of the guide, you'll wire the module to an Arduino UNO, flash a short sketch, and read live values on the Serial Monitor — no prior electronics experience required.
Need this built for you? I do custom Arduino, ESP32 and Proteus projects from $15.
What you'll learn
- How the module works in plain language
- The exact parts you need and how to wire them safely
- The full Arduino IDE sketch with comments
- Common issues and how to fix them
 — overview](/uploads/arduino/interfacing-the-sr04t-sr04m-waterproof-ultrasonic-sensor-with-arduino/Arduino-Ultrasonic-Sensor-Module-Project.jpg)
 — wiring diagram](/uploads/arduino/interfacing-the-sr04t-sr04m-waterproof-ultrasonic-sensor-with-arduino/Picture1.jpg)
Arduino code
Open the Arduino IDE, paste the sketch below into a new file, install any libraries the sketch #includes (Tools → Manage Libraries), select your board and COM port, then click Upload.
#include <NewPing.h>
#define TRIGGER_PIN 3
#define ECHO_PIN 2
#define MAX_DISTANCE 400
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
float tempval1;
float tempval2;
int finalval;
void setup() {
Serial.begin(57600);
}
void loop() {
delay(20);
Serial.print("Ping: ");
int iterations = 10;
tempval1=((sonar.ping_median(iterations) / 2) * 0.0343);
if(tempval1-tempval2>60 || tempval1-tempval2<-60)
{
tempval2=(tempval1*0.02 )+ (tempval2*0.98);
}
else
{
tempval2=(tempval1*0.4 )+ (tempval2*0.6);
}
finalval=tempval2;
Serial.print(finalval);
Serial.println("cm");
}
 — reference image](/uploads/arduino/interfacing-the-sr04t-sr04m-waterproof-ultrasonic-sensor-with-arduino/Schematic_UltrasonicModule_2022-07-19.png)
How it works
The sketch initialises serial communication and the the sr04t, sr04m waterproof ultrasonic driver in setup(), then in loop() it samples the sensor at a regular interval and prints the result to the Serial Monitor at 9600 baud. Open the Serial Monitor (Ctrl+Shift+M) after upload to see live readings.
Troubleshooting checklist
- No readings: verify the baud rate in Serial Monitor matches the sketch (usually 9600).
- Garbage characters: wrong baud rate or loose GND wire.
- Library not found: install the exact library referenced in the
#includeline via Library Manager. - Sensor not detected (I²C): run an I²C scanner sketch to confirm the address.
What to build next
Once the basic readout works, try logging values to an SD card, sending them over Wi-Fi with an ESP32, or pushing them to a Blynk IoT dashboard. Pair this module with our simulator round-up to prototype the circuit before soldering.
Stuck on your own build? Send me your Arduino / ESP32 / Proteus project on Fiverr — from $15, you don't pay if it doesn't work.
You may also like
Frequently Asked Questions
Q.How is the SR04T different from the HC-SR04?
The SR04T's transducer is a single sealed waterproof probe connected by a JST cable, and many boards wire it in one-signal mode where Trig and Echo share a line. It is weatherproof but has a larger blind zone (~20–25 cm) and a narrower beam.
Q.Can the SR04T work with the HC-SR04 Arduino code?
In one-signal mode you pulse the shared line for 10 µs, switch it to INPUT, then read the echo with pulseIn(). In two-pin mode the standard HC-SR04 sketch works unchanged — check which mode your module is configured for.
Q.Is the SR04T good for rain tanks and outdoor level sensing?
Yes — that is its main use case. Mount it pointing straight down, keep the ~25 cm blind zone above the maximum water level, and shield the module electronics from direct rain.
Q.Why does my SR04T read 0 or a fixed small value?
Objects inside the blind zone return echoes the module rejects, so it reports a minimum value. Increase the standoff distance, and confirm the JST connector pins match your board's signal pin.
The Engineer Post
Embedded systems engineer and educator. Writes weekly tutorials at EmbedLab to help beginners ship real hardware.
Related projects

Arduino with an Ultrasonic Sensor
Step-by-step Arduino tutorial: wire the ultrasonic to an Arduino UNO, upload the example sketch, and read live values on the Serial Monitor.

Water Level Sensor with Arduino
Step-by-step Arduino tutorial: wire the water level to an Arduino UNO, upload the example sketch, and read live values on the Serial Monitor.

Rain Detection Sensor with Arduino
Step-by-step Arduino tutorial: wire the rain detection to an Arduino UNO, upload the example sketch, and read live values on the Serial Monitor.

Pulse Sensor with Arduino
Step-by-step Arduino tutorial: wire the pulse to an Arduino UNO, upload the example sketch, and read live values on the Serial Monitor.

MQ2 Combustible Gas Sensor with Arduino
Step-by-step Arduino tutorial: wire the mq2 combustible gas to an Arduino UNO, upload the example sketch, and read live values on the Serial Monitor.
