Fiverr gig
$15 — I will help you with your project and simulate it for you
Hire me
🤖 Arduino Projects

Interfacing SR04T/SR04M Waterproof UltraSonic Sensor with Arduino Uno

The Engineer PostMay 29, 20265 min read
[  Interfacing SR04T/SR04M Waterproof UltraSonic Sensor with Arduino Uno](https://circuitdigest.com/microcontroller-projects/interfacing-ultrasonic-sensor-module-with-arduino#) — Arduino UNO tutorial cover image

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.

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
[  Interfacing SR04T/SR04M Waterproof UltraSonic Sensor with Arduino Uno](https://circuitdigest.com/microcontroller-projects/interfacing-ultrasonic-sensor-module-with-arduino#) — overview
[  Interfacing SR04T/SR04M Waterproof UltraSonic Sensor with Arduino Uno](https://circuitdigest.com/microcontroller-projects/interfacing-ultrasonic-sensor-module-with-arduino#) — wiring diagram

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");
}
[  Interfacing SR04T/SR04M Waterproof UltraSonic Sensor with Arduino Uno](https://circuitdigest.com/microcontroller-projects/interfacing-ultrasonic-sensor-module-with-arduino#) — reference image

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 #include line 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.

Frequently Asked Questions

Q.What library do I need for the the sr04t, sr04m waterproof ultrasonic?

Open Arduino IDE → Tools → Manage Libraries, then search for any library named in the sketch's #include lines and install the latest version.

Q.Why does the Serial Monitor show nothing?

The most common cause is a baud-rate mismatch — set the Serial Monitor to 9600 baud (bottom-right dropdown) so it matches Serial.begin(9600) in the code.

Q.Can I use this with an ESP32 instead of Arduino UNO?

Yes. The the sr04t, sr04m waterproof ultrasonic works with any 3.3-5 V microcontroller. Just remap the wiring to ESP32 I/O pins and keep the rest of the sketch the same.

TEP

The Engineer Post

Embedded systems engineer and educator. Writes weekly tutorials at EmbedLab to help beginners ship real hardware.

Related projects

Browse all 50+ projects