Ultrasonic Sensor USB Serial Python

AJ-SR04M

How’s it going. I’m going to try and limit this for simplicity. I am a student studying EE. I have an UltraSonic Sensor connected using USB Serial Port CH340. I have tried to look for a python script that could give the distance but it seems it’s not possible. Any help or guidance is greatly appreciated.

Or any help with GPIO is also greatly appreciated but I’ve read online it’s not recommended because of OS.

I have looked into this article and have attached a script on this.

import serial
import time
import sys
ser = serial.Serial('/dev/ttyUSB0')  # open serial port
byte = b'\xff' # xff is the start byte
while True:
    x = ser.read()
    if x == byte: #starbyte found
        b1 = ser.read(1)
        b2 = ser.read(1)
        x1 = int.from_bytes(b1, sys.byteorder)
        dist = (((x1 << 8) + int.from_bytes(b2, sys.byteorder)) / 10) # some bitshift magic, no idea what's happening here
        print(dist - 5) # for some reason the distance value is 5cm off, remove them before printing the value
        clear = ser.read_all()
        ser.flushOutput()
    time.sleep(2)

please refer to Topic 1049116 for Python GPIO support, and you might also check Jetson Nano GPIO Header PINOUT.
thanks

Hey @kayccc so I have looked into using GPIO from other forums. However, this forum and a couple of others point to not using GPIO because it is unreliable. I wanted to look into using USB Serial Port.

Hi sergio.deleon1,

Are you using the devkit or custom board for Jetson Nano?
What’s the Jetpack version in use?

It seems you want to use an UltraSonic Sensor connecting through USB.
So, it is an USB device for Jetson.
What’s your current issue about this device?
Have you also tried using this device on an Ubuntu host pC?

About GPIO issue, I would suggest you creating another topic to discuss in details since it is not relating to the current USB device.

Hey @KevinFFF yeah I probably should have specified. The Jetson Nano I’m using is the dev kit using the board B01. In addition the jet pack version is 4.6.1. It’s an old board but nevertheless works well with our RPLidar C1 sensor.

Here’s the set up I have. I have the AJ-SR04M (ultrasonic sensor) connected using RX → to TXD and vice versa TX - RXD to my usb. I have tried this using a host PC and Jetson but reads wrong distance.

I have tried it with this code but doesn’t read it right


# terminal.py - jsn_sr04t ultrasonic transducer

import serial
import sys
import msvcrt
import time
from time import sleep

serialPort = serial.Serial(
    port="COM24", baudrate=9600, bytesize=8, timeout=1, stopbits=serial.STOPBITS_ONE
)
serialPort.rtscts = False
serialPort.xonxoff = False
serialPort.dsrdtr = False 
sys.stdout.write("Python jsn_sr04t ultrasonic transducer\n")
# display Device information
sys.stdout.write("Device information ");
command = b'\xF1' # character in hex
serialPort.write(command)  # read Device information
sleep(0.1)     # delay 100mSec
while serialPort.in_waiting > 0:
         char = serialPort.read()
         #sys.stdout.write("\nreceived ")
         if ord(char) < 128:
             sys.stdout.write(str(char,'ASCII'))
# loop reading continuous distance 
print("\nreading distances")
while 1:
    sleep(1)
    command = b'\xA0'          # read distance command
    serialPort.write(command)  # read distance
    sleep(0.1)                 # delay 100mSec
    # read three bytes convert to integer
    h = int.from_bytes(serialPort.read())
    w = int.from_bytes(serialPort.read())
    l = int.from_bytes(serialPort.read())
    # calculate distance
    distance = ((h << 16) + (w << 8) + l) / 1000
    print(f'Distance {distance}');
;

I have it set on mode 5 per this forums instructions

There is no update from you for a period, assuming this is not an issue anymore.
Hence, we are closing this topic. If need further support, please open a new one.
Thanks

What’s the module with RED LED light-ON in your picture?
Is it a USB to serial converter?

If so, it is still a USB device for Jetson Nano.
Why don’t you just connect them to the UART interface(at 40 pins expansion header) on Jetson Nano devkit?

Do you mean that host PC read the correct value but Jetson Nano dosen’t?