Waterproof JSN-SR04T Ultrasonic Distance Sensor with Arduino Tutorial

JSN-SR04T

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!

I have included a wiring diagram and example codes so you can start experimenting with your sensor.

What this article covers

Information about the sensor
Tools and Materials
Wiring – Connecting JSN-SR04T to Arduino UNO
Example code for JSN-SR04T with Arduino
How the code works
Example code JSN-SR04T with Arduino and NewPing library

If you need a sensor with a shorter range or an IR sensor that isn’t influenced by temperature changes, the following articles might be helpful.

Other distance/proximity sensors

Information about the 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, if you get water onto the breakout board, bad things will happen. 

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.

Distance (cm) = Speed of sound (cm/µs) × Time (µs) / 2

Where Time is the time between sending and receiving the sound waves in microseconds.

So 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. This transducer serves as both the transmitter and the receiver of the ultrasound waves. 

For more info on how ultrasonic sensors work, you can check out the article on the HC-SR04 below. In this article the working principle is explained in much greater detail.

JSN-SR04T Specifications

JSN-SR04TSpecifications
Operating voltage5V
Operating current30mA
Quiescent current5mA
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.

Tools and Materials

You can find the tools and materials that are used in this tutorial in the links below. Or click the markers in the image for more info and prices on Amazon.

Components

*Note: the above links are affiliated which means – at no additional cost to you – if you purchase anything using them you’re helping to support my work on this site. My humble thanks (I really appreciate it)! 

When shopping for the sensor, you might find the updated version, JSN-SR04T-2.0. This newer version works exactly the same, but some users have found issues while using the sensors at a lower voltage. This newer version is rated for 3–5 V. 

Using a longer trigger puls of at least 20 µs instead of 10µs seems to help if you are having faulty readings.

Wiring - Connecting JSN-SR04T to Arduino UNO

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.

JSN-SR04T with Arduino wiring diagram

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

JSN-SR04T Connections

JSN-SR04TArduino
5V5V
TrigPin 2
EchoPin 3
GNDGND

Example code for JSN-SR04T 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.

You can upload the following example code to your Arduino using the Arduino IDE. Next, I will explain you how the code works. (This code works for the JSN-SR04T-2.0 too).

/* JSN-SR04T Ultrasonic Distance Sensor with Arduino Example Code. More info: https://www.makerguides.com */

//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
Serial.begin(9600); // Starts the serial communication at a baudrate of 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);
}

You should see the following output in the serial monitor:

JSN-SR04T Serial monitor output

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 2 and the echo pin to digital pin 3 on the Arduino.

The statement #define is used to give a name to a constant value. The compiler will replace any references to this constant with the defined value when the the program is compiled. So everywhere you mention trigPin, the compiler will replace it with the value 2 when the program is compiled.

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

Next I defined two variables: duration and distance. Duration stores the time between sending and receiving the sound waves. The distance variable is used to store the calculated distance.

//Define variables
long duration;
int distance;

In the setup(), you start by setting the trigPin as an output and the echoPin as an input. Next you initialize serial communication at a baud rate of 9600. Later you will display the measured distance in the serial monitor, which can be accessed with Ctrl+Shift+M or Tools > Serial Monitor. Make sure the baud rate is also set to 9600 in the serial monitor.

void setup()
{
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

//Begin Serial communication
Serial.begin(9600); // Starts the serial communication at a baud rate of 9600
}

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() 
{
//Clear the trigPin by setting it LOW
digitalWrite(trigPin, LOW);
delayMicroseconds(5);

//Trigger the sensor by setting the trigPin high for 20 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(20);
digitalWrite(trigPin, LOW);

Next, you need to read the length 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.

//Read the echoPin. This returns the duration (length of the pulse) in microseconds.
duration = pulseIn(echoPin, HIGH);

//Calculate the distance
distance= duration*0.034/2;

Finally, print the calculated distance in the serial monitor.

// Print the distance on the Serial Monitor (Ctrl+Shift+M)
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
delay(50);
}

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.

/* JSN-SR04T Ultrasonic Distance Sensor with NewPing library Example Code, More info: https://www.makerguides.com*/

#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-500cm.
#define MAX_DISTANCE 400

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

void setup()
{
Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results.
}

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

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!

Personal project: About a year ago I was working on a waterlevel measuring station for developing countries as part of a university project. We used one of these sensors and modified it so it could be used in a narrow tube. You can read more about it here on Hackaday.

I would love to know what projects you plan on building (or have already built) with this sensor. Please leave a comment down below and share your ideas.

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email
Share on whatsapp

Beginner

Creative Commons License

If you have any questions, suggestions or if you think that things are missing in this tutorial, please leave a comment down below. Note that comments are held for moderation in order to prevent spam.

5 Responses

  1. Is it possible to connect a 3 pin oem parking sensor on this board? Or are there an other board to make that?

    1. Hi,

      The sensor/ultrasonic transducer that connects to the control board only has two wires/pins. If your oem parking sensor comes with it’s own pcb that creates the signal, then you might be able to control it with an Arduino or other microcontroller. I have written a small section about controlling 3 pin sensors with the NewPing library in my tutorial about the HC-SR04 ultrasonic sensor: https://www.makerguides.com/hc-sr04-arduino-tutorial/.

      Benne

    1. This can easily be done with the NewPing library. Just create two instances of the NewPing class and add an additional trigger and echo pin:

      #define trigPin 2
      #define echoPin 3
      #define trigPin2 4
      #define echoPin2 5

      NewPing sonar(trigPin, echoPin, MAX_DISTANCE);
      NewPing sonar2(trigPin2, echoPin2, MAX_DISTANCE);

      Now if you want to read the first sensor you can use sonar.ping_cm() and for the second sensor sonar2.ping_cm().

      Hope this helps.

      Benne

Leave a Reply

Your email address will not be published. Required fields are marked *