I want to try out JSN-SR04T, which is an ultrasonic distance measuring module, that is also supposedly water proof. The main reason that i want to try this module over the HC-SR04, is because the JSN-SR04T is waterproof, which is what I need. Unfortunately, I am not sure how to use it on a Raspberry Pi. I am not sure how to connect it, and how to program it using Python. If someone is able to help me, on how to properly connect and program this module using python, on the Raspberry Pi, please post below, and this will help me a lot.

More Information and specifications about the module can be found here :




Thank You!

  • Your program says you are connected to 14 and 15. But your diagram shows echo 8, trigger 10. ``` TRIG = 15 #Associate pin 15 to TRIG ECHO = 14 #Associate pin 14 to Echo ``` Please confirm By the way I liked the way you put timeout :)
    – Sarathy
    Commented Oct 31, 2019 at 3:55
  • If someone else wonders about the discrepancy. Pin-Header pin 8/10 are the GPIO pin 14/15. see pinout.xyz/pinout/pin10_gpio15
    – SubOptimal
    Commented Mar 14, 2021 at 22:30

3 Answers 3


It has exactly the same programming interface as the HC-SR04.

You send a trigger pulse of 10µs or more on the trigger line.

Shortly after the trigger line goes low the echo line will go high. The echo line will stay high until the echo has returned (or the echo times out). The echo line high time is the time the sound took to travel to the detected object and back.

Sonar trigger and echo

The distance in centimetres may be calculated as

distance = (echo_high_time_in_µs / 1000000.0) * 17015

  • Ok, so it has the exact same connections to the RPi as the SR04 sensor ? And the programming is pretty much the same except for the distance formula, right ? Commented Jul 29, 2015 at 22:00
  • @ViktorRaspberry Yes, you can treat it as a drop in replacement for the HC-SR04. The distance calculation will be the same as well. You will see slight variations in the distance calculations depending on whether factors such as temperature and humidity are taken into account (the speed of sound varies).
    – joan
    Commented Jul 29, 2015 at 22:12
  • Thank You so much for your answer, it really helped me understand. I have a quick question. I am planning to use this underwater, and do you think there will be some differences ? I know that the speed of sound in water is about 5 times faster than in air, and in water there is a lot of interference, due to waves and reflections and refractions, its like an extremely dense air. What are your thoughts on using this sensor underwater ? Do you think it will work effectively ? I also believe that the distance formula might change if the sensor is used underwater, but I might be mistaken. Commented Jul 29, 2015 at 22:37
  • 3
    @ViktorRaspberry I do not think it will work underwater. The (40kHz) chirps will be seriously attenutated. I suspect you will have to spend a lot more to get an underwater sonar. The speed of sound in water is roughly 1482 m/s rather than 340 m/s in air. So any distances would have to be multiplied by 1482/340 compared to air calculations.
    – joan
    Commented Jul 30, 2015 at 7:42
  • I undertand. Do you think this sensor will be good for underwater ? adafruit.com/product/1137 Commented Jul 30, 2015 at 23:59

I wanted to add an improved version of the code which uses timeouts to make sure the script doesn't hang:

import RPi.GPIO as GPIO
import os
import time

# Define GPIO to use on Pi

TRIGGER_TIME = 0.00001
MAX_TIME = 0.004  # max time waiting for response in case something is missed
GPIO.setup(GPIO_ECHO, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # Echo

GPIO.output(GPIO_TRIGGER, False)

# This function measures a distance

def measure():
    # Pulse the trigger/echo line to initiate a measurement
    GPIO.output(GPIO_TRIGGER, True)
    GPIO.output(GPIO_TRIGGER, False)

    # ensure start time is set in case of very quick return
    start = time.time()
    timeout = start + MAX_TIME

    # set line to input to check for start of echo response
    while GPIO.input(GPIO_ECHO) == 0 and start <= timeout:
        start = time.time()

    if(start > timeout):
        return -1

    stop = time.time()
    timeout = stop + MAX_TIME
    # Wait for end of echo response
    while GPIO.input(GPIO_ECHO) == 1 and stop <= timeout:
        stop = time.time()

    if(stop <= timeout):
        elapsed = stop-start
        distance = float(elapsed * 34300)/2.0
        return -1
    return distance

if __name__ == '__main__':
        while True:
            distance = measure()
            if(distance > -1):
                print("Measured Distance = %.1f cm" % distance)
        # Reset by pressing CTRL + C
    except KeyboardInterrupt:
        print("Measurement stopped by User")

The JSN-SR04T can be used pretty easily with the Raspberry Pi. The only really important thing to remember is that the electronics module is expecting to signal a time corresponding to distance using a 5V pulse. The Pi's GPIO pins are only designed to accept a 3.3V input, so we need to step the voltage down with a voltage divider.

enter image description here

Connection: For the sake of keeping the connecting pins together on the Pi’s GPIO block I have preferred to connect as follows;

  • Pin 4 connects directly to the +5V connector
  • Pin 6 Connects directly to the GND connector, but is incorporated into the voltage divider for the Echo pin.
  • Pin 8 (GPIO 14 TXD) connects to the centre of the voltage divider
  • Pin 10 (GPIO 15 RXD) connects directly to the Trig connector.

enter image description here

The following python script will get you started. save it as something (e.g. distance.py) and then run it by typing python distance.py at the command line.


import RPi.GPIO as GPIO                    #Import GPIO library
import time                                #Import time library
GPIO.setmode(GPIO.BCM)                     #Set GPIO pin numbering 

TRIG = 15                                  #Associate pin 15 to TRIG
ECHO = 14                                  #Associate pin 14 to Echo

print "Distance measurement in progress"

GPIO.setup(TRIG,GPIO.OUT)                  #Set pin as GPIO out
GPIO.setup(ECHO,GPIO.IN)                   #Set pin as GPIO in

while True:

  GPIO.output(TRIG, False)                 #Set TRIG as LOW
  print "Waiting For Sensor To Settle"
  time.sleep(2)                            #Delay of 2 seconds

  GPIO.output(TRIG, True)                  #Set TRIG as HIGH
  time.sleep(0.00001)                      #Delay of 0.00001 seconds
  GPIO.output(TRIG, False)                 #Set TRIG as LOW

  while GPIO.input(ECHO)==0:               #Check if Echo is LOW
    pulse_start = time.time()              #Time of the last  LOW pulse

  while GPIO.input(ECHO)==1:               #Check whether Echo is HIGH
    pulse_end = time.time()                #Time of the last HIGH pulse 

  pulse_duration = pulse_end - pulse_start #pulse duration to a variable

  distance = pulse_duration * 17150        #Calculate distance
  distance = round(distance, 2)            #Round to two decimal points

  if distance > 20 and distance < 400:     #Is distance within range
    print "Distance:",distance - 0.5,"cm"  #Distance with calibration
    print "Out Of Range"                   #display out of range

When executed the sensor will settle and then start displaying the distance every two seconds.

Waitng For Sensor To Settle
Distance: 53.44 cm
Waitng For Sensor To Settle
Distance: 52.95 cm

There is more information on operation, connection and code here.

  • This answer helped me a lot, but I run into 2 problems with the above script: 1) unless the air conditions are known, calculating an accurate distance requires taking into account the air temperature, dampness and barometric pressure 2) raspbian is not a real time OS and wait loops lead to erratic and imprecise readings. Better to rely on a library that leverages HW interrupts and uses callbacks providing microsecond-precision timestamps like pigpio (abyz.me.uk/rpi/pigpio/python.html). A working code example is here abyz.me.uk/rpi/pigpio/code/sonar_trigger_echo_py.zip
    – Sergio
    Commented Sep 1, 2019 at 22:33
  • @Sergio is correct. The method is not appropriate for precision measurement.
    – d3noob
    Commented Sep 2, 2019 at 18:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.