I have an Arduino Mega with an MaxBotix (MB7066) sonar attached, the sonar has the ability to send serial data with it's reading. It uses a asynchronous RS232 format, except the voltages are 0-Vcc. The output should be a capital "R" (ASCII), followed by four digits(ASCII) and a carriage return (ASCII 13). The baud rate is 9600, 8 bits, no parity with one stop bit. What I get from the device is as follows: (sonarSerial.read() while sonarSerial.available())


What do I have to get to the information? Or is my sensor just retuning garbage? Please see the code below:

#include <SoftwareSerial.h>

#define pin1 3
#define pin2 5 //pwm
#define pin3 6 //analog
#define pin4 10 //RX for sonar, TX for arduino
#define pin5 11 //TX for sonar, RX for arduino

SoftwareSerial sonarPort(pin5,pin4);

void setup(){
   Serial.begin (9600); 
   pinMode(pin1, OUTPUT);

   pinMode(pin4, OUTPUT);
   pinMode(pin5, INPUT);

void loop(){
  digitalWrite(pin1, HIGH);

  digitalWrite(pin4, LOW);
  digitalWrite(pin4, HIGH);
  digitalWrite(pin4, LOW);  

  long pulse = pulseIn(pin2, HIGH)/58;
  Serial.print("PM Result: ");

  String buffer = "";
  while(sonarPort.available() > 0){
      int inByte = sonarPort.read();
      buffer += inByte; buffer += " ";
          Serial.print("Serial result: "); 
  • Please provide specific model of the sensor you are using. Jan 12, 2015 at 21:17

2 Answers 2


I contacted MaxBotix with the problem and for my surprise their technical support is really great:

The Arduino is set up to natively read TTL serial data.The MB7066 outputs its serial data in the RS232 format. Do not worry; this problem can easily be fixed. You can use an inverter such as the MAX232. You can also follow the following tutorial to make your own https://github.com/simonmonk/raspirobotboard/wiki/Rangefinder-Adapter.

Another option would be to test using this code section SoftwareSerial sonarSerial(rxPin, txPin, true); //define serial port for recieving data, output from maxSonar is inverted requiring true to be set.


In the bitstream of your ASCII data the pattern for "R" (01010010b) does never occur.
So obviously something is wrong in your system and/or setup. Have you already considered the information given here http://www.maxbotix.com/articles/034.htm?

I would connect the sensor to a PC to check whether it's working correctly using a standard terminal software.

  • I don't have the cable yet so no, I have not checked, will later post my code to remove the possibility of a bad setup. Jan 12, 2015 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.