HRXL MaxSonar 7360

Hi all,

I created this sketch for the sensor HRXL MaxSonar 7360. It’s a outdoor Ultrasonic range finder. I think that you can use this code with other sensors HRXL MaxSonar, you can check the specification inside the datasheet of your version.
I hope that this code it’s helpful for you.

#include <SoftwareSerial.h>

/* The sensor working only in modality Tx ----> pin 5 */
/* Connect the sensor pin 5 at the arduino pin 10 */
serialPin = 10;
fakePin = 11;

/* The serial of this sensor working with the inverted mode */
serialInverted = true;

SoftwareSerial port(serialPin, fakePin, serialInverted);

String charIn;
float distanceInMeters;

void setup() {
  Serial.begin(9600);
  port.begin(9600);
  delay(50);
}

void loop() {
  
  while (port.available() > 0)
    {
        char received = port.read();
        /* Search Syncro byte */
        if (received != 'R'){
          charIn += received;
         /* Process starting when we receive a interline (ASCII 13) char */ 
          if (received == 13)
          {
              /* Trasfomr the mm in m */
              distanceInMeters = charIn.toFloat()/1000;
              Serial.print("meters: ");
              Serial.println(distanceInMeters);
              /* Clear received buffer */
              charIn = "";
          }
        }
        //delay(50);
    }
}

Thanks for sharing,

some remarks,

  • code as posted won’t compile
  • you might use -1 as fakepin so pin11 stays free
  • better implement it without the use of Strings as these are fragmenting the little memory an Arduino has.
  • better implement the essence as a function (even better as a class)
#include <SoftwareSerial.h>

/* The sensor working only in modality Tx ----> pin 5 */
/* Connect the sensor pin 5 at the arduino pin 10 */
/* use a fakepin of -1 for Tx
/* The serial of this sensor working with the inverted mode */
SoftwareSerial port(10, -1, true);

float HRXLMaxSonar()
{
  float d = 0;
  char received = '0';

  // wait for sync
  while (received != 'R');
  {
    while (port.available() == 0) ;
    received = port.read();
  }

  while (received != '\n')
  {
    while (port.available() == 0) ;
    received = port.read();
    if ('0' <= received && received <= '9')
    {
      d = d * 10 + (received - '0');
    }
  }
  return d;
}


void setup()
{
  Serial.begin(9600);
  Serial.println("Start HRXLMaxSonar");
  port.begin(9600);
  delay(50);
}

void loop()
{
  float distance = HRXLMaxSonar();
  Serial.print("meters: ");
  Serial.println(distance * 0.001);
}

Please can you test if this variation works?

Thanks for your help.

I tried your code, but doesn’t work, I don’t see the output.
I think that your indications are very good, and I will try to implement a new version of this sketch following your indications.
You wrote that my code won’t compile, but this morning i tried my code and I can confirm that working correctly. Do you can explain me this problem?

Thank you for your interest and your help

Emilio

example (from post above)

serialPin = 10; fakePin = 11;

both are incorrect code - type information is missing (not a big bug but still a bug)

Hi,

I wrote a new working version of this sketch following your suggestions.

#include <SoftwareSerial.h>
/* The sensor working only in modality Tx ----> pin 5 */
/* Connect the sensor pin 5 at the arduino pin 10 */
/* use a fakepin of -1 for Tx
/* The serial of this sensor working with the inverted mode */

SoftwareSerial port(10, -1, true);

float HRXLMaxSonar()
{
  float d = 0;
  char input[5];
  while (port.available() > 0)
  {
    d = 0;
    port.readBytesUntil('\r', input, 5);
    for(int i=0;i<5;i++)
      if ('0' <= input[i] && input[i] <= '9')
        d = d * 10 + (input[i] - '0');   
   }
   delay(130);
   return d;  /* Distance in mm*/
}


void setup()
{
  Serial.begin(9600);
  Serial.println("Start HRXLMaxSonar");
  port.begin(9600);
  delay(50);
}

void loop()
{
  float distance = HRXLMaxSonar();
  Serial.print("meters: ");
  Serial.println(distance * 0.001);
}

Thanks
Emilio