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. 

MAX485 Pinout
MAX485 Pinout
  • 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 micro-controller. 
  • 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. 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' then 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 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("");
    }
}

You can also use the code below for Arduino Uno, Arduino Leonardo, Arduino Mega etc. For using the code with Leonardo just change the occurrence of Serial to Serial1.

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);
}


Related Products

Arduino MEGA - ATmega2560 Microcontroller Development Board - Arduino Compatible

Arduino MEGA - ATmega2560 Microcontroller Development Board - Arduino Compatible

The Arduino Mega 2560 is a microcontroller board based on the ATmega2560. It has 54 Digital ..

RM60.00

Arduino Pro Mini Microcontroller ATmega328 Development Board 3.3V 8Mhz - Arduino Compatible

Arduino Pro Mini Microcontroller ATmega328 Development Board 3.3V 8Mhz - Arduino Compatible

* You will need a Arduino Pro Mini Programmer to program your Pro Mini. Anothe..

RM15.00

RS485 to TTL Serial Level Signals Converter Module - MAX485

RS485 to TTL Serial Level Signals Converter Module - MAX485

RS485 to TTL Module (supporting EIA-485 standard) is for long range, high data rate err..

RM6.50