Using a Raspberry Pi Distance Sensor
Using a Raspberry Pi Distance Sensor
Many of our customers use Raspberry Pi and other micro-controllers when testing or developing their ideas.
If you’ve never tried using our sensors before, it may seem overwhelming, this guide and the others listed below will help you get started with our easy to use ultrasonic range finders.
If you need help finding the right sensor feel free to click the button “Get Help Finding the Right Sensor for Your Raspberry Pi Project” and answer 10 quick questions, so we can help you get started.
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.
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.
- UART communication needs to be enabled on the Raspberry Pi.
- The required steps will vary depending on the revisions of your operating system.
- The Raspberry Pi is a convenient and affordable way to read ultrasonic sensor range data. Here are 11 reasons why the Raspberry Pi it’s the perfect small server.
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.
- Raspberry Pi with an operating system
- Ultrasonic sensors with a serial output
- 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 withthe Raspberry Pi.
- MB1003 Ultrasonic Sensors – Industrial Self-Cleaning Outdoor High-Performance Ultrasonic Rangefinder
- MB7344 Ultrasonic Sensors – Industrial Outdoor High-Performance Ultrasonic Precision Rangefinder
- MB7374 Ultrasonic Sensors – Industrial Outdoor High-Performance Ultrasonic Precision Rangefinder
- MB7380 Ultrasonic Sensors – Industrial Outdoor High-Performance Ultrasonic Precision Rangefinder
- MB7544 Ultrasonic Sensors – Industrial Self-Cleaning Outdoor High-Performance Ultrasonic Precision Rangefinder
- MB7574 Ultrasonic Sensors – Industrial Self-Cleaning Outdoor High-Performance Ultrasonic Precision Rangefinder
- MB7584 Ultrasonic Sensors – Industrial Self-Cleaning Outdoor High-Performance Ultrasonic Precision Rangefinder
Ultrasonic Sensors that Require an Inverter
The Sensors listed below require an inverter to operate with the Raspberry Pi.
- MB1000 Ultrasonic Sensors – High-Performance Ultrasonic Rangefinder
- MB1004 Ultrasonic Sensors – High-Performance Ultrasonic Rangefinder
- MB1200 Ultrasonic Sensors – High-Performance Ultrasonic Rangefinder
- MB1300 Sensors – High-Performance Ultrasonic Rangefinder
- MB7052 Sensors – Industrial Outdoor High-Performance Ultrasonic Precision Rangefinder
- MB7334 Ultrasonic Sensors – Industrial Outdoor High-Performance Ultrasonic Precision Rangefinder
- MB7354 Ultrasonic Sensors – Industrial Outdoor High-Performance Ultrasonic Precision Rangefinder
- MB7364 Ultrasonic Sensors – Industrial Outdoor High-Performance Ultrasonic Precision Rangefinder
- MB7534 Ultrasonic Sensors – Industrial Outdoor High-Performance Ultrasonic Precision Rangefinder
- MB7554 Ultrasonic Sensors – Industrial Self-Cleaning Outdoor High-Performance Ultrasonic Precision Rangefinder
- MB7564 Ultrasonic Sensors – Industrial Self-Cleaning Outdoor High-Performance Ultrasonic Precision Rangefinder
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.
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.
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 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.
- Open LXTerminal
- 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.
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.
Other Raspberry Pi Resources
How It Works
1. Discuss your application with our support team
2. Choose a Sensor
3. Easily integrate the sensor in your application
Thousands of applications supported
- We test our sensors for over 200,000 hours to verify reliability (MTBF)
- We warranty our sensor from a manufacturing defect
We Care About The Success of Your Application
We’ve helped over 20,000 engineers find sensor solutions for their applications.
Our sensors have been integrated into over 10,000 projects.
Our collaborators serve as case studies to show you how the sensors are being used in an end user application. You can either leverage our Collaborators end product or work with us to find the right sensor for your solution.
Long-range wireless IoT sensor designer and manufacturer
Radio Bridge Inc. provides a general purpose LoRaWAN bridge using MaxBotix sensors to be used in many applications.
- Industries served: retail, industrial, oil and gas, deduction, utilities, security
- Typically used in tank monitoring applications
- Wireless connectivity with LoRaWAN protocol
- Wireless penetration through structures such as walls and floors
Ultrasonic Distance Sensor for Tank Level Measurement
LX Group developed an Ultrasonic Distance Sensor for Tank Level Measurement
- Used in grain silos, water tanks, and dams
- Monitor the level of water tanks remotely
- The plug-and-play product works out of the box and is robustly designed to work in rough environments.
LoRaWAN IoT Distance Sensor
Sweden based company ELSYS uses a sensor from our WR line in their IoT Distance Sensor.
Real world applications include:
Underground water level measurement
Waste level measurement in containers
Snow depth measurement
MaxBotix Inc. designs and manufactures ultrasonic sensors for use in level, proximity, and distance sensing.
Terms and Conditions