ModularSensors > Modules > Supported Sensors > MaxBotix MaxSonar

MaxBotix MaxSonar module

Classes for the MaxBotix MaxSonar ultrasonic range finders.


The IP67 rated HRXL-MaxSonar-WR ultrasonic rangefinders offer 1mm resolution, 2.7-5.5VDC operation, a narrow beam pattern, high power output, noise rejection, automatic calibration, and temperature compensation. Depending on the precise model, the range finders have ranges between 300 and 9999mm and read rates of 6-7.5Hz. This library supports TTL or RS323 sensor output, though an RS232-to-TTL adapter is needed for the RS232 models. Analog and pulse-width outputs are not supported. The MaxBotix sensors require a 2.7V - 5.5V power supply to pin 6 on the sensor (which can be turned off between measurements) and the level of the TTL returned by the MaxSonar will match the power level it is supplied with. The digital TTL or RS232 output is sent out on pin 5 on the sensor. Pin 7 of the MaxSonar must be connected to power ground and pin 4 can optionally be used to trigger the MaxSonar.

If you are using the MaxBotix HR-MaxTemp MB7955 temperature compensator on your MaxBotix (which greatly improves data quality), the red wire from the MaxTemp should be attached to pin 1 (square) on the MaxSonar. The white and shield (bare silver) wires from the MaxTemp should both be attached to Pin 7 (GND). The MaxTemp communicates directly with the MaxSonar and there is no need to make any changes on the Aruduino itself to accomodate it. It is not possible to read the temperature data from the MaxTemp.

The MaxBotix sensor have two different modes: free-ranging and triggered. Unless the trigger pin is externally held low, the sensor will continuously take readings at a rate of 6Hz or greater and immediate report each result over the digital output pin. (That is, it will be in free-ranging mode.) When continuously powered and operating in free-range mode, the data output is automatically filtered to help improve accuracy. If you are turning the power to the sensor off between readings, there is no advantage to using the free-ranging because many readings must be taken before the filter becomes effective. In this case, you may save a very small amount of power by setting up a trigger pin and manually trigger individual readings.

Please see the section "[Notes on Arduino Streams and Software Serial](" for more information about what streams can be used along with this library.

This library supports using multiple MaxBotix sensors on the same logger, with a few caveats:

  • Any sensor operating in free-ranging mode (powered at the same time as any other sensors with the trigger pins unconnected) must have a dedicated stream instance/serial port.
  • To have two sensors operating in free-ranging mode, they must each have a dedicated stream instance/serial port AND you must specify a unique negative pin number for the trigger pin. Giving a negative pin number ensures that the Arduino will not attempt to trigger trigger individual readings but will still be able to tell the sensors apart. (Software-wise, simply specifying the different streams is not enough!) Keep in mind that two or more free ranging sensors must be spaced far enough apart in the field to prevent interference between the sonar beams.
  • Two or more sensors may send data to the same stream instance/serial port if both sensors are being triggered and each is triggered by a different trigger pin.
  • "Daisy chaining" sensors so the pulse-width output of one sensor acts as the trigger for a second sensor is not supported.

Sensor Datasheet

Sensor Constructor

MaxBotixSonar(Stream* stream, int8_t powerPin, int8_t triggerPin = -1, uint8_t measurementsToAverage = 1)

Construct a new MaxBotix Sonar object.

stream An Arduino data stream for TTL or RS232 communication. See notes for more information on what streams can be used.

The pin on the mcu controlling power to the MaxSonar. Use -1 if it is continuously powered.

  • The MaxSonar requires a 2.7V - 5.5V power supply.
triggerPin The pin on the mcu controlling the "trigger" for the MaxSonar. Use -1 or omit for continuous ranging.
measurementsToAverage The number of measurements to take and average before giving a "final" result from the sensor; optional with a default value of 1.

Example Code

The MaxBotix MaxSonar is used in the menu a la carte example.

#include <sensors/MaxBotixSonar.h>

// A Maxbotix sonar with the trigger pin disconnect CANNOT share the serial port
// A Maxbotix sonar using the trigger may be able to share but YMMV

// NOTE: Extra hardware and software serial ports are created in the "Settings
// for Additional Serial Ports" section

const int8_t SonarPower =
    sensorPowerPin;  // Excite (power) pin (-1 if unconnected)
const int8_t Sonar1Trigger =
    -1;  // Trigger pin (a unique negative number if unconnected)
const uint8_t sonar1NumberReadings = 3;  // The number of readings to average

// Create a MaxBotix Sonar sensor object
MaxBotixSonar sonar1(sonarSerial, SonarPower, Sonar1Trigger,

// Create an ultrasonic range variable pointer
Variable* sonar1Range =
    new MaxBotixSonar_Range(&sonar1, "12345678-abcd-1234-ef00-1234567890ab");


class MaxBotixSonar
The Sensor sub-class for the MaxBotix ultrasonic range finders.
class MaxBotixSonar_Range
The Variable sub-class used for the range output from a MaxBotix HRXL-MaxSonar ultrasonic range finder.


Sensor::_numReturnedValues; the HRXL can report 1 value.

Sensor Timing

The sensor timing for a Maxbotix HRXL ultrasonic range finder

#define HRXL_WARM_UP_TIME_MS = 160
Sensor::_warmUpTime_ms; warm up time to completion of header: 160ms.
Sensor::_stabilizationTime_ms; the HRXL is stable as soon as it warms up (0ms stabilization).
Sensor::_measurementTime_ms; the HRXL takes 166ms to complete a measurement.


The range variable from a Maxbotix HRXL ultrasonic range finder

  • Range is 300 to 5000mm or 500 to 9999mm, depending on model
  • Accuracy is ±1%

MaxBotixSonar_Range(MaxBotixSonar* parentSense, const char* uuid = "", const char* varCode = HRXL_DEFAULT_CODE) explicit

Construct a new MaxBotixSonar_Range object.

parentSense The parent MaxBotixSonar providing the result values.
uuid A universally unique identifier (UUID or GUID) for the variable; optional with the default value of an empty string.
varCode A short code to help identify the variable in files; optional with a default value of "SonarRange".

Decimals places in string representation; range should have 0 - resolution is 1mm.
#define HRXL_VAR_NUM = 0
Sensor variable number; range is stored in sensorValues[0].
#define HRXL_VAR_NAME = "distance"
Variable name in ODM2 controlled vocabulary; "distance".
#define HRXL_UNIT_NAME = "millimeter"
Variable unit name in ODM2 controlled vocabulary; "millimeter".
#define HRXL_DEFAULT_CODE = "SonarRange"
Default variable short code; "SonarRange".