How to Measure Distance Using a Waterproof Ultrasonic Sensor and Arduino Nano
Welcome to Engineers Butty Lab BD
Phone: 01888-011500
Address: 31 A/B, Seyed Villa, Jobbar Housing Society, Mazar Road, Mirpur -1, Dhaka.
Introduction
Measuring distance is one of the most common tasks in electronics and automation projects. Ultrasonic sensors provide a simple and reliable way to measure the distance between the sensor and an object without physical contact.
In this tutorial, we will learn how to measure distance using a Waterproof Ultrasonic Sensor (SR04M-2 / JSN-SR04T type) and an Arduino Nano. This project is suitable for students, hobbyists, and beginners who want to learn about distance measurement, water-level monitoring, robotics, and automation systems.
What is a Waterproof Ultrasonic Sensor?
A waterproof ultrasonic sensor uses high-frequency sound waves to measure the distance between the sensor and an object.
The sensor works by:
- Sending an ultrasonic pulse.
- Waiting for the echo to return.
- Measuring the travel time of the sound wave.
- Calculating the distance based on the speed of sound.
Unlike standard HC-SR04 sensors, waterproof ultrasonic sensors can operate in wet, dusty, and outdoor environments.
Applications
- Water tank level monitoring
- Smart parking systems
- Obstacle detection robots
- Industrial automation
- Distance measurement projects
- Liquid level measurement
Components Required
| Component | Quantity |
|---|---|
| Arduino Nano | 1 |
| Waterproof Ultrasonic Sensor (SR04M-2 / JSN-SR04T) | 1 |
| Jumper Wires | As Required |
| USB Cable | 1 |
| Breadboard (Optional) |
1 |
Pin Configuration
Waterproof Ultrasonic Sensor
| Sensor Pin | Arduino Nano |
| 5V | 5V |
| GND | GND |
| RX (Trigger) | D9 |
| TX (Echo) | D10 |
Arduino Code
#define TRIG_PIN 9
#define ECHO_PIN 10
float readDistance()
{
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(5);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(20);
digitalWrite(TRIG_PIN, LOW);
unsigned long duration = pulseIn(ECHO_PIN, HIGH, 30000);
if(duration == 0)
return -1;
return duration * 0.0343 / 2.0;
}
void setup()
{
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop()
{
float sum = 0;
int count = 0;
for(int i = 0; i < 5; i++)
{
float d = readDistance();
if(d > 15 && d < 500)
{
sum += d;
count++;
}
delay(50);
}
if(count > 0)
{
Serial.print("Distance = ");
Serial.print(sum / count);
Serial.println(" cm");
}
else
{
Serial.println("No Valid Reading");
}
delay(200);
}
How the Code Works
Step 1: Trigger Pulse
The Arduino sends a short pulse to the sensor through the RX pin.
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(20);
digitalWrite(TRIG_PIN, LOW);
This pulse instructs the sensor to transmit an ultrasonic wave.
Step 2: Echo Detection
The sensor waits for the reflected sound wave and sends a pulse on the TX pin.
unsigned long duration = pulseIn(ECHO_PIN, HIGH, 30000);
The pulse duration represents the round-trip travel time of the ultrasonic signal.
Step 3: Distance Calculation
distance = duration * 0.0343 / 2;
Where:
- 0.0343 cm/µs = Speed of sound
- Division by 2 compensates for the outgoing and returning path.
Step 4: Noise Filtering
The program takes 5 readings and calculates the average value.
This improves measurement stability and reduces false readings.
Serial Monitor Output
Example output:
Distance = 22.31 cm
Distance = 22.45 cm
Distance = 22.27 cm
Distance = 22.40 cm
Important Notes
Minimum Distance
Most waterproof ultrasonic sensors cannot reliably measure below 20 cm.
Maximum Distance
Typical measuring range:
- 20 cm to 450 cm
Stable Power Supply
For best performance:
- Use a stable 5V supply.
- Add a 100µF capacitor across 5V and GND if measurements are unstable.
Object Surface
Flat and solid surfaces provide the most accurate results.
Soft or irregular surfaces may produce weaker echoes.
Advantages of Waterproof Ultrasonic Sensors
- Waterproof design
- Suitable for outdoor use
- Long-distance measurement
- Easy Arduino interfacing
- Ideal for water-level monitoring
Conclusion
In this project, we successfully measured distance using a Waterproof Ultrasonic Sensor and Arduino Nano. The sensor emits ultrasonic waves, receives the reflected echo, and calculates the distance using the speed of sound. By averaging multiple readings, we can obtain stable and reliable measurements.