Using serial and USB connection?

I want to use a maxbotix mb1030 sensor and i wish to use the "TX" output of this sensor in which it gives:

When the *BW is open or held low, the TX output delivers
asynchronous serial with an RS232 format, except voltages are 0-Vcc.
The output is an ASCII capital “R”, followed by three ASCII character
digits representing the range in inches up to a maximum of 255,
followed by a carriage return (ASCII 13). The baud rate is 9600, 8
bits, no parity, with one stop bit. Although the voltage of 0-Vcc is
outside the RS232 standard, most RS232 devices have sufficient
margin to read 0-Vcc serial data. If standard voltage level RS232 is
desired, invert, and connect an RS232 converter such as a MAX232.
When BW pin is held high the TX output sends a single pulse, suitable
for low noise chaining. (no serial data).

now, it looks like the only way that i can read this input with the arduino is to connect it to the RX pin on the arduino (i have an arduino nano)

i read up on some documentation and it says that this serial connection with the pins 0 & 1 (rx and tx) is the same serial connection that the arduino uses to connect to the computer. How would i go about connecting this so that i can use the digital output from the sensor, power the arduino via USB and display the range numbers in the serial monitor inside the arduino program?

or is this not possible..?

it is okay for me if it is not possible to do but it would be nice to see live ranges show up on my computer for debugging purposes

thanks

The Arduino Nano uses pin 0 for RX and pin 1 for TX.

If you use SoftwareSerial, you can create another serial port for the sensor.

So you can still write debug message to the serial monitor of the Arduino IDE.

so any of the arduino Nano digital pins can be used for serial connection

is the software serial library built into the sketch ?

as in do you need to import the library?

i am new to the arduino software and i have never used extra libraries before

SoftwareSerial is a standard library.

I'm not certain from the description you quoted that the serial port uses TTL levels. I thought that TTL and RS232 used opposite polarity but the description seems to imply that it's using RS232 just with a different voltage. It doesn't do any harm to give it a try, but if you can't get the SoftwareSerial library to work consider the possibility that you may also need to invert signal.

PeterH is right, according to the description the signal has to be inverted.
Is Vcc 5 volt ? Not higher I hope ?

Erdin:
PeterH is right, according to the description the signal has to be inverted.
Is Vcc 5 volt ? Not higher I hope ?

yes Vcc is volts for the sensor. I found a source where it said:

However, the serial connection that the MaxBotix sensors use is a weird beast: It’s TTL-like in that voltage levels are 0 and VCC, but it’s RS232-like in that a low voltage level corresponds to a logical one and a high voltage level to a logical 0. Therefore, the Arduino’s hardware serial connection or SoftwareSerial aren’t usable. So I’ve written my own implementation of RS232-at-TTL-level based on Arduino’s SoftwareSerial code, and it works great.

and his code was:

/*
* USTest - test program for Ultrasound rangers of the Maxbotix EZ range
*
* This uses serial communication for noise-free transfer. Since the Maxbotix
* sensors use RS232 logic but TTL levels, I had to write my own implementation
* of serial communications, which borrows from Arduino's SoftwareSerial code.
*/

#define PIN_USTX 25
#define PIN_USBW 24
#define PIN_SERIAL 15

void setup() {
Serial.begin(115200);
pinMode(PIN_USTX, OUTPUT);
pinMode(PIN_USBW, OUTPUT);
pinMode(PIN_SERIAL, INPUT);
digitalWrite(PIN_USBW, 1);
digitalWrite(PIN_USTX, 0);
}

int bitPeriod = 1000000/9600;

int read() {
char buf[6];
buf[5] = 0;
for(int i=0; i<5; i++) {
int val = 0;
int bitDelay = bitPeriod - clockCyclesToMicroseconds(50);
while(digitalRead(PIN_SERIAL) == LOW);

if(digitalRead(PIN_SERIAL) == HIGH) {
delayMicroseconds(bitDelay/2 - clockCyclesToMicroseconds(50));
for(int offset = 0; offset < 8; offset++) {
delayMicroseconds(bitDelay);
int pinval = digitalRead(PIN_SERIAL);
if(pinval == LOW) val |= 1 << offset;
}
}

buf[i] = val;
delayMicroseconds(bitPeriod);
}

if(buf[0] != 'R' || buf[4] != 13) return -1;
buf[4] = 0;
return atoi(&(buf[1]));
}

void loop() {
int val;

// command ranging (pull TX pin up)
digitalWrite(PIN_USTX, 1);
delayMicroseconds(30);
digitalWrite(PIN_USTX, 0);

// wait >40ms for measurement and processing
delay(50);

// command output over serial and read
digitalWrite(PIN_USBW, 0);
int val = read();
digitalWrite(PIN_USBW, 1);

Serial.print("Range: "); Serial.println(val);

delay(100);
}

it does not seem to work yet.....it might be because of the libraries but i am trying to figure out what he actually did

I'm confused... looking at the datasheet, there is nothing odd about it except that they are using the term RS232. They are simply trying to say that it is TTL level serial. In fact, it is just using a PIC micro for the serial. There is no reason that this wouldn't just work on any serial (software or hardware) port of the Arduino. You connect the TX pin of the MB1030 to the RXD on the Arduino. But RX on the MB1030 is not really serial. It is used to control whether the sensor is ranging or not. Holding it HIGH (or leaving it unconnected), it does ranging and outputs data to the TX pin continuously. Pull it LOW and it stops ranging.

Since that really isn't possible using serial, just connect RX to a normal digital pin and control it that way (or just leave it disconnected if you want it to continuously measure) to turn it on and off.

The following code will output whatever comes out of the MB1030 to the USB serial port to look at. Make sure that the GND pin from the MB1030 is connected to the ground on the Arduino.

#include <SoftwareSerial.h>


/* Connect TX from MB1030 to pin 2, connect RX to pin 4, and connect BW to ground.

SoftwareSerial MB1030(2, 3); // RX, TX - We aren't going to use TX

#define command 4;


void setup(){
MB1030.begin(9600);
Serial.begin(9600);


pinMode(command, OUTPUT);
digitalWrite(command, HIGH); //we just hold it HIGH for continuous measurements 
			     //Control this pin in your code if you want to measure only when commanded.

}

void loop(){

if(MB1030.available()){
char c = MB1030.read();
Serial.write(c);
}
	

}

Retroplayer:
The following code will output whatever comes out of the MB1030 to the USB serial port to look at. Make sure that the GND pin from the MB1030 is connected to the ground on the Arduino.

#include <SoftwareSerial.h>

/* Connect TX from MB1030 to pin 2, connect RX to pin 4, and connect BW to ground.

SoftwareSerial MB1030(2, 3); // RX, TX - We aren't going to use TX

#define command 4;

void setup(){
MB1030.begin(9600);
Serial.begin(9600);

pinMode(command, OUTPUT);
digitalWrite(command, HIGH); //we just hold it HIGH for continuous measurements
     //Control this pin in your code if you want to measure only when commanded.

}

void loop(){

if(MB1030.available()){
char c = MB1030.read();
Serial.write(c);
}

}

Well i tried the code and revised it a little bit and it looks like the signal is always just garbage all the time. It gives me a 0 or a 6 value

i used the "bitwise NOT" command to try and invert the signal using software but it still gives me nothing.

here is my code:

#include <SoftwareSerial.h>


// Connect TX from MB1030 to pin 7, connect RX to pin 4, and connect BW to ground.

SoftwareSerial MB1030(7, 4); // RX, TX - We aren't going to use TX

int command = 4;


void setup(){
MB1030.begin(9600);
Serial.begin(9600);


pinMode(command, OUTPUT);
digitalWrite(command, HIGH); //we just hold it HIGH for continuous measurements 
			     //Control this pin in your code if you want to measure only when commanded.

}

void loop(){
int numberbytes = MB1030.available();
int i = 0;

Serial.println(numberbytes);
Serial.println("-----------");

for (i = 0 ; i < numberbytes ; i++) {

char c = MB1030.read();
int value = atoi(&c);
int value1; 
value1 = ~value;
//Serial.write(c);
//if (value != 0) {
  Serial.println(value, BIN);
  Serial.println(value);
  Serial.println(value1, BIN);
  Serial.println(value1);
  Serial.println("============");
  
//}

}

delay(1000);
	

}

and from that i got this in the serial monitor tool:

0
0
11111111111111111111111111111111
-1

0
0
11111111111111111111111111111111
-1

0
0
11111111111111111111111111111111
-1

0
0
11111111111111111111111111111111
-1

0
0
11111111111111111111111111111111
-1

0
0
11111111111111111111111111111111
-1

0
0
11111111111111111111111111111111
-1

0
0
11111111111111111111111111111111
-1

If you do need to invert the signal then the way to do that would be to enhance the SoftwareSerial library to support an inversion option. I haven't looked into the code, but I'd expect that to be fairly simple to implement.

Yes, an inverter is needed... just found this on the maxbotix site:

TTL Serial
The HR-MaxSonar sensors have added TTL serial to the Pin 5 output. This allows micro-controllers that accept TTL to use our sensors. If you wish to use an LV-MaxSonar or XL-MaxSonar with a TTL compatible micro-controller, it is required to use a hardware inverter when interfacing with the micro-controller. To read more on this skip down to Troubleshooting the Serial Output here

http://www.maxbotix.com/articles/034.htm

But that is a bit confusing because when they say "inverter" later on in the article, they are talking about converters like the MAX232 which switch the voltages up to 12V RS232.

BTW, have you considered using the analog output and just reading that with an analog pin on the Arduino? Seems much more straight-forward.

http://www.maxbotix.com/articles/032.htm

Retroplayer:
Yes, an inverter is needed... just found this on the maxbotix site:

TTL Serial
The HR-MaxSonar sensors have added TTL serial to the Pin 5 output. This allows micro-controllers that accept TTL to use our sensors. If you wish to use an LV-MaxSonar or XL-MaxSonar with a TTL compatible micro-controller, it is required to use a hardware inverter when interfacing with the micro-controller. To read more on this skip down to Troubleshooting the Serial Output here

http://www.maxbotix.com/articles/034.htm

But that is a bit confusing because when they say "inverter" later on in the article, they are talking about converters like the MAX232 which switch the voltages up to 12V RS232.

BTW, have you considered using the analog output and just reading that with an analog pin on the Arduino? Seems much more straight-forward.

http://www.maxbotix.com/articles/032.htm

I need to send this data not over wire, but through a pair of RF transmitters/receivers so thats why i am only allowed to use digital signals :~ :~ :~ :~

.....of course if it were pulse width or analog it would be too easy!

But, either way, there is a microcontroller in between. Otherwise we wouldn't be talking about Arduino code, USB, serial pins at all, would we? So, just get the data with the analog pin, format it however you want and then send it over the wireless link however you want...

Or am I missing something?

Retroplayer:
But, either way, there is a microcontroller in between. Otherwise we wouldn't be talking about Arduino code, USB, serial pins at all, would we? So, just get the data with the analog pin, format it however you want and then send it over the wireless link however you want...

Or am I missing something?

the sensor is away from the arduino. so the sensor is sending the signal to the tx, and then the rx receives it and sends the signal to the arduino (the rx and arduino are connected together) and then the arduino processes the signal

i would need another microcontroller if serial cant happen...

Too many things that can go wrong.

Develop it step by step.
You could make a small adapter board to connect the sensor to a computer. So you can test the sensor.
For example a USB-to-ttl-level-converter with an inverter to connect the sensor to a computer.
After that connect it directly to the Arduino.
After that test your wireless RF modules.
After that connect the sensor to the RF modules.

About serial data:
Lets call the TTL 5V level RX and TX from the Arduino the 'real' and 'normal' data.
The RS-232 with the higher voltages uses an inverted signal. That is why the MAX232 makes higher voltages, but also inverts the signal.

The sensor is neither of these. It uses an inverted signal, as if it was the higher voltage RS-232. But it uses TTL 5V levels for that (assuming Vcc is 5V). They did that to be able to connect it directly to a serial port of a PC. That was a not-so-smart trick 15 years ago, but since less PCs have a serial port it is now a burden.

jeff77789:
the arduino is sending the signal to the tx, and then the rx receives it and sends the signal to the arduino (the rx and arduino are connected together) and then the arduino processes the signal

I don't quite follow that, but if you're using an external transmitter/receiver to connect to the device, how about putting a hardware inverter in between the Arduino and the sender/receiver or between the device and the sender/receiver at the far side? It should only need a couple of transistors, and would convert the '5V RS232' to a more conventional TTL serial stream.

I am having trouble following that too. Perhaps we need to see a block diagram of the system to help. I think you are trying to say that the sensor is going to be connected directly to RF transmitter and the RF receiver will be connected to the Arduino. If this is correct, then the received side will already be in compatible TTL format, so your code will not need to be anything special. However, you would want to invert the data from the sensor as Peter suggested on the RF transmitter side.

Doing it this way, you will be stuck with only continuous measurement mode since you will not be able to control the BW and RX pins of the RB1030 this way unless you added another RF module to go the other way. And if you did that, it would probably just be cheaper to put a microcontroller in there to do what you want, process the data, and format it in a friendly way.

Note that some of these RF modules support an analog input, BTW.

Serial is not impossible, we are just having some trouble trying to picture exactly what you are trying to do and taking stabs in the dark. I think that if you showed us an overall system diagram, it will help us help you.

sorry for the typo. The sensor is connected to the Tx. the arduino is connected to the Rx.

As of right now I dont even have the RF tx/rx connected. It is a straight wire connection between the arduino and the sensor and none of the code seems to work

Okay here is a picture. Hopefully it clears things up. My ultimate setup will have an LED and a potentiometer along with two sensors with 2 tx/rx pairs at different frequencies

you see that what i have right now is very simple and it just consists of a usb cable to the arduino, power supplied to the sensor and a data cable between the sensor and the arduino

i cant even get the "what i have now" working!!! :0 :0 :0

and just to appease you guys, the led is to indicate that the range is too close and the potentiometer is for an adjustable threshold

bump