I apologize for the double post, but I asked this question earlier and somehow it does not appear under my user and I can't make any edit or comments to it: Trouble with UART Serial Communication with Raspberry Pi and Arduino Mega.
In this project i will discuss about how to use Max485 for serial communication between two arduino boards. How to effectively configure RS485 in receive and transmit mode. What are advantages of using RS485 protocol for serial communication and finally i will present a working model of RS485 serial communication between arduino Mega and Arduino Pro Mini.
Rs485 serial communication protocol is capable of transmitting digital serial data up to maximum distance of 4000 m or 4 km. But normally after 1200 m the signal starts drooping off and transmitted signal starts losing its strength due to which their is slight chance of data loss. To achieve 4 km successful data transmission the carrying medium should be smooth and the length of A and B lines should be equi-distance in length. External EMF(Electro Motive Force) also effects the lines carrying the data.
Rs485 transmits two differential signals on two different wires, named A and B lines. Since signals are opposite in polarity(90 Degree out of Phase) they can travel to pretty far distance. Transmitted signals are opposite in polarity and any external induced signal during travel can easily be filtered at the output.
Microcontrollers them self can not output rs485 signal caring data. Rather an external device/module is required to be interfaced with microcontroller for achieving rs485 communication. Max485 is popular rs485 transceiver which can be interfaced with any microcontroller for rs485 communication. Microcontroller sends data to max485. Then max485 sends the data out on rs485 lines.
DI (Data In) Data on DI pin is transmitted on A & B lines when the module is in transmit mode. To set module in transmit mode make DE=1 and RE=1. DI pin is connected to Tx pin of Host Microcontroller UART.
RE (Receive Enable) RE pin is Used to configure the module in Receive Mode.
DE (Data Enable) DE pin is Used to Configure the module in Transmit Mode
RO (Receive Out) Data Received on A & B pin is given to RO pin. RO pin is connected to Rx pin of microcontroller.
A & B (Differential Input and Output Pins) Data is transmitted and received on A & B lines.
RS485 Communication between Arduino Mega and Arduino Pro Mini
The demo project below explains how to do serial RS485 communication in practical. Serial data is send between Arduino Mega and Arduino Pro Mini, the communication is Full Duplex. Baud Rate for communication is set to 9600. Arduino mega has 4 Serial channels, I am using Serial Channel-1 for my application. Arduino Pro Mini has only one Serial channel so i am using it in my application. Pin# 8 of Arduino Mega and Arduino Pro Mini is used to control the RE(Receive Enable) and DE(Data Enable) pins of MAX485.
I use cheap arduino rs485 module in the project. I bought them online from aliexpress. Single arduino rs485 module cost me around $2.3. Pre assembled arduino rs485 modules reduces time of making max485 circuit on bread board and all the other messy wiring stuff. Circuit diagram of the project is below.
RS485 Communication Between Arduino Mega & Arduino ProMini, Leonardo etc
Initially Mega is in Transmit mode and Pro Mini is in Receive Mode. Mega transmits character ‘9’ on Serial Channel and then goes in receive Mode, Pro Mini Receives the character from serial line, check’s it if it is ‘9’. If ‘9’ than in turn it sends a string “AcruxTekIsld” back on the Serial line. Mega now in receive mode receives the string and displays it on serial monitor. Pro Mini comes with a pre mounted led connected to pin no 13. This led is toggling after ever 2 seconds.
Note the function Serial.readString() didn’t worked in Rs485 Communication because the Serial buffer didn’t receive the terminating character. Its because the biasing and terminating resistors they are not calibrated to the impedence of the wire. That’s why i have used the statemnet while(Serial1.available() && getdata!=’d’). Which means if data is available in buffer and its not equal to ‘d’ then read the data. In the while fuction i am reading data one by one character from buffer. Serial.readString() works perfectively in Rs232 communication.
You can also use the code below for Arduino Uno, Arduino Lenardo, Arduino Mega etc. For using the code with Leonardo just change the occurance of Serial to Serial1.
Download the project code .ino arduino files and please give us your feed back on the project. In case you have any queries please write them below in the comments section. Suggestions and recommendations are highly appreciated. Feel free for any discussion.