RS485 Serial Communication Between Arduino Mega and Arduino Pro Mini
Refer: Rs485 Serial Communication Between Arduino
In this project, we will discuss about how to use Max485 for serial communication. 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 data to maximum distance of 4000m or 4km. But normally after 1200km the signal starts dropping off and eventually data starts losing. To achieve 4 km successful data transmission the carrying medium should be smooth and the length of A and B lines should be equidistance in length. External EMF(Electro Motive Force) also affects the lines carrying the data.
Rs485 Transmits two differential signals on A and B lines. Since signals are opposite in polarity(90 Degree out of Phase) they travel a well distance. Since the Signals are opposite in Polarity any external induced signal is easily filtered at the output.
|
RS485 Communication between Arduino Mega and Arduino Pro Mini
Note the function Serial.readString() didn't work in Rs485 Communication because the Serial buffer didn't receive the terminating character. It's because the biasing and terminating resistors they are not calibrated to the impedance of the wire. That's why i have used the statement 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.
Arduino Mega Code
Download the project code from the links given at the bottom of the Post.
void setup() { Serial1.begin(9600);//Using Serial1 Port Serial.begin(9600); pinMode(8, OUTPUT);//DE/RE Controling pin of RS-485 } void loop() { char getdata='m'; digitalWrite(8,HIGH);//DE/RE=HIGH Transmit Enabled M1 Serial1.print('9');//Write '9' and Fetch Data From Pro Mini digitalWrite(8,LOW);//DE/RE=LOW Receive Enabled M1 delay(1000); if(Serial1.available()){ //If Serial Data is available while(Serial1.available() && getdata!='d') { getdata=Serial1.read(); Serial.print(getdata); } Serial.println(""); } }
Project Code
Download the project code from the links given at the bottom of the Post.
void setup() { //Serial1.begin(9600);//Uncomment for Arduino Lenardo Serial.begin(9600); //while(!Serial1);//Uncomment for Arduino Lenardo pinMode(13, OUTPUT);//Led Connected pinMode(8, OUTPUT);//DE/RE Controling pin of RS-485 } void loop() { // put your main code here, to run repeatedly: char getdata='c'; digitalWrite(13,LOW);//Led OFF digitalWrite(8,LOW);//DE/RE=LOW Receive Enabled if(Serial.available()){ getdata=Serial.read(); } if(getdata='9'){ digitalWrite(8,HIGH);//DE/RE=HIGH Transmit Enabled Serial.print("AcruxTek"); Serial.print("Isld"); } delay(2000); digitalWrite(13,HIGH);//Led ON delay(2000); }