JetsonHacks

Developing on NVIDIA® Jetson™ for AI on the Edge

Ultrasonic Distance Sensor – HC-SR04 – NVIDIA Jetson TK1

Ultrasonic Distance Sensors, like the HC-SR04, are an inexpensive way to measure distance from the Jetson TK1. Looky here:

Background

Echo location is used by echolocating animals, such as bats and dolphins, for navigation and object detection. Echolocating animals emit calls out to the environment and listen to the echoes of those calls that return from objects around them. They use the echoes to locate and identify the objects.

A version of this idea has been implemented in the HC-SR04 sensor. When triggered, the HC-SR04 sends out 8 40KHz ultrasonic audio pulses and waits for the echo return. The sensor reads the echo return with the length of the wait determining how far away a detected object is located. This sensor is inexpensive as far as distance sensors go, only costing a few dollars.

Parts

HC-SR02

There are many versions of the HC-SR02 for sale by a large variety of vendors. In the video, a SunFounder 2 pcs Ultrasonic Module HC-SR04 Distance Sensor was used. There are two sensors in the package, and while only one is needed for demonstration, it is nice to have an extra one lying about.
61J9VgQFBqL._SL1000_

Level Converter

An Adafruit 4-channel I2C-safe Bi-directional Logic Level Converter:
757-04
In the video, a Solderless BreadBoard, 400 tie-points, 4 power rails was used, along with some regular breadboard jumpers, along with Hook up Wire – 22 Gauge.

Additional Tools

There are a couple of tools that are useful for this project:
1) A soldering iron, for the video a Hakko FX888D-23BY Digital Soldering Station was used.
2) Wire strippers, for the video a Hakko CHP CSP-30-1 Wire Stripper, 30-20 Gauge Maximum Cutting Capacity was used.

Wiring

Here’s a picture or two of the wiring:
IMG_0963
IMG_0968

The HC-SR04 operates with 5 volts. This includes the signals as well as the power supply. The Jetson TK1 GPIO signals are 1.8V, which means that a level converter (or voltage divider) must be used to make the signals compatible. For this demonstration, a level converter is used.

Power

5V from Jetson J3A1 pin 1 to 1 (+) 5 volt side power rail
5V to Level Converter HV
5V to HC-SR04 Vcc
1.8V from Jetson J3A1 pin 19 to 1 (+) 1.8 volt side
1.8V to Level Converter LV
On the prototype board, grounds go to the Level Converter HV side GND, Level Converter LV side GND as well as to the GND of the HC-SR04. You can tie the GND rails together on the prototyping board for easier wiring. In the video, the GND rails are tied together at row 30 on the prototype board.

GPIO pins

gpio165 from Jetson J3A2 pin 55 (trigger) to Level Shifter A2 (1.8V)
gpio166 from Jetson J3A2 pin 58 (echo) to Level Shifter A1 (1.8V)
Level Shifter B1 (5V) to HC-SR04 ECHO
Level Shifter B2 (5V) to HC-SR04 Trig

Software

A simple library with examples to interface with the HC-SR04 is located on the JetsonHacks Github repository.
To install the library and examples:
$ git clone https://github.com/jetsonhacks/JHHC-SR04.git
To run the simple example:

$ cd JHHC-SR04/example
$ make
$ sudo ./simpleHCSR04

Note: The ‘sudo’ command is required to access the GPIO signals from user space under L4T.

The plotExample requires installation and configuration of Qt Creator. For help on installation, looky here. PlotPractice is the name of the project. Build the project, locate the executable, and run it using sudo for permissions, i.e.:

$ sudo ./PlotPractice

Note: The plotExample uses QCustomPlot, a Qt C++ widget for plotting data visualization. Note that QCustomPlot is distributed as GPL licensed.

Note: The demonstration was run on L4T 21.4.

Discussion

The software that reads the HC-SR04 works, but is probably not as accurate as needed for a serious project. A SoC like the Jetson TK1 running Ubuntu (L4T) is not usually purposed towards real-time control of a bare sensor. Better strategies for dealing with raw sensors like the HC-SR04 usually involve having a micro-controller in the mix to handle the care and feeding of the sensor. Also, the library does not take into account the actual speed of sound dependent on atmospheric temperature and pressure. However, this does not take away any of the fun of connecting the sensor and playing with it on the Jetson!

Conclusion

So there you have it. For a few bucks you can start exploring non-contact distance ranging.

Facebook
Twitter
LinkedIn
Reddit
Email
Print

12 Responses

  1. Hi,

    Above tutorial was very informative. Thanks for the tutorial.
    I am new to Jetson. But I am trying to learn the Embedded Interface with Jetson for my master’s project. Can I have a step by step tutorial of how to interface it. Any help will be appreciated.

    Thank you.

    1. Hi Bhushan,
      Other than the article and video on this sensor, I don’t have any more information to share about this sensor. I’m not sure what you mean “step-by-step” instructions.

  2. Hi,

    I tried so many times with this tutorial with same parts …but failed
    There are some problems to execute this tutorial.
    First, I use same Level Shifter( TXB0108) but it’s not working even I wired like this page..
    2nd, when I execute(sudo ./simpleHCSR04), error “gpioExport: Device or resource busy” occurs. I refer ‘http://elinux.org/Jetson/Tutorials/GPIO’ page to use GPIO pin but still same error..

    Can u help me?

    Thank you.

    1. Hi Tim,
      Sorry that you’ve had issues. One thing to check: In the video I was using an Adafruit BSS138 I2C-safe logic level converter. I2C has some timing issues that the level shifter needs to account for. The TXB0108 does not have the required circuitry. I apologize for not including the level shifter model number in the article, I had used it in so many of the other articles that I must have forgotten to add it here. I’ll add it in so that others may benefit. Try a BSS138 (Product ID 1438) and see if you get better results. Thanks for reading!

  3. Hi,

    I tried to follow your post but I am getting an error which is as follows:

    gpioGetValue unable to open gpio166: No such file or directory

    Do you have any idea why this is happening?

    1. What command did you run when you get that message? It is possible that the device is wired incorrectly, or that gpio66 is not exported into user space.

        1. Sorry. There are two apps in the example section. It’s difficult for me to answer the question without knowing what command you executed. The question, “What command did you run when you get that message?” means what did you type on the command line of the Terminal before the message appears.

          1. I checked the connection and now I am not getting that GPIO issue. Thanks

            But, now I have an another issue: I am always getting a distance value of around 35-40 inches, even if I move the obstacle the close to the sensor. I tried with both of my ultrasonic sensor but I am getting the same behavior.

    1. This is not a step by step instruction on how to use this on the Jetson TX2. However, the same ideas apply. You will need to modify the GPIO mappings in the software, and correctly shift the voltage levels on the hardware. Good luck on your project, and thanks for reading!

Leave a Reply

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

Disclaimer

Some links here are affiliate links. If you purchase through these links I will receive a small commission at no additional cost to you. As an Amazon Associate, I earn from qualifying purchases.

Books, Ideas & Other Curiosities