Interfacing Waterproof JSN-SR04T Ultrasonic Distance Sensor with Arduino

2,526 views

Introduction

The use of distance sensors in robotics and automation has revolutionized the way we interact with the world around us. The JSN-SR04T ultrasonic distance sensor is an innovative device that can detect distances accurately up to 4 meters. It is waterproof, making it ideal for use in harsh environments such as marine applications, automotive systems, and industrial automation.

In this article, we will explore how to interface the “JSN-SR04T Ultrasonic Distance Sensor with an Arduino to create an efficient and reliable distance measurement system.

What is Waterproof JSN-SR04T Ultrasonic Distance Sensor?

The Waterproof JSN-SR04T Ultrasonic Distance Sensor is a distance-measuring device that uses ultrasonic sound waves to detect the distance between the sensor and an object. It can measure distances accurately up to 4 meters and is waterproof, making it suitable for use in harsh environments such as marine applications, automotive systems, and industrial automation. The sensor works by emitting a high-frequency sound wave, which bounces off the object and returns to the sensor.

Hardware Components

To interface a JSN-SR04T Ultrasonic Distance Sensor with Arduino, you’ll need the following hardware components to get started:

ComponentsValueQty
Arduino UNO1
USB Cable Type A to B1
DC Power for Arduino1
Ultrasonic Distance SensorJSN-SR04T 1
Breadboard1
Jumper Wires1

Waterproof JSN-SR04T Ultrasonic Distance Sensor Pinout

S. NO.Pin NamePin Description
15VPositive supply pin of the sensor
2TrigInput pin of sensor. This pin has to be kept high for 10us to initialize measurement by sending ultrasonic waves.
3EchoOutput pin sensor. This pin goes high for a period that will be equal to the time taken for the ultrasonic wave to return to the sensor.
4GNDThis pin is connected to the Ground of the system.

Waterproof JSN-SR04T Ultrasonic Distance Sensor Circuit

Make connections according to the circuit diagram given below.

Wiring / Connections

ArduinoWaterproof Ultrasonic Distance
5VVCC
GNDGND
D2Trig
D3Echo

Installing Arduino IDE

First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.

Installing Libraries

Before you start uploading a code, download and unzip the following libraries at /Program Files(x86)/Arduino/Libraries (default), in order to use the sensor with the Arduino board. Here is a simple step-by-step guide on “How to Add Libraries in Arduino IDE“.

Code 1

Now copy the following code and upload it to Arduino IDE Software.

// Define Trig and Echo pin:
#define trigPin 2
#define echoPin 3

// Define variables:
long duration;
int distance;

void setup() {
  // Define inputs and outputs
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  // Begin Serial communication at a baudrate of 9600:
  Serial.begin(9600);
}

void loop() {
  // Clear the trigPin by setting it LOW:
  digitalWrite(trigPin, LOW);
  
  delayMicroseconds(5);

 // Trigger the sensor by setting the trigPin high for 10 microseconds:
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Read the echoPin. pulseIn() returns the duration (length of the pulse) in microseconds:
  duration = pulseIn(echoPin, HIGH);
  
  // Calculate the distance:
  distance = duration*0.034/2;
  
  // Print the distance on the Serial Monitor (Ctrl+Shift+M):
  Serial.print("Distance = ");
  Serial.print(distance);
  Serial.println(" cm");
  
  delay(100);
}

Code Explanation

This Arduino code sets up the ultrasonic distance sensor to measure the distance between the sensor and an object. It first defines the trig and echo pins used by the sensor as digital pins 2 and 3 respectively.

The code then sets up the pins as either input or output, with the trig pin as output and the echo pin as input. The serial communication is then initialized at a baud rate of 9600 to enable the distance measurement to be displayed on the Serial Monitor.

In the loop() function, the trig pin is cleared by setting it LOW and then delayed for 5 microseconds. The sensor is triggered by setting the trig pin HIGH for 10 microseconds and then LOW.

The pulseIn() function reads the duration of the pulse from the echo pin in microseconds, which is then used to calculate the distance between the sensor and the object. The distance calculation uses the speed of sound in air, which is approximately 0.034 cm/microsecond, and the time taken for the sound wave to travel to and from the object.

The distance measurement is printed on the Serial Monitor with a delay of 100 milliseconds between measurements. This code provides a simple and efficient way to interface the waterproof JSN-SR04T ultrasonic distance sensor with an Arduino board.

Code 2

// Include the library:
#include <NewPing.h>

// Define Trig and Echo pin:
#define trigPin 2
#define echoPin 3

// Define maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500 cm:
#define MAX_DISTANCE 400

// NewPing setup of pins and maximum distance.
NewPing sonar = NewPing(trigPin, echoPin, MAX_DISTANCE);

void setup() {
  // Open the Serial Monitor at 9600 baudrate to see ping results:
  Serial.begin(9600);
}

void loop() {
  // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings:
  delay(50); 
  
  // Measure distance and print to the Serial Monitor:
  Serial.print("Distance = ");
  // Send ping, get distance in cm and print result (0 = outside set distance range):
  Serial.print(sonar.ping_cm()); 
  Serial.println(" cm");
}

Applications

  • Marine applications for measuring water depth or distance from the boat to other objects
  • Automotive systems for obstacle detection and parking assist
  • Industrial automation for measuring the distance between objects on a conveyor belt or in a manufacturing process
  • Home automation for measuring distance for security systems or object detection in smart homes
  • Robotics for obstacle detection and autonomous navigation
  • Agriculture for measuring the distance between crops and irrigation systems
  • Aerospace for measuring the distance between the aircraft and the runway or other obstacles during landing or takeoff.

Conclusion

The JSN-SR04T ultrasonic distance sensor provides an accurate and reliable way of measuring distances in harsh environments. By interfacing it with an Arduino, we can create an efficient distance measurement system that can be used in various applications.