Using Serial Data Pin 5

Connecting The MaxSonar to a Computer

DB9

The MaxSonar can be directly interfaced to be used with your PC if you have a PC with a DB9 Serial connection or a USB to DB9 converter. The DB9 connection is capable of being connected to theTX pin output on the MaxSonar sensor. Connecting the sensor to a computer allows the user to see range readings that have already been processed to distance using RS232.

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 or MAX3232.

When interfacing MaxSonar sensors with a computer, a power input is still required for the sensor to operate. A computer’s DB9 input typically has enough margin to read the serial data output even when the sensor is powered at its minimum required voltage.

We recommend powering that the sensor is powered at 5VDC when connecting the sensor to a computer’s DB9 connection. It is also recommended that the power supplied to the sensor is from a voltage regulator or any source able to provide clean stable power to the sensor.

To interface the sensor with the computer, pin 5 on the DB9 connection must be connected to the GND pin on the MaxSonar® and pin 2 on the DB9 connection must be connected to Pin 5 (TX) on the MaxSonar®. Please refer to figure 1.1 below for the pin layout of DB9 serial connections.

The serial (TX) output delivers asynchronous serial with an RS232 format, except voltages are 0-Vcc. The output is an ASCII capital “R”, followed by ASCII character digits representing the range.  This is then followed by a return carriage. For the maximum range reported for your sensor please refer to your sensor’s data sheet.

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. You can read more on this in the Troubleshooting the Serial Output portion of this article.

Serial Port Configuration Settings

For computers with a DB9 port attached to the motherboard, this port is typically assigned “ComPort 1”. If an adapter such as a USB to DB9 adapter is being used, the computer operating system will assign this port a unique Com Port.

TTL Serial

using serial data

Serial output for a MaxSonar sensor

First, you will want to select the proper COM port on your computer. After the COM port is selected configure the port. Set the bits per second (BAUD) to 9600. Set your data bits to 8 and your parity

to none. If there is a way to set the stop bits, set the stop bit to 1. Final setting if the software has it is Flow Control. If Flow Control is available, set this setting to none.

 

In the past, HyperTerminal came standard on computers with Windows XP or older. For Windows Vista and Windows 7 users, the software has to be downloaded in order to read the serial port.

To download HyperTerminal click here.

A free, open source alternative to using HyperTerminal is Tera Term.

Tera Term is a free software terminal emulator that will assist you in controlling your ultrasonic sensor.

 

Troubleshooting the Serial Output

One common issue when connecting a MaxSonar sensor to a computer is the baud rate, bit, or parity not set correctly. The computer configuration is: a baud rate of 9600, 8 bits, no parity, and one stop bit.

Another common issue when using the serial output of the MaxSonar is inverting. The TX output needs to be inverted from RS232 to TTL to be compatible with TTL devices. To invert the signal you will need an inverter such as a MAX232 or MAX3232.

Some users may not have DB9 connection on their computer and need a USB to serial adapter. USB to serial adapters may have an auto switch that switches between TTL and RS232 which may not work properly with our sensors. To allow proper operation of our sensor with an adapter you may have to invert the serial output (with an inverter like the Max232/Max3232) prior to connecting the sensor to the USB to serial adapter.

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 o MAX3232.

Back to blog

Lets connect on your project!