Sony Arouje

a programmer's log

RS485 Communication Protocol with Micro Controllers and Raspberry pi

with 8 comments

We are in the age of connected devices, devices could talk each other either via RF or wired. In one of my post I explained about Radio Frequency communication using Xbee. In this post lets see how devices can talk each other via wired network. One of the advantage of Wired network is, it is cost effective compare to buying an XBee modules like Xbee pro. I personally prefer Wireless communication as it’s hassle free, just place the devices where ever we need.

In my previous post I explained how to Bootload Atmega16A and program it using Arduino. Next step is to establish the communication between the Micro controllers and Raspberry pi. In this case my Raspberry Pi is acting as the central hub that talk to other controllers and collect data or issue command to do some job.

RS 485 Protocol

I better leave it to Wikipedia to give a detailed explanation of the protocol. In brief using RS485, devices can communicate Full duplex or Half duplex. I used a Half duplex communication. So it’s a protocol, how do we implement the protocol in our hardware, there is a chip Max485 from Maxim. This chip can establish a half duplex communication.

Max485 Pin configuration

Max 485 is a 8 pin chip as shown below. It’s a DIP chip and SMD have different pin layout.

image

Image from Maxim site

Pin 1 – RO: It’s the Data-In pin, devices can read data from the bus using this pin. Rx pin of the Micro controllers should connect to this pin.

RE and DE: Set this pin to Logical High to transmit any data to the bus. Set this pin to Low to receive data from the bus.

Pin 4 DI: Devices can transmit data via this pin.  This pin should be connected to the Tx pin of the device.

Pin 8 and 5 – VCC and GND: To power the chip, VCC should be 5 volt.

Pin 7 and 6 – A and B: Here we connect the data line. A should connect to the A Pin of the next Max 485 and B with B.

 

Connection Diagram

The below picture will illustrate how to connect all the devices together.

RS485_SerialCommunication_bb 

Image created using Fritzing

As per the diagram, if RPi want to transmit any data. We should set a high voltage on GPIO 18 and issue a serial write. All other devices in the network will be in listening mode and can read data using serial read, if any other device wanted to transmit data, then issue a Logical High to RE DE pin and will get promoted as a master and transmit data via serial write.

Now it’s up to you to program it and do some cool things with it.

Advertisements

Written by Sony Arouje

April 23, 2015 at 11:05 pm

8 Responses

Subscribe to comments with RSS.

  1. […] I could do a serial communication with Raspberrypi and Arduino Uno via MAX485 without any issues. For the complete connection detail of MAX485 and other Arduino, check my blog. […]

  2. RS485 is not a protocol. It’s only a physical layer and you have to implement your own protocol to use it.

    Also using raspberry gpio to handle the DE is not a very good idea. Usually there is no real-time os installed on Raspberry so gpio pin has delay to 1 ms to tens of ms and it varies. In that time window your microcontroller could have already responded and you miss it because of the delay.

    Better solutions is to use 555 timer to trigger DE from TX or use a uC. 555 or uC are triggered every TX pulse and maintains DE high about a time of one byte and then disables DE.

    Rs485

    July 31, 2015 at 4:46 pm

    • Thanks for some valid inputs from the creators itself.

      I could receive and send data between RPi and Arduino. You are right and I might loose data. It will be very helpful if you could point to any doc that deals with 555 timer to trigger TX.

      Thanks again.

      Regards,
      Sony Arouje

      Sony Arouje

      August 2, 2015 at 12:34 pm

      • I’m not sure if it was that what I tested: http://serwis.avt.pl/manuals/AVT530.pdf
        I don’t understand polish so don’t know what they are saying but there is a schema.

        You can tune the delay with R1. With that value in the schema DE will go low propably after every bit and then rise again on another but communication works though.

        Titino has used yet another way to control the DE pin: http://www.titino.eu/wp-content/uploads/Titino_schematics_V3.pdf

        Rs485

        August 2, 2015 at 8:07 pm

      • Thank you so much for the immediate reply. I will go through the links. Also the comments will help others as well.

        Regards,
        Sony Arouje

        Sony Arouje

        August 2, 2015 at 8:33 pm

    • In addition to automatic DE control for RPi, I would also add about 120 ohm termination resistor to each end of the cable run between A and B. Value of the termination resistor depends on the cable impedance and 120 ohm is good for CAT5. CAT5 is pretty good for RS485 because it has twisted pairs (good for RS485’s balanced signal) and it is quite cheap. For some cases it would be also good to add additional biasing resistors on one end of the cable run. (http://www.ni.com/support/serial/resinfo.htm)

      I would consider using MAX3485 chips for the Pi because they are for 3.3V and RPi’s UART and GPIO pins’ TTL levels are also 3.3V. Connecting 5V to RPi’s UART or GPIO can fry the board.

      Rs485

      August 2, 2015 at 10:09 pm

  3. Critical error in your diagram
    You failed to include logic level shifters when connecting to the Pi
    All Pi’s, Arduino Due, Zero and MKR1000 boards are strictly 3v3 and they are NOT 5 v tolerant
    This can potentially destroy your computer
    It is critically important to use logic level shifters when driving 3v3 inputs from 5v logic sources

    • Thanks for your valid comment Peter. You are absolutely right, I must use level shifters. This experiment I did a very long time ago and that I was very ignorant about level shifters and nothing about electronics.

      Sony Arouje

      May 16, 2016 at 1:16 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: