Interfacing a Raspberry Pi with an Ultrasonic Sensor

This article and code example may not work with the most recent versions of the Raspberry Pi operating system.  You may be required to make updates to the code for the sensor to work.   

Do you enjoy or love testing new Raspberry Pi Projects? Maxbotix has put together a tutorial that teaches you how to interface a Raspberry Pi with an ultrasonic sensor to read TTL serial data. You may also want to check out our tutorials on Using an ultrasonic sensor with a Raspberry Pi and how to use USB sensors for a Raspberry Pi.

Getting Started

The Raspberry Pi needs to install an operating system the first time it is used. This article was created for the Raspberry Pi 3 using the Raspbian Jessie operating system (downloaded on 10/06/16). This article was also successfully tested on the Raspberry Pi 2 with the same operating system. This article will be periodically updated as the operating systems, files, and settings are updated and moved.

Check out these 5 Ways New Raspbian Jessie Makes Raspberry Pi Even Easier to Use. We hope you enjoy this tutorial. After you’re done reading, don’t forget to come back for more Raspberry Pi Projects exclusively provided by MaxBotix.

Key Takeaways

raspberry pi ultrasonic sensors

TIP For the safety of the electronics on the Raspberry Pi and MaxBotix ultrasonic sensors, please use an ESD strap when working around components. Static discharge could damage components of the Raspberry Pi as well as the ultrasonic sensors.

Required Equipment

  • Raspberry Pi with an operating system
  • Ultrasonic sensors with a serial output
  • Mini-Grabbers
  • Male to female jumpers
  • Serial inverter (only for ultrasonic sensors that require an inverter)
  • DC power supply (only for ultrasonic sensors that need a separate power supply)
  • ESD strap

Directly Compatible Ultrasonic Sensors

The Sensors listed below provide TTL serial data and directly interface with
the Raspberry Pi.

Ultrasonic Sensors that Require an Inverter

The Sensors listed below require an inverter to operate with the Raspberry Pi.

Important: Connect the center square with the outer portion of the pad with a small amount of solder to enable the TTL output on the MB1003 from the HRLV-MaxSonar-EZ line.

Ultrasonic Sensor - Solder before

Ultrasonic Sensor - Solder after

Enable Serial Data on ttyAMA0

We want to enable the RX pin on ttyAMA0 to receive asynchronous serial data. To allow this, we need to change the set the enable UART setting in the configurations files.

Open the file /boot/config.txt by using the following command:

sudo nano /boot/config.txt

Scroll to the end of the file and add the following lines to the file. Scroll to the end of the file and add the following lines to the file.


This will disable Bluetooth on the Raspberry Pi 3 and allow serial communication in its place. While this isn’t fully needed for other versions of the Raspberry Pi, it will not impact their performance.

Save and close the file by simultaneously pressing Ctrl and X to exit the screen and then pressing Y to save it.

Optional: Download a Python Script

This is optional, but you may wish to write or download a Python Script to log and store the data you collect with your ultrasonic sensor.

Here is sample python code provided by one of our users to automate the reading of distance values.

Supplying Power to the Sensor

When using an XL-MaxSonar or WR ultrasonic sensor with a Raspberry Pi, it is highly recommended to use an external power supply. The Raspberry Pi is not intended to supply a high enough current draw to power these ultrasonic sensors.

Ultrasonic sensors from the LV-MaxSonar, LV-ProxSonar, HRLV-MaxSonar, and ParkSonar lines can be directly powered by the Raspberry Pi.

TIP You may want to place a capacitor between the power and ground lines immediately before the ultrasonic sensor’s pinout. During the ultrasonic sensor’s transmit, the sensor draws a higher current for a fraction of a second. The higher current draw may cause the Raspberry Pi’s microcontroller to begin to shut down. To prevent any issue with long term use, we recommend that you use a capacitor with at least a one microfarad rating to ease the current draw from the Raspberry Pi.

Wiring Instructions

Raspberry Pi 2 wiring diagram

Connect the female end of a jumper to pin 10 of the Raspberry Pi GPIO. This is the RX pin for serial data.

Connect a Mini-Grabber or solder a wire into pin 5 of the sensor, and connect this to the jumper.

Connect your sensor to an appropriate power supply.

Raspberry Pi Pins

Installing Terminal Software

The recommended software for accessing the range data output is Minicom. Please use the following steps to install Minicom.

  1. Open LXTerminal
  2. Download Minicom with the following command
sudo apt‑get install minicom

This will install the Minicom software that is used for reading the serial port.

Accessing Ultrasonic Sensor Range Data Output

Minicom is the recommended terminal software for Raspbian Jessie operating system. Please use the steps below to access Minicom and read the range data.

  1. Open LXTerminal
  2. Open Minicom with the following command

(For Raspberry Pi 3)

minicom -b 9600 -o -D /dev/ttyS0

(For Raspberry Pi 2)
Click Here to learn how to use USB ultrasonic sensors for a Raspberry Pi.

minicom -b 9600 -o -D /dev/ttyAMA0

This clears the LXTerminal window and you will see a window that looks similar to the image below.

Raspberry Pi 2 wiring diagram

The number following the ASCII character is the range reading that is output by the ultrasonic sensor. If the readout says R0764, like the image above, the range to the target is 764mm. As the target gets further away from the ultrasonic sensor, the reported range will increase. If no target is detectable, the target will report maximum range.


Monitoring oil tank level with a MaxBotix ultrasonic sensor


Final Notes & Outcome

Using an ultrasonic sensor with a Raspberry Pi allows for inexpensive monitoring of tank levels, height measurement, as well as countless other applications that require distance measurement. If you have any applications or code that can be added to this article, or if you would like to share pictures of your setup with us so we can share with others please email [email protected].

NOTE: This article was created for the Raspberry Pi 3 using the Raspbian Jessie operating system (downloaded on 7/18/2016).

This article was not written for Raspberry Pi 2.


Products Related to the Article

MB1013 HRLV-MaxSonar-EZ1

MB1013 HRLV-MaxSonar-EZ1

The HRLV‑MaxSonar‑EZ1 is a great choice for use where sensitivity is needed along with side object rejection…

Buy Now

MB7380 HRXL-MaxSonar-WRT

MB7380 HRXL-MaxSonar-WRT

The weather-resistant HRXL-MaxSonar-WRT is a rugged, ultrasonic sensor component module…

Buy Now

MB7589 SCXL-MaxSonar-WRMT

MB7589 SCXL-MaxSonar-WRMT

The weather-resistant SCXL-MaxSonar-WR self‑cleaning sensor is a rugged ultrasonic sensor component…

Buy Now


Articles Related to the Article Above

How to Use an Ultrasonic Sensor with an Arduino

Using a MaxSonar with an Arduino

Arduino ultrasonic sensors are very popular for integrating when designing solutions for many applications in robotics and automation. The MaxBotix ultrasonic sensors that interface with the Arduino platform make it easy for users to implement the needed ranging capabilities no matter the need.

Read Full Article

MB7954 Shielded Cable

MB7954 Shielded Cable

The MaxSonar Connection Wire is used to reduce interference caused by electrical noise on the lines. This cable is a great solution to use when running the sensors at a long distance or in an area with a lot of EMI and electrical noise. MaxBotix Inc. has successfully tested our sensors at a distance of 1,000 ft using this wire and it was as stable as if it were next to the power supply.

See Product Page

Power Supply Testing

Power Supply Testing

Whether you believe your power supply may be faulty or you are doing routine testing, it is important to verify the performance. Issues with a power supply can limit the performance of your equipment, and it even has the potential to damage your fine electronics. Proper and regular power supply testing can help minimize this risk.

Read Full Article

Support Limitations

MaxBotix provides code examples as a reference starting place for our customer. At the time of development, the code examples worked for the given platform and setup. Due to the fast changing nature of these platforms (Hardware, OS and peripheral devises), we are unable to keep up with the changes in these code examples. Additionally, every system setup is different which further increases the complexity of the system and troubleshooting.

Support for code is very limited regarding our free sensor support system. We recommend using the platforms forums for support to help troubleshoot the code issue as these are the most up to date regarding information. We do offer paid engineering support for code if this is something you may be interested in. Please contact us if you would like to look into this.

Please see the Raspberry Pi forum for more support at this link:

We use cookies to give you the best online experience. By agreeing you accept the use of cookies in accordance with our cookie policy.

Privacy Settings saved!
Privacy Settings

When you visit any web site, it may store or retrieve information on your browser, mostly in the form of cookies. Control your personal Cookie Services here.

These cookies are necessary for the website to function and cannot be switched off in our systems.

In order to use this website we use the following technically required cookies
  • wordpress_test_cookie
  • wordpress_logged_in_
  • wordpress_sec

For perfomance reasons we use Cloudflare as a CDN network. This saves a cookie "__cfduid" to apply security settings on a per-client basis. This cookie is strictly necessary for Cloudflare's security features and cannot be turned off.
  • __cfduid

Decline all Services
Accept all Services