2021 FIRST Robotics Teams Receive a MaxBotix Ultrasonic Sensor

2021 First Robotics Teams – Welcome!


FRC competitors, once again we are supporting your work in the field of robotics by giving each team a free MB1043 MaxSonar Sensor from our HRLV-MaxSonar-EZ line. This small and lightweight sensor is designed for easy integration into your project, and it is a great option for autonomous robot navigation.

Take a look at our data sheet for more information on your sensor’s specs! 


How Ultrasonics work

All of our ultrasonic rangefinders will measure distance by tracking the time-of-flight of a sound wave. The diagram below shows how the sensor sends and receives the sound waves. Once the sensor emits a sound wave, it tracks how long it takes for the sound to reflect off of a surface and travel back to the sensor. The sensor then uses the known speed-of-sound to turn the time-of-flight into a range reading.

how ultrasonic sensors work


Using Your Ultrasonic Sensor with your NI roboRIO



Our sensors can easily be connected and powered by the analog inputs on your NI roboRIO. Do note that if you choose to use multiple sensors you will have to connect the trigger pin of the sensor to a digital output on the NI roboRIO. It is recommended to do multitasking such as creating a new thread to trigger the sensor while your main robot code is still running.


You will need to wire up 3 pins in order to use our sensors. Firstly, we need 5 Volt power (Pin 6) and Ground (Pin 7) to power the sensor.

Secondly, we need to connect the Analog Output (Pin 3) so that we can read values from the sensor.

To identify which pins these are on your sensor, you can note that one of the 7 pins on the sensor has a square copper pad around it, instead of a circular one.

The square pad is Pin 1, and they count up along the board. The pins are also labeled on the back of the Circuit Board.

We need to connect these pins to an Analog Input port on your RoboRIO. We will use Analog Port 0 in our example.

Pin 6 on our sensor should be connected to the RoboRIO pin labeled “5V”, Pin 7 should be connected to the pin labeled with the Ground Symbol.

Pin 3 should be connected to the pin labeled “S”.

That’s all we need to do to connect our Sensor to the RoboRIO. We’ve included a wiring diagram below.


Single Sensor Setup
MaxBotix roboRIO



Our sensors communicate with your RoboRIO using one of two methods: Analog Voltage Output (Pin 3), or Pulse Width Output (Pin 2). In this example we are using Analog Voltage Output for simplicity. When power is applied to Pins 6 and 7 of our sensor, the voltage on pin 3 will change according to the distance between the sensor and the object in front of it. So in order to read this distance, we need the RoboRIO to measure the voltage on Pin 3. This can be done using the WPILib AnalogInput Library.

First, let’s import the library:


import edu.wpi.first.wpilibj.AnalogInput;



#include <frc/AnalogInput.h>


Next, let’s create an instance of the AnalogInput class:


private final AnalogInput ultrasonic = new AnalogInput(0); //the 0 represents the Analog port number we’ve connected our sensor to.


frc::AnalogInput ultrasonic{0}; //the 0 represents the Analog port number we’ve connected our sensor to.


Now that our sensor is setup, let’s grab a distance value from the sensor:


double rawValue = ultrasonic.getValue()


double rawValue = ultrasonic.GetValue()


This will return a number in between 0 and 4095. 0 represents 0V applied, and 4095 represents 5V applied. Now that we know what voltage the sensor is outputing, let’s scale this to our distance range:


double currentDistance = rawValue * 0.125;



double currentDistance = rawValue * 0.125;


This will return our distance in Centimeters. The Maximum range of our MB1043 sensor is 5 Meters, and the minimum range is 30 centimeters.

0.125 is a constant value that converts to centimeters. If you want to use a different unit, simply convert 0.125 centimeters to your desired unit and use that value.


Below is a complete example of how to use this sensor to make a robot maintain a specific distance from an object:



<See Main.Java>


<See main.cpp>


  1. Summary

Our sensors output Analog Voltage or Pulse Width signals. Here is a wiring diagram showing the RoboRIO connected to the Analog Output of our sensor. The Square pin is Pin 1.


Once that’s connected, you can use the sensor in your code like this:


import edu.wpi.first.wpilibj.AnalogInput; // Import WPILib AnalogInput library.

private final AnalogInput ultrasonic = new AnalogInput(0); // Create an instance

double rawValue = ultrasonic.getValue()  // Get a Raw Analog Value, 0-4095

double currentDistance = rawValue * 0.125; // Scale value to Centimeters



#include <frc/AnalogInput.h> // Import WPILib AnalogInput library.

frc::AnalogInput ultrasonic{0}; // Create an instance

double rawValue = ultrasonic.GetValue() // Get a Raw Analog Value, 0-4095

double currentDistance = rawValue * 0.125; // Scale value to Centimeters


  1. How this helps you

Once you have your ultrasonic sensor hooked up, you can write code to control your Robot. For example, your robot could use these values to drive a certain distance in Autonomous. You could use range information to automatically perform an action when you approach a target. You could also use this data to assist your drivers during the Teleoperated period.

Even if you are using other sensors, Ultrasonic sensors are a great addition. Only ultrasonic sensors can reliably detect certain surfaces such as glass. Transparency and color of objects have no effect on an ultrasonic sensor’s ability to see an object. This is especially useful if you don’t know what kind of environment your robot will be operating in.


If you need any help using our sensors feel free to contact us at [email protected] or if you’d like to purchase additional sensors, visit us at www.Maxbotix.com, or contact us at [email protected]. Thank you, and Good Luck this season!



MaxBotix Inc.

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