Sensor
Sensor
Sensor
com
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.
Learn more
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 my article on the
HC-SR04 (https://www.makerguides.com/hc-sr04-arduino-tutorial/). In this article
the working principles of an ultrasonic distance sensor are explained in much greater
detail.
JSN-SR04T Specifications
Operating voltage 5V
Operating current 30 mA
Quiescent current 5 mA
Frequency 40 kHz
Resolution 2 mm
Mounting hole 18 mm
For more information you can check out the datasheet here.
JSN-SR04T Datasheet
(https://www.makerguides.com/wp-content/uploads/2019/02/JSN-SR04T-
Datasheet.pdf)
Tools and Materials
Parts are available on Amazon via the links below.
Hardware components
Software
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, the JSN-
SR04T-2.0 (https://amzn.to/2FmiZ2G). 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 diagram/schamatic for the JSN-SR04T ultrasonic distance sensor with Arduino.
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-SR04T Arduino
5V 5V
Trig Pin 2
Echo Pin 3
GND GND
You can upload the following example code to your Arduino using the Arduino IDE
(https://www.arduino.cc/en/main/software). Next, I will explain you how the code
works. (This code works for the JSN-SR04T-2.0 too).
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.
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.
7. //Define variables
8. long duration;
9. 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.
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.
NewPing_v1.9.1.zip
(https://www.makerguides.com/wp-
content/uploads/2019/02/NewPing_v1.9.1.zip)
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.
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 (https://hackaday.io/project/21579-affordable-water-
level-measuring-station).
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.
Note that comments are held for moderation in order to prevent spam.
(https://twitter.com/intent/tweet?url=https://www.makerguides.com/jsn-
sr04t-arduino-tutorial/&text=&via=makerguides_com)
(https://www.facebook.com/sharer/sharer.php?
u=https://www.makerguides.com/jsn-sr04t-arduino-tutorial/)
(https://www.linkedin.com/shareArticle?
mini=true&url=https://www.makerguides.com/jsn-sr04t-arduino-tutorial/&title=)
(http://pinterest.com/pin/create/button/?
url=https://www.makerguides.com/jsn-sr04t-arduino-tutorial/)
Beginner
Comments
Paul says
April 24, 2019 at 10:09 am (https://www.makerguides.com/jsn-sr04t-arduino-
tutorial/#comment-26)
Reply
Gustavo says
May 13, 2019 at 3:54 am (https://www.makerguides.com/jsn-sr04t-arduino-
tutorial/#comment-44)
Is it possible to connect a 3 pin oem parking sensor on this board? Or are there an
other board to make that?
Reply
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/
(https://www.makerguides.com/hc-sr04-arduino-tutorial/).
Benne
Reply
Alexey says
May 21, 2019 at 11:41 am (https://www.makerguides.com/jsn-sr04t-arduino-
tutorial/#comment-67)
Hello!
Tell me how to connect 2 sensors JSN-SR04T and how to write a sketch?
Reply
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
Now if you want to read the first sensor you can use sonar.ping_cm() and for the
second sensor sonar2.ping_cm().
Benne
Reply
Tommy says
June 4, 2019 at 9:04 am (https://www.makerguides.com/jsn-sr04t-arduino-
tutorial/#comment-83)
Reply
Hi Tommy,
Good question. A breadboard is definitely not needed, so you can just wire
everything directly from the sensor to the Arduino. I just find a breadboard easy
to use when prototyping. If you only have male-male jumper wires, a breadboard
makes it easier to wire up the sensor without soldering. If you plan to build
something more permanent, I wouldn’t use a breadboard.
Greetings,
Benne
Reply
And would it be possible to write code so the desired distance for the sensor is say,
within 10cm, to try and eliminate oscillation?
Regards
Andre
Reply
Hi Andre,
Reply
Reply
Thank you for the well presented tutorial. It is very helpful. Once I get my project
working, I will let you know what it is.
Reply
Marc says
June 25, 2019 at 12:19 pm (https://www.makerguides.com/jsn-sr04t-arduino-
tutorial/#comment-148)
Hi,
Great tuto it’s very clear thank you! But did you manage to make the function
ping_median of new ping work? I had HCSRO4 and this functions worked perfectly
but now it does not… My issue is that I have to get really precise measurements
(with millimeters) so with the HCRO4 I used 5 captors and I filtered the values but
now I can’t manage to get precise measurements without instability with the JSN
SRO4T V2.
Could you please help me? I feel like I tried everything…
Reply
I haven’t extensively tested the NewPing library with the V2 sensor board
myself, but I have read about similar problems before. It mostly has to do with
the length of the trigger pulse. For the HC-SR04 this can be slightly below 10us
but for the JSN-SR04T V2 it needs to be a bit longer. You can change this in the
library source code (sadly there is no other way). You have to change the file
NewPing.cpp around line 134. Change the delay time from 10us to around 13 or
more microseconds. Tim mentions in this post that you should also add ” #define
ONE_PIN_ENABLED false ” to your sketch. See this post:
https://bitbucket.org/teckel12/arduino-new-ping/issues/41/jsn-sr04t-20-needs-
to-have-longer-high (https://bitbucket.org/teckel12/arduino-new-
ping/issues/41/jsn-sr04t-20-needs-to-have-longer-high)
I am not 100% sure whether or not this solution always works, but I hope it fixes
your problem.
Benne
Reply
Scott Wilson says
June 26, 2019 at 7:16 pm (https://www.makerguides.com/jsn-sr04t-arduino-
tutorial/#comment-156)
In the Conclusion (above) you say “We used one of these sensors and modified it so
it could be used in a narrow tube.” In the link to Hackaday you provide I didn’t find
that information.
Am doing a similar thing in Mexico trying to monitor flood levels in streets, and
water levels in holding tanks. That pesky 45-70 degree “beam” of the ultrasonic
sensor creates problems. The modification to the sensor you mentioned to use a
narrow tube would be extremely beneficial. Thanks!
Reply
Hi Scott,
Thanks for the comment. The answer to your question is buried somewhere in
the project logs on Hackaday. What we did is create a 3d printed funnel/cone
that we mounted in front of the sensor. On the inside of the funnel we placed
some sound absorbing material (in this case we used felt). The funnel has
roughly 4 degree walls and combined with the sound absorbing material, we
could reduce the effective beam angle quite a bit. Basically, just part of the
‘beam’ is absorbed and only a small narrow part of it can exit the funnel.
I think there are some SolidWorks files on Hackaday, but you can quite easily
DIY something together. Hope this helps a bit.
Benne
Reply
Hi Benne! Thanks for the info. I do see a funnel thing in one of the Solidworks
files. Looks like its about 60mm long and shaped roughly 4 degrees. That
must be it. I will do up something like it in Sketchup and 3D print it to
experiment. Thanks so much!
Reply
Your article provided very useful information for a planned disaster mitigation project
in my country. We have a lot of mining companies who have the so called mine
tailings dam whose water levels can rise to dangerously high levels during the rainy
season.. Several environmental catastrophes have occurred when the dammed
water breached the dikes. A simple water level monitoring instrument employing the
jsn-sr04t sensor with arduino and gsm modem will likely prevent such a disaster by
alerting the mining company’s personnel to open the sluice valves to relieve the
water pressure. We are currently in a discussion on how best to implement the
project.
Reply
Trackbacks