Go Down

Topic: Reading serial data from maxSonar ultrasonic rangefinder (Read 3125 times) previous topic - next topic

Hi all, just started with arduino and thought I would share this. There seems to be plenty on using the analog or pulse width outputs from the MaxSonar products but nothing about their serial use. This simple function reads the incoming serial data, converts it to an integer and returns the result.

A few things to note first.

     - The serial output on the MAxSonar is always enabled unless told otherwise. Attempting to hook it up directly to the serial port will cause communication problems between arduino and computer.

     - Serial data is inverted from the MaxSonar, to remove the need for extra circuitry I have used the very handy function in SoftwareSerial to flip the signal

     - This should work for all LV-EZ* and XL* modules, however the range returned will either be inches or centimeters depending on the type.

Code: [Select]

/* test function to Parse data from MaxSonar serial data
and return integer */

#include <SoftwareSerial.h>

#define txPin 3                                            //define pins used for software serial for sonar
#define rxPin 2

SoftwareSerial sonarSerial(rxPin, txPin, true);            //define serial port for recieving data, output from maxSonar is inverted requiring true to be set.

boolean stringComplete = false;

void setup()
  Serial.begin(9600);                                      //start serial port for display
  sonarSerial.begin(9600);                                 //start serial port for maxSonar
  delay(500);                                              //wait for everything to initialize


void loop()
  int range = EZread();
    stringComplete = false;                                //reset sringComplete ready for next reading

    Serial.print("Range ");
    //delay(500);                                          //delay for debugging

int EZread()
  int result;
  char inData[4];                                          //char array to read data into
  int index = 0;

  sonarSerial.flush();                                     // Clear cache ready for next reading

  while (stringComplete == false) {
    //Serial.print("reading ");    //debug line

      if (sonarSerial.available())
      char rByte = sonarSerial.read();                     //read serial input for "R" to mark start of data
      if(rByte == 'R')
        //Serial.println("rByte set");
        while (index < 3)                                  //read next three character for range from sensor
          if (sonarSerial.available())
            inData[index] = sonarSerial.read();
            //Serial.println(inData[index]);               //Debug line

            index++;                                       // Increment where to write next
        inData[index] = 0x00;                              //add a padding byte at end for atoi() function

      rByte = 0;                                           //reset the rByte ready for next reading

      index = 0;                                           // Reset index ready for next reading
      stringComplete = true;                               // Set completion of read to true
      result = atoi(inData);                               // Changes string data into an integer for use

  return result;

Hope this helps someone out there. Thanks to everyone who posts their code, I would never have got anywhere without you.


I don't understand the presence of the flag "stringComplete".
If I'm reading that correctly, "EZreadead" doesn't return (blocks) unless it has a complete reading, so the flag is redundant.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


AWOL: I am looking to get the same information using TTL. Should I use a different approach than softwareserial?
I specifically ordered a maxbotix rangefinder that uses TTL logic, thinking that it would be easier to interface with the arduino UNO than RS-232 but I see no examples on how to communicate with it using TTL.
Should I post this as a new topic? My apololgies for digging up an old one.


It should be just as easy using TTL, just keep in mind that unlike RS232, TTL is not normally inverted.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Thanks for this Goldthing.
Does anyone out there know how to get a serial (apparently the most accurate) mm resolution reading from a HRLV unit ?


A couple of days ago, I was helping a user with support for this topic (remember I work for MaxBotix), and I noticed that by default when reading the 1mm resolution from the MB1013 if the target was under 1 meter in distance, the Arduino would only read 3 numbers.  As soon as the target was further than 1 meter, the Arduino would output all 4 numbers.

Attached is the code that I gave this end user for support with this topic.

The information given, is from my personal experience and does not represent MaxBotix Inc. in any way.

Hi all,
In order to get the above serial code to resolve 1mm resolution the array has to be changed to 5 characters (from four), otherwise the last digit is not stored (giving you unrounded cm results as opposed to mm results).

Change from:
char inData[4];                                          //char array to read data into
char inData[5];                                          //char array to read data into

Change from:
while (index < 3)
while (index < 4)


Thank you for the correction.  I will make that change in my code, I appreciate this.

What is the purpose of the line:

#define txPin 3   

(And isn't the specific usage of #define outdated?)
I do not current have anything connected to Pin 3 and the code seems to work fine.


It tells the Arduino that this pin may not be used.

I tried removing this part of the code, and I kept getting errors because it wasn't defined.   So to keep from having a headache, I just removed it.

if anyone has a better way to using the serial data connection, please let me know.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131