Follow MaxBotix:
Ask A Question
|     Written By: Tom Bonar    Date Posted: 08-29-2013     |     Updated by: Cody Carlson    Date Updated: 03-03-2016     |

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

This article was not written for Raspberry Pi 3. Here is the new article.

Key Takeaways

  • The status of the serial data pins must be reset to connect a sensor to a Raspberry Pi.
  • The required steps may vary depending on the revisions of your operating system.
  • The Raspberry Pi is a convenient and affordable way to read sensor range data.
Raspberry Pi 2 drawing

MaxSonar sensors offer a variety of outputs including TTL serial data. This tutorial will teach you how to interface a Raspberry Pi with a MaxBotix sensor to read its serial data.

TIP For the safety of the electronics on the Raspberry Pi and MaxBotix 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 operating system
  • MaxSonar sensor with serial output
  • Mini-Grabbers
  • Male to female jumpers
  • Serial inverter (only for sensors that require an inverter)
  • DC power supply (only for sensors that need a separate power supply)
  • ESD strap

Directly Compatible Sensors

Sensors that Require an Inverter

The sensors listed below provide TTL serial data and directly interface with the Raspberry Pi. The sensors listed below require an inverter to operate with the Raspberry Pi.
  • MB10X3 sensors
  • MB7344
  • MB7374
  • MB738X sensors
  • MB7544
  • MB7574
  • MB758X sensors
  • MB10X0 sensors
  • MB10X4 sensors
  • MB12X0 sensors
  • MB13XX sensors
  • MB70XX sensors
  • MB7334
  • MB7354
  • MB736X sensors
  • MB7534
  • MB7554
  • MB756X sensors

Getting Started

You will need to install an operating system if this is the first time using your Raspberry Pi. This article was created for the Raspberry Pi 2 using the Raspbian Jessie operating system (downloaded on 2/23/2016). This article will also work for the original Raspberry Pi and Raspberry Pi Zero. Depending on the Raspberry Pi model that you own, you may only have pins 1 - 26 rather than the 40 pin output shown in this tutorial. If you only have 26 pins, the layout of these pins is identical to the layout of pins 1 - 26 of the 40 pin layout. This article will be periodically updated as the operating systems, files, and settings are updated and moved.

Step One: Disable Bootup Information on TX/RX Pins

The serial pins are set to send bootup data to any attached devices, but we want to allow them to receive asynchronous serial data. To allow this we must disable the bootup data. This step removes the code for the bootup data to be sent on these pins. Open the file /boot/cmdline.txt by using the following command:

sudo nano /boot/cmdline.txt
console=ttyAMA0,115200 & kgdboc=ttyAMA0,115200
(The file may be blank or kgdboc=ttyAMA0,115200 may not be present on all models. Only remove the matching text that you can find.)

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

Step Two: Disable Serial Port Login

Depending on the version of your operating system, the TX and RX pins may be set up to allow a serial port login. This prevents you from using the serial ports in any programs. This step comments out the code that allows serial port login.
Open the file /etc/inittab by using the following command:

sudo nano /etc/inittab
#Spawn a getty on Raspberry Pi serial line
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
Add a # symbol to the second line as follows.
#Spawn a getty on Raspberry Pi serial line
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
(Depending on the version of your Raspbian Jessie operating system, this file may be blank. No changes need to be made if the file is blank.) Save and close the file by simultaneously pressing Ctrl and X to exit the screen and then pressing Y to save it.

Optional Step: 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 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 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 sensors.

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 sensor's pinout. During the sensor 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

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.

Installing Terminal Software

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

Open LXTerminal
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 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.

Open LXTerminal
Open Minicom with the following command

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 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 sensor, the reported range will increase. If no target is detectable, the target will report maximum range.

Final Notes & Outcome

Now that the Raspberry Pi is configured to not look at the RX and TX pins, devices such as the HRLV‑MaxSonar‑EZ1 MB1013 can be connected directly to the Raspberry Pi before it is powered up. If a sensor or component was connected to the Raspberry Pi before this configuration took place, the Raspberry Pi may not have booted properly.

Using a MaxBotix 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


Author: Kathy Kostal  Date: 08-31-2016
Inc 5000 Maxbotix Logo Inc. Magazine Unveils 35th Annual List of America's Fastest Growing Private Companies–the Inc. 5000. MaxBotix Inc., Ranks No. 1752 on the 2016 Inc. 5000 with Three–Year Sales Growth of 213%.
Click here for full article.
Author: Cody Carlson  Date: 08-02-2016
Raspberry Pi 2 board MaxSonar sensors offer a variety of outputs including TTL serial data. This tutorial guides you through the process of setting up your Raspberry Pi 3 with a MaxBotix sensor. Click here for full article.
Author: Scott Wielenberg  Date: 07-26-2016
Full Horn Housing MaxBotix offers an expanded range of packaging options for many of our sensors. Each option provides unique benefits to certain mounting integrations. This article provides a brief overview of each option.
Click here for full article.
Author: Scott Wielenberg  Date: 07-18-2016
Sensor is Tested When providing support, our technical support team may determine that further testing at our facility is the best way to help resolve the issue that you are facing. At this point, they will start the Return Merchandise Authorization (RMA) process. This article will explain what you can expect as your ultrasonic sensor travels through our RMA process.
Click here for full article.
Author: Scott Wielenberg  Date: 07-11-2016
Typical Wall Pipe Many customers have requested the option to mount an ultrasonic sensor in a pipe. During the testing and development cycle, we discovered a number of considerations and requirements that must be met for the application to be successful. When all of these are met, a user may be able to achieve the desired level of success for measuring the liquid level inside of a pipe.
Click here for full article.
Author: Jenney Grover  Date: 06-28-2016
one of the options for outputs On April 19th, we welcomed our supporters to join us for the Grand Opening of the Build Out. Bob and Nita Gross gave a tour of the build out and their vision for the space. We continue to be in awe of the support from our community, our employees, our distributors, and our customers. Thank you for the many years of support, and we look forward to serving you in the years to come.
Click here for full article.
Signup for notification of our exciting new products and periodic new letters. We are excited to provide the latest information from MaxBotix Inc.