Having trouble reading a string with Serial1 from a Maxbotix MB7092 rangefinder

Hi there. Does anyone have experience with hooking up a Maxbotix MB7092 ultrasonic rangefinder with a Photon? I’m talking about this one: https://www.adafruit.com/products/1137
It can output serial data on one of the pins, but it has to be inverted to be readable by a USB/serial adapter. I’m able to read data from it with a TTL level console cable and a simple inverter circuit. It outputs a string looking something like this:
R56\n
where the numeric value is the range in centimeters. I’ve been able to read data from it by using the Serial1 class and hooking the TX pin from the sensor to the RX pin of the Photon. I’m getting a bunch of bytes, but they don’t seem to be ASCII values. If I use the Serial1.readString() function, it blocks forever. If I’m using something like this, I’m getting a bunch of values, but they don’t make any sense when I look them up in the ASCII table:

void setup() {
   Serial1.begin(9600);
   Serial.println("Setting up...");
   delay(1000);
}

void loop() {
  Serial.print("Reading data from Maxbotix...");
  while (Serial1.available()) {
    Serial.println("Reading a byte...");
    int data = Serial1.read();
    Serial.print("data: ");
    Serial.println(data);
  }
  
  Serial.println("---");
  delay(3000);
}

I’m new to the Arduino Wiring language and I’m not exactly sure what size an int is. The reference documentation says the Serial.read() returns a byte, so I’m guessing it’s a signed 8 bit type. However, when printing it out as a decimal, I sometimes get values larger than the ASCII range. There must be a better way of doing byte/string conversion in Wiring. Any help is much appreciated. I’m kind of stuck on this. Thanks.

Also, I’ve tried reading it into a byte instead of an int, but it doesn’t make any difference.

The code looks correct. You say you’re getting values outside the ASCII range - do you mean > 127? The byte will of course give you values up to 255 with the default 8-bit framing of Serial1.

Thanks mdma! Yes, I get values > 127 occasionally. Is there a better way of doing this? The Maxbotix device prints the string mentioned above (R123\n) but a Serial1.readString() blocks forever. Not quite sure what’s going on. I never get anything that is equivalent to an ASCII ‘R’ character or a newline either for that matter. But when I connect the Maxbotix to a USB/serial cable and read it with a terminal program like Kermit, the output looks perfectly fine. I’m just assuming I’m doing something wrong.

Hi @Bluzcat

I just wanted to be sure you knew that you need the inverter for Photon as well. The rangefinder outputs pseudo RS-232 which is inverted relative to TTL/CMOS serial like Photon or Arduino uses. You mentioned that you had an inverter for USB/serial adapter but you need one here too.

So how are you inverting the RX signal?

1 Like

Hi @bko, thanks for the reply. I’m using the inverter with the Photon as well. It’s just an NPN transistor where the TX signal from the rangefinder is connected to the base via a 1KOhm resistor, and the emitter is connected to the RX port of the Photon. I’m powering the rangefinder and the inverter with the 3.3V pin on the Photon. This worked fine with the TTL level FTDI cable I have https://www.adafruit.com/products/70
When I use the cable hooked up to the USB port of my Mac, I can use Kermit and read the expected output from the rangefinder when I use the “poor man’s converter” transistor. I just assumed it would work with the Photon. Anyway, I discovered that the rangefinder also emits an analog voltage scaled by Vcc/1024 per cm. So I was able to hook it up and read the values through one of the analog ports.

@Bluzcat, I think this section of code needs fixing:

    int data = Serial1.read();
    Serial.print("data: ");
    Serial.println(data);

I believe it should be:

    char data = Serial1.read();
    Serial.print("data: ");
    Serial.write(data);
    Serial.println("");

:smile:

Hi @peekay123. Thanks for your reply. Yeah, I experimented with using byte, char, etc, even using the char() and byte() methods, but there was no difference. The characters I was getting didn’t make sense. One of the characters that would frequently show up was ‘+’ (ASCII 43). This is what I was seeing before I used the inverter with the FTDI cable. I’m using the inverter with the Photon as well, but I’m not convinced it’s working as expected in this case. Either way, as I mentioned in my reply to @bko, I’m now using the analog voltage out from the rangefinder and I’m able to get accurate readings by scaling it according to the data sheet. Thanks.

1 Like

Hi @Bluzcat

I am having trouble picturing your inverter circuit since the NPN transistor will supply a current path from collector to emitter when the base is biased positive. If you have the collector tied to +3.3V and the emitter tied to RX, that sounds backwards. In any event, you need another resistor to make the inverter work, particularly with CMOS inputs like the Photon RX pin. The best way would be emitter tied to GND, collector to RX and a 2.2K ohm resistor that continues up to +3.3V. The base would be as you have it.

Glad to hear the analog range pin is working well!

1 Like

Oh @peekay123, I just noticed you mentioned to use Serial.write() instead of println()! I haven’t tried that. Maybe that’s the ticket. But given that I have it working with the analog voltage, I think I might stick to that. But that’s good to know. Since I’m curious, I’ll have to check that out.

Hi @bko. Yeah, I think I had a brain lapse with the inverter. I looked up some schematics online that use a resistor as you describe it. It’s been three decades since I did any tinkering with electronics. At this point, I probably only remember enough to be dangerous :slight_smile:
Just for educational purposes, I will fix the inverter and try the Serial1 code again, but I’ll probably stick with the analog pin since it seems to work fine, and fits my application better. Thanks for the help!

2 Likes