7

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 :

http://www.ebay.ca/itm/Ultrasonic-Module-Distance-Measuring-Transducer-Sensor-Perfect-Waterproof-/141728581416?hash=item20ffae8728

and

http://www.icstation.com/ultrasonic-module-sr04t-distance-measuring-transducer-sensor-p-5046.html

Thank You!

Viktor Raspberry
  • 425
  • 2
  • 5
  • 9

3 Answers3

4

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

joan
  • 71,852
  • 5
  • 76
  • 108
4

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.

 #!/usr/bin/python
#encoding:utf-8

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 else: 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.

Danilo
  • 3
  • 3
d3noob
  • 1,926
  • 1
  • 15
  • 15
3

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
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO_TRIGGER = 23
GPIO_ECHO = 24

TRIGGER_TIME = 0.00001
MAX_TIME = 0.004  # max time waiting for response in case something is missed
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)  # Trigger
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)
    time.sleep(TRIGGER_TIME)
    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
    else:
        return -1
    return distance


if __name__ == '__main__':
    try:
        while True:
            distance = measure()
            if(distance > -1):
                print("Measured Distance = %.1f cm" % distance)
            else:
                print("#")
            time.sleep(0.5)
        # Reset by pressing CTRL + C
    except KeyboardInterrupt:
        print("Measurement stopped by User")
        GPIO.cleanup()
N3sh
  • 131
  • 1