2021 FIRST Robotics Teams Receive a MaxBotix Ultrasonic Sensor

2021 First Robotics Teams – Welcome!

MB1013

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

 

1. HOOK UP AND USE YOUR CONTROLLER WITH ONE SENSOR

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

 

2. PROGRAMMING

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:

Java:

import edu.wpi.first.wpilibj.AnalogInput;

 

C++:

#include <frc/AnalogInput.h>

 

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

Java:

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

C++:

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:

Java:

double rawValue = ultrasonic.getValue()

C++:

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:

Java:

double currentDistance = rawValue * 0.125;

 

C++:

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:

 

Java:

<See Main.Java>

C++:

<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:

Java:

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

 

C++:

#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!

 

 


Do NOT follow this link or you will be banned from the site!