Skip to Content

Waterproof JSN-SR04T Ultrasonic Distance Sensor with Arduino Tutorial

Waterproof JSN-SR04T Ultrasonic Distance Sensor with Arduino Tutorial

The JSN-SR04T is an easy to use waterproof ultrasonic distance sensor with a range of 25 to 450 cm. If you are planning to build a water level measuring system or if you need to take other distance measurements outside, then this is the sensor you need!

In this article, I have included a wiring diagram and example codes so you can start experimenting with your sensor. After each example, I break down and explain how the code works, so you should have no problems modifying it to suit your needs.

First, we will look at an example that doesn’t use an Arduino library. Next, I will cover the easy to use NewPing library that has some nice built-in features. In the last, example I will show you how to display the measured distances on a 4-Digit-Display.

But let’s get started with the parts you will need to build this project.

Required parts

Below you will find the parts required for this project. When looking for the JSN-SR04T, you might come across the updated version, the JSN-SR04T-2.0. This newer version works exactly the same but is rated for 3-5 V instead of 5 V. However, some users have found issues while using the sensors at a lower voltage. Using a longer trigger pulse of at least 20 µs instead of 10 µs seems to help if you are having faulty readings.

Arduino Uno

USB Data Sync cable Arduino

USB Cable for Arduino UNO

Dupont wire set

Dupont Wire Set

Half_breadboard56a

Breadboard

JSN-SR04T Ultrasonic Sensor Module

JSN-SR04T Sensor

 4-Digit-Display TM1637

Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com. As an Amazon Associate we earn from qualifying purchases.

About the JSN-SR04T Sensor

The sensor comes with a 2.5 m long cable that connects to a breakout board which controls the sensor and does all the processing of the signal. Note that only the sensor and the cable itself are waterproof. Don’t immerse the breakout board itself in water!

An ultrasonic distance sensor works by sending out ultrasound waves. These ultrasound waves get reflected back by an object and the ultrasonic sensor detects them. By timing how much time passed between sending and receiving the sound waves, you can calculate the distance between the sensor and an object.

Sound impulse and its reflection to measure distance
Sound impulse and its reflection to measure distance

Pinout of JSN-SR04T

The breakout board of of JSN-SR04T the has four pins. 5V and GND for the power supply, Trig to trigger sending the ultrasound impulse and the output Echo, which returns a pulse that is proportional in length to the measured distance. The picture below shows the pinout.

Pinout for JSN-SR04T board
Pinout for JSN-SR04T board

Function of JSN-SR04T

To operate the sensor, you have the set Trig to HIGH for at least 10 microseconds. This will cause the board to send eight ultrasonic impulses (sonic burst).

Timing diagram of JSN-SR04T from datasheet
Timing diagram of JSN-SR04T from datasheet

If a reflected sound (echo) is received the Echo output will go HIGH level and the distance can be calculated as

distance = high level time * 340 m/s / 2

where 340m/s is the speed of sound. We will use this formula later in our code but since we are going to measure the distance in cm, the constant will be 0.034cm/s.

Note that ultrasonic sensors are not like lasers with a focused beam. Instead their directivity is shaped like a bubble and also requires a sufficiently large and hard/reflective surface for accurate distance measurements.

Directivity of JSN-SR04T (source)

In case of the JSN-SR04T, the target should be at least 0.5 square metres in size and you want to point the sensor perpendicular to the target. The table below shows the specification of the sensor but be beware that measuring range and resolution assume ideal conditions.

JSN-SR04T Specifications

Operating voltage5 V
Operating current30 mA
Quiescent current5 mA
Frequency40 kHz
Measuring range25-450 cm
Resolution2 mm
Measuring angle45-75 degrees
Sensor dimensions23.5 x 20 mm, 2.5 m long cable
PCB dimensions41 x 28.5 mm
Mounting hole18 mm

For more information you can check out the datasheet here.

JSN-SR04T vs HC-SR04

What are the differences between this sensor and the HC-SR04? The main difference, besides it being waterproof, is that this sensor uses only one ultrasonic transducer instead of two. This transducer serves as both the transmitter and the receiver of the ultrasound waves.

Waterproof JSN-SR04T ultrasonic distance sensor with Arduino Tutorial
The Transducer of the JSN-SR04T

For more info on how ultrasonic sensors work, you can check out my article on the HC-SR04. In this article the working principles of an ultrasonic distance sensor are explained in greater detail.

Wiring – JSN-SR04T to Arduino

The wiring diagram/schematic below shows you how to connect the JSN-SR04T sensor to the Arduino. The breakout board of the JSN-SR04T has the exact same pinout as the HC-SR04, so it can be used as a drop-in replacement. The cable of the sensor itself can be plugged into the connector on the back of the breakout board.

Wiring diagram/schematic for the JSN-SR04T ultrasonic distance sensor with Arduino
Wiring diagram/schematic for the JSN-SR04T ultrasonic distance sensor with Arduino

The code examples below use digital pin 12 and 11 for the trigger and echo pin, but of course you can change this to any digital pin you want.

5 V5 V
TrigPin 12
EchoPin 11
GNDGND

Example code for JSN-SR04T sensor with Arduino

Now that you have wired up the sensor it is time to connect the Arduino to the computer and upload some code. The sensor can be used without an Arduino library. Later I will show you an example with the NewPing library, which makes the code a lot shorter.

Upload the following example code to your Arduino using the Arduino IDE. This code works for the JSN-SR04T-2.0 too. In the following sections, I will explain how the code works in detail.

const int trigPin = 12;
const int echoPin = 11;

void setup() {
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  long duration = pulseIn(echoPin, HIGH);
  long distance = duration * 0.034 / 2;

  Serial.print("Distance:");
  Serial.println(distance);
  delay(100);
}

If you run the code and point the sensor to objects in different distances, you should varying distances (in cm) appear on the Serial Monitor. Don’t forget to set the baud rate to 115200 for this example!

Serial Monitor showing measured distances by JSN-SR04T in cm.
Serial Monitor showing measured distances in cm

You can also open the Serial plotter and should see a wavy line if you move the sensor around to measure various distances.

Serial Plotter showing measured distances in cm.
Serial Plotter showing measured distances in cm

How the code works

First, the trigger pin and the echo pin are defined. I call them trigPin and echoPin. The trigger pin is connected to digital pin 12 and the echo pin to digital pin11 on the Arduino. The statement const is used to give a name to a constant value.

const int trigPin = 12;
const int echoPin = 11;

In the setup(), you start by initializing the serial communication at a baud rate of 115200. To display the measured distance in the serial monitor, press Ctrl+Shift+M or Tools > Serial Monitor. Make sure the baud rate is also set to 115200 in the serial monitor.

Next, we are setting the trigPin as an output and the echoPin as an input.

void setup() {
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

In the loop(), you trigger the sensor by setting the trigPin HIGH for 20 µs. Note that to get a clean signal you start by clearing the trigPin by setting it LOW for 5 microseconds.

void loop() {
  digitalWrite(trigPin, LOW);  
  delayMicroseconds(5);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

Next, you need to read the duration of the pulse sent by the echoPin. I use the function pulseIn() for this. This function waits for the pin to go from LOW to HIGH, starts timing, then waits for the pin to go LOW and stops timing.

After that, you can calculate the distance by using the formula mentioned in the introduction of this tutorial. Note that we use a constant of 0.034 for the speed of sound (instead of 340), since we want the distance in centimetres and not in meters.

  long duration = pulseIn(echoPin, HIGH);
  long distance = duration * 0.034 / 2;

Finally, print the calculated distance in the serial monitor and also show in in the plotter. The delay of 100 milliseconds at the end slows the measurements down to a reasonable rate.

  Serial.print("Distance:");
  Serial.println(distance);
  delay(100);

Example code JSN-SR04T with Arduino and NewPing library

The NewPing library written by Tim Eckel can be used with many ultrasonic distance sensors. The latest version of this library can be downloaded here on bitbucket.org. You might notice that the code below, which uses the NewPing library, is a lot shorter than the code we used before.

You can install the library by going to Sketch > Include Library > Add .ZIP Library in the Arduino IDE.

The library does include some examples that you can use, but you will have to modify them to match your hardware setup. I have included a modified example code below that can be used with the same wiring setup as before.

#include "newping.h"

const int trigPin = 12;
const int echoPin = 11;
const int maxDist = 450;

NewPing sonar(trigPin, echoPin, maxDist);

void setup() {
  Serial.begin(115200);
}

void loop() {
  // Wait 50ms between pings (about 20 pings/sec)
  delay(50); 

  long distance = sonar.ping_cm();

  Serial.print("Distance:");
  Serial.println(distance);
  delay(100);
}

Example: JSN-SR04T with Arduino and 4-Digit-Display

As a last example, I want to show you how to display the measured distances on a 4-Digit 7-Segment Display. The display, listed under required parts, has a TM1637 driver, which makes it very easy to connect and to control. Let’s start with the wiring of this display in addition to the sensor.

Wiring – JSN-SR04T to Arduino and 4-Digit Display

We will wire the JSN-SR04T sensor the same as before but use the breadboard in addition to distribute the power to the sensor and the display.

Wiring of JSN-SR04T with Arduino and 4-Digit Display
Wiring of JSN-SR04T with Arduino and 4-Digit Display

The display receives its power (VSS, GND) from the breadboard and has two extra pins that you need to connect to the Arduino. Connect the CLK (Clock) pin to pin 3 of the Arduino (white wire) and the DIO (Digital IO) to pin 4 of the Arduino (gray wire). And that completes the wiring.

Now, let’s have a look at the code.

Code – JSN-SR04T with Arduino and 4-Digit Display

We keep the code relating to the JSN-SR04T sensor as it is. As before we use the NewPing library, initialized with the same pins, and read distances via sonar.ping_cm().

#include "NewPing.h"
#include "TM1637Display.h"

// Display
const int clkPin = 3;
const int dioPin = 4;

// Sonar
const int trigPin = 12;
const int echoPin = 11;
const int maxDist = 450;

NewPing sonar(trigPin, echoPin, maxDist);
TM1637Display display = TM1637Display(clkPin, dioPin);

void setup() {
  Serial.begin(115200);
  display.setBrightness(5);
}

void loop() {
  long distance = sonar.ping_cm();
  Serial.print("Distance:");
  Serial.println(distance);
  display.showNumberDec(distance);
  delay(200);
}

What is new, is that we create the display TM1637Display object. It is initialized with the clkPin and dioPin constants, which are the pins we connected the display to.

In the setup() function, we set the brightness of the display, with 0 being the lowest brightness and being 7 the highest brightness. This command also turns the display on.

Finally, in the loop() function, we simply display the measured distance by calling display.showNumberDec(distance). And that’s it. Now you have an ultrasonic distance measure that displays distances in centimetres on a 4-digit display.

JSN-SR04T with Arduino and 4-Digit Display
JSN-SR04T with Arduino and 4-Digit Display

Conclusion

In this article, I have shown you how the JSN-SR04T ultrasonic distance sensor works and how you can use it with Arduino. I hope you found it useful and informative. If you did, please share it with a friend that also likes electronics!

I would love to know what projects you plan on building (or have already built) with this sensor. If you have any questions, suggestions, or if you think that things are missing in this tutorial, please leave a comment down below.

Links

If you would like to learn more about other distance sensors, then the articles below might be useful:

ARJUN M

Wednesday 24th of May 2023

Hello, Iam working with this sensor for my project water level indicator , Iam struggling to make this sensor beam width as linear manner ,so i f any know about this idea please help out of this. And also one more problem that iam facing iam trying to visualizing the ultrasonic sensor path.....

Angel Pedroza

Thursday 18th of August 2022

Una muy buena forma de explicación. Realmente comentas cada código y al final la comparativa que realizas es realmente muy buena. Gracias por tomarte el tiempo de escribirlo. Muchas felicidades.

Saludos desde Aguascalientes, México

My Site

Saturday 23rd of July 2022

Such great website

Amazing blog thanks for sharing today on this blog

Steve

Sunday 20th of March 2022

Hi, thanks for the great details. I have been able to configure this to measure snowfall. I am getting accurate readings when there is no snow and I am able to get an accurate reading after the snow is done, and therefore can easily determine how much snow has fallen by subtracting the 2. What I am struggling with now, is when the snow is falling - my readings are "WILD AND CRAZY". I am guessing the cause is the sensor is reflecting whatever snow flakes are under the sensor based on each reading. I am guessing there is not way to resolve this, but wanted to see if anyone had any thoughts for dealing with this? Appreciate any and all suggestions.

Bhaaskar Rajarajan

Friday 18th of June 2021

Hi

Very good explanation.

1) I like to know how to use this sensor with three wire system echo and trigger pin in same I/O Is any component needs to connected. I read somewhere in web. 2K2 resistor to be connected between Echo and Trigger pin. If so in which pin I/O to be connected

2) As you told, the sensor to be used in Narrow tube for measuring in water tank, what is the size of the tube (inches)

kindly looking for your reply

Thanks and Regards

Bhaaskar R