RS-485 and Arduino

As seen in the above illustration from National Instruments, RS-485 ports are identical in appearance to RS-232 but with different pin assignments. You will not find them on contemporary home computer hardware, and they are only rarely seen on legacy machines. They are, however, found on various types of commercial computer and telecom equipment and if you want to explore such devices you’ll need a way to communicate with RS-485 ports. (For example, we recently encountered one while exploring a Harris Communications OpenSky base station controller/transceiver.)

According to the Wikipedia entry:

EIA-485, also known as TIA/EIA-485 or RS-485, is a standard defining the electrical characteristics of drivers and receivers for use in balanced digital multipoint systems. The standard is published by the ANSI Telecommunications Industry Association/Electronic Industries Alliance (TIA/EIA). Digital communications networks implementing the EIA-485 standard can be used effectively over long distances and in electrically noisy environments. Multiple receivers may be connected to such a network in a linear, multi-drop configuration.


You can read more details on RS485 design basics in this 3 page PDF from EETimes Asia.

When hacking commercial and industrial hardware you may encounter RS-485 ports and need a quick and easy way to interface with a PC. If you have access to MAX485 converter chips and an Arduino you can use the circuit and methods described in Paddy’s Blog for interfacing RS485 and Arduino.

Of course, if you don’t wish to build the interface there is always the RS485 Shield for Arduino from RobotShop.

In any event, when exploring a new hardware find, it’s important to remember that not every DB9 connector is an RS232 serial port.

This entry was posted in Arduino, components, data transfer, how-to and tagged , , .

Comments

  1. Paul says:

    If you use Teensy, there is an (undocumented) option to automatically control the DE pin on your MAX485. You just call begin with a baud and pin, like this:

    Uart.begin(9600, 2);

    It will automatically drive pin 2 high when data is transmitting, and low at all other times so you can receive.

    Of course, “Serial” is a USB virtual serial on the native USB port, so you can use the UART (real serial port) for your RS485 without worrying about uploads from Arduino.

  2. Drone says:

    Most implementations of EIA-485 use a master with one or more slaves on a two-wire multi-drop bus. The challenge for some implementations is that there be no single-point-of-failure in the network. So the programmer needs to design the protocol so there is no master – that’s in interesting problem IMO. (Remember, EIA-485 specifies only the physical layer.)

  3. rsdio says:

    RS-422 is another balanced serial standard, older than RS-485 but newer than RS-232. The Macintosh had this RS-422 port, and various NeXT Computers had either RS-422 or RS-423 ports, as many musicians found out when trying to connect MIDI interfaces.

  4. Squonk says:

    First, there is no such thing as standard RS485 connector: see Wikipedia. And from my experience, you actually can find all kinds of DSUB connectors, RJ connectors or screw terminals with all kind of wiring schemes.

    And beside the required differential buffers, the most tricky thing dealing with RS485 is to use the most common form of RS485 link, ie. a single differential pair with ground, working in half-duplex mode.

    As you may have to disconnect from the bus as soon as 1 bit-time after your last character’s stop bit, you either 1) rely on your UART to do that, or 2) send 1 character at a time a drive the control pin (usually RTS) by software…

    Oh, and don’t forget the termination resistors, too.

  5. engr.sajidzaman says:

    hi,
    i want to use RS-485 modules to communicate multiple arduino’s i-e One Master arduino will collect all the data from n number of slave arduino and then the master arduino send the collected data to computer serial, does any one have idea or sourse of material how to do this job?

    • rsdio says:

      That sounds like a fun project. Have you read all of the links provided in the article above?
      Also, look at the manufacturer web site and data sheets for each of the RS-485 chips mentioned. You should find Application Notes in most of them. Grab these Notes and read through them for ideas.
      I think your biggest challenge is how to connect multiple slave RS-485 ports to a single master RS-485 port. The first thing you should do is determine whether the RS-485 Arduino shield is capable of multi-slave operations. Then you’ll need to design your wiring and/or protocol so that only one slave is talking at a time.

      • Paul says:

        Connecting multiple devices together is pretty much exactly what RS-485 does! You just connect a 2 conductor+ground cable between all the chips, and maybe a termination resistor if the line is long (100+ feet). If you use the slew rate limited chips the termination resistor is much less important. How to physically connect them is the easiest part.

        The command/response protocol you use is the tricky part. Each device needs to turn on its transmitting only at the correct time, and turn it off quickly following the stop bit of the last character in each message. Most chips have 4 signal pins, RX, TX, receive enable and transmit enable. Typically the receive and transmit enables are opposite polarity and next to each other, so you can just short them together for a 3 wire interface to your microcontroller.

        The data in the message from the master usually indicates which slave is to respond. A reliable master needs to implement some sort of timeout or error recovery if the slave doesn’t respond, rather than the simplest while(Serial.available()<replySize); which just waits forever if the reply never arrives.

        Driving the transmitter enable pin is the really tricky part. On Teensy there's an extension for this, so the HardwareSerial interrupt does it for you automatically. If using official Arduino, you almost certainly want to get Arduino 1.0.2 (released earlier today). It's HardwareSerial finally fixes a bug in Serial.flush() where it didn't wait until all the bits were sent. With the fixed version, it's easy to use Serial.flush() to wait for the right time to turn the transmitter off. Incidentally, the fix in 1.0.2 came from an adaptation of Teensy's HardwareSerial (I'm the original author… I answered a few questions for someone else who ported my code back to Arduino to fix the bug).

        The other gotcha which some people miss with RS-485 is a brief delay before the slave begins sending a reply, or for the master before it sends the next query. Even if you've received the entire response from the master, its transmitter might be be on for a brief time (especially if you're not using interrupt-based code to shut it off). The delay gives a little time for the line to become idle before you send the next message. If you later use RS-485 optical isolator or repeater products, they usually infer the data direction by which side begins talking first and have timers in the 1 to 2 ms range to turn off the transmitter. A 2+ ms delay before transmitting gives you good compatibility with those RS-485 products.

        Of course, on Arduino Uno there's only 1 Hardware Serial port. You could try using SoftwareSerial, or my improved AltSoftSerial (which can reliably work while the Hardware Serial is running at higher baud rates), or build a hardware mux if you don't need to use both at the same time. But your best bet is to use a board like Teensy or Leonardo that works via USB virtual serial and has a Hardware Serial port which is separate, or Mega or Due or Sanguino (or Teensy 3.0) which has multiple Hardware Serial ports.

  6. rsdio says:

    Great support, Paul. Glad to see you here and your ideas being used in various products!

    • engr.sajidzaman says:

      thanks a lot for help specially for Paul for his explanation ,i have done it through SPI (only two arduino yet) and thinking that which will be better can you suggest me plz.Thanks again Paul.

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.